如何使用多个表更新GridView

时间:2014-03-27 09:46:43

标签: c# winforms gridview devexpress multiple-tables

我有一个带EditingMode = EditFormInplace.的DevExpress GridView 我尝试用多个表更新GridView并获得错误

  

多个基表不支持动态SQL生成

我认为,将UpdateCommand添加到dataAdapter是件有用的。但我不明白如何获取Update查询的参数值(来自编辑行)。

使用多个(已加入)表更新DataSource的正确方法是什么?

代码:

public Form2()
{
InitializeComponent();

dataAdapter = new SqlDataAdapter("select t.Id, t.DT, t.Name, t.Age, l.SecondName from TestTime t left join TestTimeLinked l on l.Id = t.Id", Properties.Settings.Default.MARKETConnectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;

dataAdapter.Fill(table);
bindingSource1.DataSource = table;
gridControl1.DataSource = bindingSource1;
}

private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
 {
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MARKETConnectionString))
 {
 conn.Open();
 dataAdapter.Update((DataTable)bindingSource1.DataSource);
 }
 } 

1 个答案:

答案 0 :(得分:0)

DataRow r = gridView1.GetDataRow(e.RowHandle);获取已修改的行值。

完全方法代码:

 private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
    {
        DataRow r = gridView1.GetDataRow(e.RowHandle);

        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.MARKETConnectionString))
        {
            conn.Open();

            dataAdapter.UpdateCommand = conn.CreateCommand();
            dataAdapter.UpdateCommand.CommandText = "UPDATE TestTimeLinked set SecondName = @Name where Id = @Id";
            dataAdapter.UpdateCommand.Parameters.AddWithValue("Name", r.Field<string>("SecondName"));
            dataAdapter.UpdateCommand.Parameters.AddWithValue("Id", r.Field<int>("ID"));
            dataAdapter.UpdateCommand.ExecuteNonQuery();
        }
    }