4
Answers

Cannot update datatable using datagridview

Luke Baker

Luke Baker

12y
3.6k
1
Im currently building a program to view and update data from a database using data grid views. I have text boxes and combo boxes to filter the data in the data grid view. This is achieved with the below code -
[code]
public void con(string command, DataGridView dgv, bool update)
    {
        string conStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\NTO.mdf;Integrated Security=True;User Instance=True";
        SqlDataAdapter da = new SqlDataAdapter(command, conStr);
       
        SqlCommandBuilder cBuilder = new SqlCommandBuilder(da);
        DataTable dt = new DataTable();
            da.Fill(dt);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dt;
            dgv.DataSource = bSource;
   
        }
[/code]

The problem being is getting to update the data when the data in the data grid view has been changed. I understand that you have to do dataAdapter.Update(datatable) but trying to do this when calling the con() method proves difficult??

ANY IDEAS??



Answers (4)