简体   繁体   中英

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 that I need into a datatable (dt). All the information I need is in that datatable, and I want to put it into a datagridview. Besides the information in the datatable, I also have two columns in the datagridview that I'll calculate as I add each row to the datagridview (dataGridView1).

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

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

I'm thinking that I could loop through the datatable, calculate the values for my first two columns of the datagridview, and then write it all to a row until I've read the whole datatable. I've never worked with a datagridview control before. Any help would be greatly appreciated.

我的应用程序的图片

将DataGridView的DataSource设置为DataTable

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

This would just mean iterating through the dbf's and grabbing the column names then inserting them with 2 for each (column) loops, then add in your custom columns you need. Also could drop any linking columns that are identical in each column.

Then 2 more for each (row) loops and you can populate the data rows.

All that is left is a for each row in table to calculate the custom column values for that row.

Depending on the relationships of the dbf's you can just tweak the loops.

If you want to manually fill datagridview I suggest to use strong typed datatables and rows from Dataset. In this case you don't have to worry about column names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM