0

DataGridView在那个组合框中,在选择组合框值后从一个表中加载组合框值我想用相应的数据更新其他列,但这仅适用于我想更新所有行的一行..请对代码更改提出任何建议。

private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)  
{
    if (dataGridView2.IsCurrentCellDirty)
    {
        for (int i = 0; i < (dataGridView2.Rows.Count)-1; i++)
        {

            try
            {
                if (dataGridView2.Rows[i].Cells[1].Value.ToString() != "")
                {
                    ConnectionDB gridRdata = new ConnectionDB("SELECT * FROM Ready_Made_Master WHERE RM_Name='" + dataGridView2.Rows[i].Cells[1].Value.ToString() + "';");
                                DataTable redydata = gridRdata.returntable();
                                dataGridView2.Rows[i].Cells[2].Value = redydata.Rows[i][2].ToString();
                }
            }
            catch
            {


            }

        }
    }
}
4

1 回答 1

0

在for循环之后,尝试重新绑定gridview,即

for (int i = 0; i < (dataGridView2.Rows.Count)-1; i++)
{

}
ConnectionDB gridRdata = new ConnectionDB("SELECT * FROM Ready_Made_Master");
DataTable redydata = gridRdata.returntable();
gridRdata .Datasource=redydata ;
gridRdata .Databind();

请在 Select 语句中进行必要的更改。

于 2012-11-21T06:45:53.660 回答