'Fill the DataSource of a DataGridView with existing Columns
Is there a way to fill the DataSource of a DataGridView using the existing Columns?
I'm trying to set a DataSource to my DataGridView; every time I set my DataSource, it creates new Columns, which is not the expected result.
I want the data to be presented in the existing Columns.
This is my code:
DataTable dt = new DataTable();
dt.Columns.Add("SourcePath", typeof(string));
dt.Columns.Add("SourceFile", typeof(string));
dt.Columns.Add("OutputPath", typeof(string));
dt.Columns.Add("OutputFile", typeof(string));
dt.Columns.Add("Status", typeof(string));
dt.Columns.Add("Message", typeof(string));
dt.Rows.Add(new object[] { "Test", "Test", "Test", "Test", "Test", "Belgium" });
dt.Rows.Add(new object[] { "Test", "Test", "Test", "Test", "Test", "France" });
dt.Rows.Add(new object[] { "Test", "Test", "Test", "Test", "Test", "Germany" });
dt.Rows.Add(new object[] { "Test", "Test", "Test", "Test", "Test", "Spain" });
dt.Rows.Add(new object[] { "Test", "Test", "Test", "Test", "Test", "Switzerland" });
dt.Rows.Add(new object[] { "Test", "Test", "Test", "Test", "Test", "United Kingdom" });
datagridview.DataSource = dt;
Am I doing something wrong here?
Solution 1:[1]
When the DataSource of a DataGridView is set or changed, the default behavior is to generate Columns using the names of the Columns in the data source, since the AutoGenerateColumns property defaults to true
.
See the Remarks section in the Docs about this property.
If DataGridViewColumns have been added in the Designer, the DataPropertyName property of each Column is compared to the name of the Columns in the data source.
When a match is found, an existing DataGridViewColumns is bound to the matching Column in the data source. Otherwise a new Column is added to the grid.
In other words - as this appears to be what the OP describes - when Columns are added in the Designer without a DataPropertyName
and a new DataSource is set, the Columns in the DataSource are added to those already present in the grid.
If the DataPropertyName
of a DataGridViewColumn is not specified in the Designer, you can set it in code before assigning the DataSource
property. For example:
var dt = new DataTable();
dt.Columns.Add(new DataColumn("SourcePath", typeof(string)) { Caption = "Source Path"});
// [...]
dt.Rows.Add(new[] { "Test", "Test", "Test", "Test", "Test", "Belgium" });
// [...]
// Assuming DGV Columns are added in the Designer without a DataPropertyName set
datagridview.Columns[0].DataPropertyName = dt.Columns[0].ColumnName
datagridview.Columns[0].HeaderText = dt.Columns[0].Caption
// Or in different ways, e.g.:
datagridview.Columns["SourcePath"].DataPropertyName = dt.Columns[0].ColumnName
datagridview.Columns["SourcePath"].HeaderText = dt.Columns[0].Caption
// [...]
datagridview.DataSource = dt;
Solution 2:[2]
The syntax Might be causing it.
DataTable dt = new DataTable();
dt.Columns.Add("SourcePath", typeof(string));
dt.Columns.Add("SourceFile", typeof(string));
dt.Columns.Add("OutputPath", typeof(string));
dt.Columns.Add("OutputFile", typeof(string));
dt.Columns.Add("Status", typeof(string));
dt.Columns.Add("Message", typeof(string));
var Row = dt.NewRow(); //Create a new row
Row["SourcePath"] = "Test";//Add Column value via Indexer
//Add Rest of Column Values
dt.Rows.Add(Row); //Append to Table Rows
Solution 3:[3]
If you don't want the grid to generate columns it finds In the DataSource and seems to be missing from itself, set
datagridview.AutoGenerateColumns = false;
before you set the DataSource
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Caius Jard |