Resolved DataGridView Specific Row set to CheckBox

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
129
Programming Experience
10+
I have need of setting a particular row to be a CheckBox. I have found many examples of setting the entire Column. I need that same idea, except for a row. Any ideas?
 
Solution
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.
C#:
var rowIndex = 0;

for (var columnIndex = 0; columnIndex < myDataGridView.ColumnCount; columnIndex++)
{
    myDataGridView[columnIndex, rowIndex] = new DataGridViewCheckBoxCell();
}
Obviously you can set the rowIndex to whatever you need.
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.
C#:
var rowIndex = 0;

for (var columnIndex = 0; columnIndex < myDataGridView.ColumnCount; columnIndex++)
{
    myDataGridView[columnIndex, rowIndex] = new DataGridViewCheckBoxCell();
}
Obviously you can set the rowIndex to whatever you need.
 
Solution
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.
C#:
var rowIndex = 0;

for (var columnIndex = 0; columnIndex < myDataGridView.ColumnCount; columnIndex++)
{
    myDataGridView[columnIndex, rowIndex] = new DataGridViewCheckBoxCell();
}
Obviously you can set the 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...
 
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.
 
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 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.

1608618625530.png


Is there anything else you need in order to help me?
 
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.

 
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.

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?
 
I don't think that the 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.

I haven't tested but I suspect that the issue here is that the check box cell expects a value of either 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.
 
I don't think that the 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.

I haven't tested but I suspect that the issue here is that the check box cell expects a value of either 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:

C#:
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";

Now the only question left is how do I detect a button click on my button?
 
Back
Top Bottom