I am using a datagridview in my windows application in VS-2005.
How can I convert some cells in a particular textbox column in my datagridview
to checkbox cell types.
Please provide some suggestions with code (if possible)
I admire your suggestion but
my requirement is quite the other way
Actually I am not supposed to change or replace the entire column's celltype property
but only few cells for that column
which means my column would have hetrogeneous cells (few textbox cells and few checkbox cells)
note:all to be done at runtime
I must agree with Jugortha. Datagridview is (as I see it) intended for use within a database context : displaying rows from a databasetable. So you can have a column with dates and a column with strings, but not intermixed. I know we had the same problem years ago( I wrote in Modula-2, sort of Pascal) We made our own gizmo....
Its logic, you can put in the cell anything you like, not all cells are necessarily the same you could make them different on content. Sure its the same class - but no one says that class has to do the same thing for all the values it supports.
Ok, I think that it is a little bit complicated because you will use your datagridview to display data from a data base generally and you know data tabe in data base is compsed by colmuns and columns could hold data only for one type so the problem persists even if someone inherits from a data grid control and create is own model that suports mixed types in one column
I think that mecanisms are going to gether, I explain: all data bases formats are composed by data tables and datatables are composed by data column, each column holds the same type of recored and not a heterogenous set of data, therefore datagridview is designed to be suitable to this paradigm
This is an old thread, but I think I can solve it by saying that everyone is correct and giving the solution to the problem. Yes, you cannot change the contents of a single cell. What you end up with is a format error because the checkbox will only accept CheckState.Checked or CheckState.Unchecked. To get around this, you can handle the dataerror. Here is the solution for the datagrid. I'll put 2 columns, with 2 rows and change the second row of the second column to a checkbox. After the second row is added, I'll change the cell and then run a conditional statement to decide the state of the checkbox.
dim isDanGreat as Boolean = True
DataGridView1.Rows.Add("Is Great?", Nothing)
DataGridView1.Rows(1).Cells(1) = new DataGridViewCheckboxCell
If isDanGreat = True Then
DataGridView1.Rows(1).SetValues("Is Great?", CheckState.Checked)
DataGridView1.Rows(1).SetValues("Is Great?", CheckState.Unchecked)
'now, go handle the data error
Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e_
As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles_
'Write what you want, or put nothing if you want to ignore the data errors. :-)