i fill the datagridview with the students marks. ( i m using sqlserver as dbms)
now i want that if i do any changes in the data DGV , for example
if i edit the three students marks in datagridview and when i cilck update then only these three students data update in database not the whole students data which is appeared in the DGV. Means only update the three row which i have edit.
how can i do this please help me?
Loading

Kirtan PatelPosted Oct 9, 2009, 8:48 AM
Here is What you want I have also included Project Files So you can take reference if you don't understand by just reading code :) download link is At bottom of post
don't forget to mark "Do you like this answer" please it will give me some credits :)
namespace WindowsFormsApplication17
{
public partial class Form1 : Form
{
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("Select * from Users",con);
SqlDataAdapter da = new SqlDataAdapter(comm);
ds = new DataSet();
da.Fill(ds, "vtable");
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
DataSet DataSetwithModifiedRows = ds.GetChanges();
DataTable dt = DataSetwithModifiedRows.Tables[0];
foreach (DataRow r in dt.Rows)
{
string Username = r[0].ToString();
string Password = r[1].ToString();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("update Users set Password=@password where username=@username", con);
comm.Parameters.AddWithValue("@Username", Username);
comm.Parameters.AddWithValue("@password", Password);
comm.ExecuteNonQuery();
con.Close();
}
MessageBox.Show("Data Updated!!");
}
}
}
Download Project Files
Vikas AhlawatPosted Oct 9, 2009, 9:31 AM
Vikas AhlawatPosted Oct 8, 2009, 11:32 AM
Kirtan PatelPosted Oct 8, 2009, 11:01 AM
I have Answer about your Question ...I will reply you tomorrow :)
Vikas AhlawatPosted Oct 8, 2009, 3:33 AM
at this event i want to collect the changed row in datatable and when i click on button then this datatable row updated in the database. m i doing right?
or tell other simple way if u have any?
Roei BarPosted Oct 8, 2009, 2:50 AM
create a collection for changed Rows
add an event to the table
then on buttonclick run on the collection and Update the DB