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)

Thanks in advance

What actual field type is the field in the column you want to change?

What have you tried?

Did you bother clicking the little arrow on the dataview and editing the columns?

I want to change the textbox column's cells to a checkbox cell at runtime.
I have tried with :
dataGridView1.rows[index].cells["Col1"]=new DataGridViewCheckBoxCell()


this type of code works but with System Format Exception

Then you needed to mention you wished to do so at runtime. We cant read your mind.

Usual way would be to change just the cell, however, you could remove the column that is wrong, and readd it as the right type, rather than try and convert it.

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

Then as I said you need to change the cell (which has many examples available via google), and you can do it depending on the contents of other cells.. thats up to you

I understood what you mean exactly, but the datagridview doesn't offer this possibility because cells have to be from a unique type for a given column

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....

You can though if you do your own as the ms article shows implementing a non standard dataview control , as you can have it display dependant on settings.

I will read the article thoroughly, because this intriges me very much. Thank you LizR.

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

But thats the behavior they want..

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("Name","Dan")
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)
Else
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_
DataGridView1.DataError

'Write what you want, or put nothing if you want to ignore the data errors. :-)

End Sub

*PLEASE* allow us to block LizR and all her totally useless and rude comments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.