I'm building an app in C# in the Visual Studio 2008 Express Edition that uses two datagridview controls in a master / detail relationship.
On the master datagridview I want to reflect a summary column from the details records for each master record. In particular, the master datagridview shows bills outstanding, and the details datagridview shows payments towards each of those bills.
So, for each row in the master datagridview I want to have an unbound column that displays the total amount paid to each of the bills. I'm very comfortable with the SQL side of this task, but I'm trying to pin down how (and when, in the lifecycle of the master datagridview control) I go about updating the column in each row to show the summed payment amounts.
Both datagridview controls on the form are based on TableAdapter / BindingSource controls.
Any help will be much appreciated!
All the best,
Based on my understanding, you want to add a column to your Master DataGridView which should show the total value of your detail DataGridView, don't you?
I think this could be done with a Expression Column, but it should be done with your DataSet.
For example, please add two DataGridViews onto the Form.Code Snippet
private void Form1_Load(object sender, EventArgs e)
DataTable master = new DataTable("master");
DataTable detail = new DataTable("detail");
detail.Rows.Add(1, "John", 100);
detail.Rows.Add(2, "Mary", 200);
detail.Rows.Add(2, "Jack", 250);
detail.Rows.Add(1, "Rose", 300);
DataSet sample = new DataSet("sample");
DataRelation relation = new DataRelation("relation", sample.Tables.Columns, sample.Tables.Columns);
//Here we got a sample DataSet with 2 DataTables and a DataRelation.
master.Columns["expColumn"].Expression = "SUM(Child.price)";
//Add a column to show the sum values.
BindingSource masterSource = new BindingSource();
masterSource.DataMember = sample.Tables.TableName;
masterSource.DataSource = sample;
BindingSource detailSource = new BindingSource();
detailSource.DataMember = sample.Relations.RelationName;
detailSource.DataSource = masterSource;
dataGridView1.DataSource = masterSource;
dataGridView2.DataSource = detailSource;
Hope this helps,