Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have developed a c# windows forms application which will get particular data from an SQL table and displays the data in the data grid view in the form.

For example, there is a textbox in c# form. If a user fills the text box with what he needs, it will get the particular data of the user entry field and display it in the data grid view.

Now if a user got the data what he needs, but he want to change the data in the table. then how can we update the table from the data grid view. I mean if a user gets data as like below:-

Customer id name Year of birth
1250 Chaitanya 1982

Now the user want to change the customer id, he just want to type 1350 in place of 1250 and clicks a button then it should update in the sql table. Is it possible? may i know how can we do that.

What I have tried:

using (new SqlCommandBuilder(adapter))
{
try
{
DataTable dtPrimary = new DataTable();

adapter.Fill(dtPrimary);

// this would be a record you identified as to update:
dtPrimary.Rows[1]["pv_name"] = "value";

sqlConn.Open();
adapter.Update(dtPrimary);
sqlConn.Close();
}


but the value should be given manually in the code here, i need the value which we enter in the datagridview should update in sql table when a button is clicked
Posted
Updated 19-May-16 1:37am

1 solution

If you show the data then gridview then you can save the changed value in database, for that you can use 'CellValueChanged' event
see below snippet
C#
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){
  if(dataGridView1.Columns[e.ColumnIndex].Name == "Reference"){
    //your code goes here
  }
}

OR 'CellEndEdit' will also works for you
C#
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){
  if(dataGridView1.Columns[e.ColumnIndex].Name == "Reference"){
    //your code goes here
  }
}

in above example 'reference' is the column name for an example.
hope it helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900