c# - adding datarow to datagridview only adding to first column -
i'm trying pull set of data database , shove in datagridview. problem is, when this, "system.data.datarow", , adds first column (i've got 5). here's code.
sqlcommand sqlcom = new sqlcommand("some sql query string", "sql database connection info"); sqldataadapter adapter = new sqldataadapter(sqlcomm); datatable table = new datatable(); if (methodtype = "select") { sqlconn.open(); adapter.fill(table); foreach (datarow row in table.rows) { dgvcrslookupresults.rows.add(row[0]); dgvcrslookupresults.rows.add(row[1]); dgvcrslookupresults.rows.add(row[2]); dgvcrslookupresults.rows.add(row[3]); dgvcrslookupresults.rows.add(row[4]); } }
obviously, fills in first column, can't figure out life of me how add each column instead. when use
dgvcrslookupresults.columns.add(row[0]);
it says has invalid arguments. know i'm damn close, i'm new of this, i'm lost how make last jump. thoughts?
instead of looping foreach(datarow row in table.rows)
use:
dgvcrslookupresults.datasource = table;
remember set:
dgvcrslookupresults.autogeneratecolumns = true;
or in designer create columns datagridview.
in column set datapropertyname
= columnname in datatable
in case -> dgvcrslookupresults.autogeneratecolumns = false;
Comments
Post a Comment