Reputation: 103
I have in my winform app a dataGridView and I displayed the data from the DAL class with dataSet like this
DataSet ds2 = DAL.Display_all();
dataGridView1.DataSource = ds2;
dataGridView1.DataMember = "To_display";
How can I update Back my data if If someone change data in the gridView ( I need to sand back to the DAL class)?
Upvotes: 0
Views: 975
Reputation: 19862
Use the DataAdapter.Update
method.
Pass the DataSet
as the parameter.
You can use DataSet.HasChanges
to see if you have any changes before calling the Update method.
Upvotes: 1
Reputation: 5916
If you loop through each row of the DataGridView you can use the below method to get the value of a specified Column on a specific row.
private T GetDataGridViewTextBoxValue<T>(int rowIndex, string columnName)
{
try
{
return (T)Convert.ChangeType(dataGridViewImport.Rows[rowIndex].Cells[columnName].Value, typeof(T));
}
catch (Exception e)
{
throw new InvalidOperationException(String.Format("Row : {0}, column : {1} could not be cast to {2}", rowIndex, columnName, typeof(T)), e);
}
}
And you can use the method like this.
for(int i=0;i< ds2.Tables[0].Rows.Count(); i++)
{
var column1 = GetDataGridViewTextBoxValue<string>(i, "column1");
//Get the rest of the row values.
//In your DAL class you must have a Method that updates the row
//and then just pass in the values that you want.
}
Upvotes: 0
Reputation: 6489
try this :
dataGridView1.ResetBindings();
Causes a control bound to the BindingSource to reread all the items in the list and refresh their displayed values. MSDN
hope this help.
Upvotes: 0