Question

I have a DevExpress GridView with EditingMode = EditFormInplace. I try to update GridView with multiple tables and get error

Dynamic SQL generation is not supported against multiple base tables

I think, it is nessesary to add UpdateCommand to the dataAdapter. But I don't understand how to get the parameters values for the Update query (from edited row).

What is the correct way to update DataSource with multiple (joined) tables?

Code:

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);
 }
 } 
Was it helpful?

Solution

DataRow r = gridView1.GetDataRow(e.RowHandle); to get edited row values.

And completely method code:

 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();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top