I'm working on a project with a DataGridView that is filled using a SQL connection. Is there a way to modify (code-behind) the number/names of columns in the DataGridView based upon the fields retrieved by the SQL string?
*Databinding is not an option because I have multiple functions for the DataGridView depending upon a radio button option and each function retrieves different SQL data.
You can Add columns to the DGV programatically. That way you can dictate the HeaderText, DefaultCellStyle, etc. The important thing, when you are adding a column, is to set the DataPropertyName. This needs to correspond to your data source. If you retreive a column of data and there is no column in the DGV with that specific DataPropertyName the DGV will create a column, and it won't look nice.
Bookmarks