Moving data from datatable to datagridview in c #

I have a C # program that selects data from two different database files and combines the data I need into a datatable (dt). All the information I need is in this datatable and I want to put it in a datagridview. Besides the information in the datatable, I also have two columns in the datagridview, which I will calculate by adding each row to the datagridview (dataGridView1).

My question is: how can I get my datatable (dt) in datagridview (dataGridView1)? Would I do something like this ?:

dataGridView1.column("MemberSep") = dt.column("MBRNUM); 

I think that I could iterate over the data, compute the values ​​for my first two columns of the datagridview, and then write it all in a row until I read the entire data type. I have never worked with a datagridview control before. Any help would be greatly appreciated.

Picture of my app

+6
source share
4 answers

Set the DataGridView DataSource to a DataTable .

+15
source
+2
source
 DataTable table = new DataTable(); //add in tables table.Columns.Add("Column 1", typeof(int)); table.Columns.Add("Column 2", typeof(int)); //add in rows table.Rows.Add(1, 2); 

That would mean iterating through dbf and grabbing the column names, then inserting them 2 for each (column) loops, and then adding your own columns that you need. You can also delete any link columns that are identical in each column.

Then 2 more for each row (row), and you can fill in the rows of data.

All that remains is a for each row in the table to calculate the values ​​of the custom columns for that row.

Depending on the dbf relationship, you can just tweak the loops.

+1
source

If you want to manually populate the datagridview, I suggest using strong typed data and rows from Dataset. In this case, you do not need to worry about column names.

0
source

Source: https://habr.com/ru/post/894047/


All Articles