I load data from SQL table in a DataGridView with the following code:
SqlConnection conn;
string connectionString = @"Server=myServer; Initial Catalog=MyDatabase; Integrated Security=True;";
using (conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT_ENTRIES", conn);
cmd.CommandType = CommandType.StoredProcedure;
//Fill The DataTable From Command
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
//Set It To Binding Navigator
gridDetailsView.DataSource = dt;
gridDetailsView.Dock = DockStyle.Fill;
}
string connectionString = @"Server=myServer; Initial Catalog=MyDatabase; Integrated Security=True;";
using (conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT_ENTRIES", conn);
cmd.CommandType = CommandType.StoredProcedure;
//Fill The DataTable From Command
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
//Set It To Binding Navigator
gridDetailsView.DataSource = dt;
gridDetailsView.Dock = DockStyle.Fill;
}
I've created stored procedures for updating (UPDATE_ENTRIES) and insert new data (INSERT_ENTRIES) to the table in SQL Server.
How can I update the table when the user change row in DataGridView?
Thank you in advance.
Munesh SharmaPosted Apr 9, 2014, 2:39 AM
and this
http://social.msdn.microsoft.com/Forums/windows/en-US/ef489f1a-48c6-4f8f-a8b7-f547b7293a96/how-to-update-a-datagridview-which-is-binded-to-a-stored-procedure-having-parameters-and-multiple?forum=winformsdatacontrols
these links will help you.
DealyPosted Apr 10, 2014, 4:15 AM
I went with this solution for Update/Delete
http://www.dotnetsharepoint.com/2013/07/insert-update-delete-edit-in.html#.U0ZOp_mSzft
When the user adds new records in dataGridView, I'm using the following code in FormClosing event for inserting the data to SQL table.
using (conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("INSERT_ENTRIES", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@EmployeeName", SqlDbType.NVarChar));
cmd.Parameters.Add(new SqlParameter("@EntryDescr", SqlDbType.NVarChar));
cmd.Parameters.Add(new SqlParameter("@InsUserID", SqlDbType.NVarChar));
conn.Open();
foreach (DataGridViewRow row in gridDetailsView.Rows)
{
if (!row.IsNewRow)
{
if (string.IsNullOrEmpty(row.Cells[2].Value.ToString())) // Hidden column with Autonumber
{
cmd.Parameters["@Department"].Value = department;
cmd.Parameters["@EmployeeName"].Value = row.Cells[4].Value;
cmd.Parameters["@EntryDescr"].Value = row.Cells[5].Value;
cmd.Parameters["@InsUserID"].Value = System.Environment.UserName;
cmd.ExecuteNonQuery();
}
}
}
}
Thank you again for all the help.
saurabh mittalPosted Apr 9, 2014, 8:33 AM
you can refer to these links given below.may be it will hepl u.
http://dotnetsridhar.blogspot.in/2012/04/datagridview-in-wndows-form.html
http://www.dotnetcurry.com/showarticle.aspx?ID=132
http://www.c-sharpcorner.com/uploadfile/kannagoud/insertupdatedelete-in-datagridview-using-linq-in-windows-forms/
JobPencilPosted Apr 9, 2014, 7:09 AM
http://dotnetsridhar.blogspot.in/2012/04/datagridview-in-wndows-form.html
DealyPosted Apr 9, 2014, 1:10 AM
JobPencilPosted Apr 8, 2014, 8:19 AM
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html
Thanks
Find Jobs