0
votes

How to create a new data table from two or more datagridview values c#

I have windows form which has multiple datagridviews has no Database, my purpose is to create reports with user entered values. Please suggest me how to make a datatable with more than one datagridview values

        DataTable dt1 = new DataTable();

        dt1.Columns.Add("Gour", typeof(string));
        dt1.Columns.Add("Seal", typeof(string));
        dt1.Columns.Add("Melting", typeof(string));

         foreach (DataGridViewRow dgr in dataGridView1.Rows)
        {
            if (dgr.Cells[0].Value != null)
                dt1.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value);
        }

I have to add columns to this same datatable dt1 with values from dataGridview2

2
Welcome to Stackoverflow. Please make sure that you have read "how to ask a good question". Following these guidelines will make it easier for others to answer, and the responses you get will be better and show up quicker. Details are described here: stackoverflow.com/help/how-to-ask - tfv

2 Answers

0
votes

You can get the user values to objects (UserValueObject) and then you can create List because you might get multiple values.

List<UserValueObject> myUserValueList;

Add your objects to the myUserValueList. Then bind it to a datagridview.

Then you can bind it to the datagridview that you want as bellow:

YourGridViewName.DataSource = myUserValueList;
GridView_List.DataBind();
0
votes

I solved it by creating multiple sub reports for each datagridview in in crystal reports