At least one of the DataGridView control's columns has no cell template
You need to give the type of the column. If you want a textBox column, you'd have to use new DataGridViewTextBoxColumn()
. Let's say you want a textBox and a combobox, you'd have the followings:
also, you are calling the first column name over and over again.
DataGridViewColumn miejsce = new DataGridViewTextBoxColumn();
miejsce.DataPropertyName = "Miejsce";
miejsce.HeaderText = "Miejsce";
miejsce.Name = "miejsceCollumn";
Change the following for the given name.
DataGridViewColumn imie = new DataGridViewComboBoxColumn();
imie.DataPropertyName = "Imie";
imie.HeaderText = "Imię";
imie.Name = "imieCollumn"
If DataGridView columns are created programmaticaly, then the AutoGenerateColumns property of the DataGridView have to be set on false.
My code is an example of a DataGridView able to display any DataTable and it is working perfectly in this form:
// Prepare the DataViewGrid
dataGridView1.Columns.Clear();
// Add each column to the grid according to the data table structure
for (int i = 0; i < dataTable.Columns.Count; i++)
{
DataGridViewColumn dataGridViewColumn = new DataGridViewColumn();
DataGridViewCell dataGridViewCell = new DataGridViewTextBoxCell();
dataGridViewColumn.DataPropertyName = dataTable.Columns[i].ColumnName;
dataGridViewColumn.HeaderText = dataTable.Columns[i].ColumnName;
dataGridViewColumn.CellTemplate = dataGridViewCell;
dataGridViewColumn.Name = dataTable.Columns[i].ColumnName;
dataGridView1.Columns.Add(dataGridViewColumn);
}
// Set the DataSource for the binding
bindingSource1.DataSource = dataTable;
// Prevent unwanted columns autogeneration
dataGridView1.AutoGenerateColumns = false;
// Provide the binding to the DataGridView
dataGridView1.DataSource = bindingSource1;
I just needed to set the CellTemplate
of the column to resolve the issue.
DataGridViewColumn c = new DataGridViewColumn();
c.Name = "ColumnName";
c.HeaderText = "DisplayText";
c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
c.CellTemplate = new DataGridViewTextBoxCell();
dataGridView1.Columns.Add(c);