开发者

update dataGridView in winform app

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()开发者_Python百科;
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)?


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.


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.


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.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜