Insert, Update, Delete With DataGridView Control in C#
Insert, Update, Delete With DataGridView Control in C#
http://huizhaocode.wordpress.com/2012/07/06/insert-update-delete-with-datagridview-control-in-c-windows-application/comment-page-1/ 1/4
HUIZHAOCODE
Insert, Update, Delete with DataGridView Control in C#
(Windows Application)
DataGridView control is one of the coolest features of Dot Net Framework. You can use this control in Windows Form using Wizard or
programmatically.
Add a DataGridView control and two Buttons on a Form. One Button is for deleting the selected row and other one is for updating or inserting
new row.
Declare those variables as shown below.
In the Form Load event set data source for the DataGridView control.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public partial class DataTrialForm : Form
{
private String connectionString = null;
private SqlConnection sqlConnection = null;
private SqlDataAdapter sqlDataAdapter = null;
private SqlCommandBuilder sqlCommandBuilder = null;
private DataTable dataTable = null;
private BindingSource bindingSource = null;
private String selectQueryString = null;
public DataTrialForm()
{
InitializeComponent();
}
1
2
3
4
private void DataTraiForm_Load(object sender, EventArgs e)
{
connectionString = ConfigurationManager.AppSettings["connectionString"];
sqlConnection = new SqlConnection(connectionString);
19/6/2014 Insert, Update, Delete with DataGridView Control in C# (Windows Application) | huizhaocode
http://huizhaocode.wordpress.com/2012/07/06/insert-update-delete-with-datagridview-control-in-c-windows-application/comment-page-1/ 2/4
To update, insert or delete data in database from DataGridView have a look at this code snippet.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
selectQueryString = "SELECT * FROM t_Bill";
sqlConnection.Open();
sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataGridViewTrial.DataSource = bindingSource;
<strong>// if you want to hide Identity column</strong>
dataGridViewTrial.Columns[0].Visible = false;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void addUpadateButton_Click(object sender, EventArgs e)
{
try
{
sqlDataAdapter.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
try
{
dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
sqlDataAdapter.Update(dataTable);
}
19/6/2014 Insert, Update, Delete with DataGridView Control in C# (Windows Application) | huizhaocode
http://huizhaocode.wordpress.com/2012/07/06/insert-update-delete-with-datagridview-control-in-c-windows-application/comment-page-1/ 3/4
You May Like
1. Profiles by
VICE: Teenage Bullfighters a week ago
vice.com VICE VICE Campaign
(sponsored) stuff
2.
COMMENTS 1 Comment
CATEGORIES C#, Windows Form
One Response to Insert, Update, Delete with DataGridView Control in C#
(Windows Application)
Jayden Bellus May 11, 2013 at 7:38 am #
Hi!
DataGridView control are used very frequently in C#. It has various type of functionality but comman funcatin are CRUD operation. So
thanks for sharing your
kanowledge. There are few other links that have described CRUD (Insert, Delete, Update) operation with good explaination and proper
20
21
22
23
24
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
About these ads (http://en.wordpress.com/about-these-ads/)
19/6/2014 Insert, Update, Delete with DataGridView Control in C# (Windows Application) | huizhaocode
http://huizhaocode.wordpress.com/2012/07/06/insert-update-delete-with-datagridview-control-in-c-windows-application/comment-page-1/ 4/4
kanowledge. There are few other links that have described CRUD (Insert, Delete, Update) operation with good explaination and proper
sample. I hope thats helpful for
beginners.
http://www.mindstick.com/Articles/9422cfc8-c2ed-4ec1-9fab-589eb850a863/?
Insert%20Delete%20Update%20in%20DataGridView%20with%20DataTable%20in%20C
http://www.dreamincode.net/forums/topic/238727-insert-update-and-delete-records-in-table-with-datagridview-using-c%23/
REPLY
CREATE A FREE WEBSITE OR BLOG AT WORDPRESS.COM.
THE BUENO THEME.
Follow
Follow huizhaocode
Powered by WordPress.com