Setting value of unbound column for each row in datagridview?


  • Hi All,


    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,





    Sunday, December 16, 2007 6:00 AM


  • Hi, planetthoughtful,


    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");

                master.Columns.Add("id", typeof(int));

                master.Columns.Add("name", typeof(string));

                master.Rows.Add(1, "abc");

                master.Rows.Add(2, "bcd");



                DataTable detail = new DataTable("detail");

                detail.Columns.Add("id", typeof(int));

                detail.Columns.Add("user", typeof(string));

                detail.Columns.Add("price", typeof(int));

                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[0].Columns[0], sample.Tables[1].Columns[0]);



                //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[0].TableName;

                masterSource.DataSource = sample;


                BindingSource detailSource = new BindingSource();

                detailSource.DataMember = sample.Relations[0].RelationName;

                detailSource.DataSource = masterSource;


                dataGridView1.DataSource = masterSource;

                dataGridView2.DataSource = detailSource;





    More info


    Hope this helps,


    Thursday, December 20, 2007 6:33 AM