Controls and Properties
To show the records in DataGridView and insert data into the
Database table in this project I have used following controls:-
a. TextBoxes: Name id="txtname", FatherName id="txtfname",
age id="txtage", emailid id="txtemail, userid id="txtuserid".
b. Button: id="btnInsert" text="Insert".
c. Button: id="btnShow" text="Show Records".
d. DataGridView: id="dgRecords".
NameSpace:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;
Code Page:
1. Insert Records into DataBase :
private void btnInsert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
try
{
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\asp.net helper\\Window Project\\19. BindDataGrid\\BindDataGrid\\BindDataGrid\\DataGridView.mdf;Integrated Security=True;User Instance=True";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "insertRecord";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Clear();
com.Parameters.AddWithValue("@name", txtName.Text.Trim());
com.Parameters.AddWithValue("@fname", txtFname.Text.Trim());
com.Parameters.AddWithValue("@age", txtAge.Text.Trim());
com.Parameters.AddWithValue("@email", txtEmail.Text.Trim());
com.Parameters.AddWithValue("@userid", txtUserid.Text.Trim());
con.Open();
int i = com.ExecuteNonQuery();
if (i > 0)
MessageBox.Show("inserted successfully", "DataGridView");
else
MessageBox.Show("not inserted", "DataGridview");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "DataGridView");
}
finally
{
con.Close();
}
}
2. Show Records in DataGridView Control from DataBase:
private void btnShow_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
try
{
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\asp.net helper\\Window Project\\19. BindDataGrid\\BindDataGrid\\BindDataGrid\\DataGridView.mdf;Integrated Security=True;User Instance=True"; ;
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "showRecord";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Clear();
con.Open();
SqlDataReader dr = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dgRecords.DataSource = dt;
setColumnWidth();
showColumn();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "DataGridView");
}
finally
{
con.Close();
}
}
3. Set the Column Width in DataGridView Control:
public void setColumnWidth()
{
dgRecords.Columns[1].Width = 100;
dgRecords.Columns[4].Width = 200;
dgRecords.Columns[5].Width = 200;
}
4. Hide particular Column in DataGridView Control :
public void showColumn()
{
dgRecords.Columns[0].Visible = false;
dgRecords.Columns[2].Visible = false;
dgRecords.Columns[3].Visible = false;
}
http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples
Premalatha
Software Engineer
Savari_Arm, if this helps please login to Mark As Answer. | Alert Moderator