var rowIndex = 0;
for (var columnIndex = 0; columnIndex < myDataGridView.ColumnCount; columnIndex++)
{
myDataGridView[columnIndex, rowIndex] = new DataGridViewCheckBoxCell();
}
rowIndex
to whatever you need.var rowIndex = 0;
for (var columnIndex = 0; columnIndex < myDataGridView.ColumnCount; columnIndex++)
{
myDataGridView[columnIndex, rowIndex] = new DataGridViewCheckBoxCell();
}
rowIndex
to whatever you need.That kind of worked. I get the error "System.Format.Exception: Formatted value of the cell has a wrong type". It did display the CheckBox, however...There is no way to set the type of the cells in a row. The column controls the template for the cells it contains and if you want to use a different cell type to that template in a particular row then you have to create an individual cell. You need to do that for each column, e.g.
Obviously you can set theC#:var rowIndex = 0; for (var columnIndex = 0; columnIndex < myDataGridView.ColumnCount; columnIndex++) { myDataGridView[columnIndex, rowIndex] = new DataGridViewCheckBoxCell(); }
rowIndex
to whatever you need.
I am attempting to create a Query Builder similar to what exists in the MS Access database. It is fairly simple in that there is basically only one table the user can build queries for. So I have a DataGridView that will display as many Fields as the suer has selected. In the screen shot, I have just started working on the "Date" field. The DataGridView displays each selected Field in a column and the columns contain 3 different types of cells. Row "Sort" contains a ComboBox, row "Show" contains a CheckBox and row "Criteria" contains a Button. I get the error on the screen shot after each row is added and thereafter every time the mouse passes over the cell.The error message is telling you exactly what the issue is. If you don't understand what to do about it then you're going to have to provide a FULL and CLEAR explanation of the problem. What is the data? Where did it come from? Etc.
I could use a PropertyGrid for the first two rows, but I need to have the user load a CriteriaBuilder form to build up the Criteria for the query. My users are not too knowledgeable on SQL Queries. I have never tried to launch a form from within a PropertyGrid. Is that even possible?Ah! The main issue above is that you are using the wrong UI for that lower pane. You shouldn't be using a DataGridView to shows what are essentially properties. You should be using a PropertyGrid.
PropertyGrid
is the answer here. If you check out the Query Builder is VS, the UI is pretty much as shown above. It allows all parameters to be viewed and configured simultaneously.true
or false
while the value is null
by default. If you were to simply set the Value
of the cell to false
by default then I suspect that it would work.You are correct. I had just done that in the code below:I don't think that thePropertyGrid
is the answer here. If you check out the Query Builder is VS, the UI is pretty much as shown above. It allows all parameters to be viewed and configured simultaneously.
I haven't tested but I suspect that the issue here is that the check box cell expects a value of eithertrue
orfalse
while the value isnull
by default. If you were to simply set theValue
of the cell tofalse
by default then I suspect that it would work.
dgvQuery.Rows.Add();
DataGridViewComboBoxCell cbCell = new DataGridViewComboBoxCell();
cbCell.Items.Add("Ascending");
cbCell.Items.Add("Descending");
cbCell.Items.Add("[not sorted]");
dgvQuery[e.Index, 0] = cbCell;
dgvQuery[e.Index, 0].Value = "[not sorted]";
dgvQuery.Rows[0].HeaderCell.Value = "Sort";
dgvQuery.Rows.Add();
DataGridViewCheckBoxCell chkCell = new DataGridViewCheckBoxCell();
dgvQuery[e.Index, 1] = chkCell;
dgvQuery[e.Index, 1].Value = false;
dgvQuery.Rows[1].HeaderCell.Value = "Show";
dgvQuery.Rows.Add();
DataGridViewButtonCell btnCell = new DataGridViewButtonCell();
btnCell.Value = "Edit";
dgvQuery[e.Index, 2] = btnCell;
dgvQuery.Rows[2].HeaderCell.Value = "Criteria";
To respond to user button clicks, handle the DataGridView.CellClick or DataGridView.CellContentClick event.
Thanks. That worked perfectly. Thanks for all the help everyone!DataGridViewButtonCell Class (System.Windows.Forms)
Displays a button-like user interface (UI) for use in a DataGridView control.docs.microsoft.com