DataSet for DataGridView with predefined columns

I have a DataGridView that I installed in a WinForms project. Columns are defined in the constructor file. When I run my SQL, the columns in the DataGridView are replaced with the column names in the query.

How to save columns defined in a DataGridView, and just add rows from a dataset from a dataset to a GridView.

//Columns as they appear in the DataGridView
 Name | Address | City | State | Zip | Account Num |

//Populate the gridView
DataSet ds = Account.search(strSearch);
accountGrid.DataSource = ds.Tables[0];

//query
string sql = "SELECT distinct acctNum, name, address, city, state, zip from accounts";
return DataService.GetDataSet(sql);

//The End Result of the columns
acctNum | name | address |city | state | zip
+5
source share
1 answer

You need to bind the columns in the DataTable to the columns in the DataGridView.

, DataPropertyName DGV DataTable, DGV. , DataPropertyName DGV acctNum. , " " ( , DGV " ..." ).

, DataGridView AutoGenerateColumns false, . , .

+6

All Articles