datagridview checkbox

M

mrstrong

Gday,

I have a datagridview that I am creating the columns programatically
which all seems to work fine. I have a couple of dropdown boxes, so I
have set the editMode= EditOnEnter.

Now my checkboxes dont seem to work, and the datagridview throws a
dataerror when I try to leave the cell after trying to check/uncheck
the checkbox?!

Anyone know why this might be happening?

Thanks,

Peter
 
G

Guest

Hi Peter,

I tried in vain to see what you are experiencing but could not reproduce any
errors. Here is what I have so far:

Dim cb As New Windows.Forms.DataGridViewCheckBoxCell
Dim cs As New DataGridViewCellStyle
cs.NullValue = False

cb.Style = cs
Me.DataGridView1.Columns.Add(New DataGridViewColumn(cb))
'Me.DataGridView1.Rows.Add(5)

Please try this or alter this to show me the error that you are referencing.

Thanks,
Rajneesh
 
M

mrstrong

I tried in vain to see what you are experiencing but could not reproduce any
errors. Here is what I have so far:

Dim cb As New Windows.Forms.DataGridViewCheckBoxCell
Dim cs As New DataGridViewCellStyle
cs.NullValue = False

cb.Style = cs
Me.DataGridView1.Columns.Add(New DataGridViewColumn(cb))
'Me.DataGridView1.Rows.Add(5)


Gday Rajneesh,

Ok, Im a fair way down the development track so it's hard to isolate
precisely what's going on...

Basically the datagridview that I have has been working correctly for
quite a while (datagridview contains both dropdowns and checkboxes)
and these were working fine...

However, in trying to address the problem with the dropdowns inside
the datagridview where a user has to click on them twice for them to
drop down - my aim is to change this so that users only have to click
once for them to drop down. One way that seemed to work was to change
the EditMode of the datagridview to be 'EditOnEnter' (instead of the
default 'EditOnKeyStrokeOrF2'.)

This allowed the dropdowns to work with a single click, however it
stopped the checkboxes from working and resulted in dataerrors in the
gridview...

Here is my code for where I add the combo and checkbox columns:

-------------------------------------------------------
// set datasource
dgv.DataSource = myDataSet;
dgv.DataMember = <myTable>;

// create dataview for our combo box
DataView myDataView = new
DataView(myDataSet.<lookup_table>);
myDataView.RowFilter = "<lookup_table.field>=<filter
criteria>";

// create a new combo box item
DataGridViewComboBoxColumn dgvCbo = new
DataGridViewComboBoxColumn();

dgvCbo.DataPropertyName = <dpname>;
dgvCbo.DataSource = myDataView;
dgvCbo.DisplayMember = <myDataView.field>;
dgvCbo.ValueMember = <myDataView.field>;
dgvCbo.Name = "myCombo";
dgvCbo.HeaderText = "Combo Box Field";
dgvCbo.Width = 80;

dgv.Columns.Add(dgvCbo);

// set the checkbox column

DataGridViewCheckBoxColumn dgvChk = new
DataGridViewCheckBoxColumn();
dgvChk.Name = "myCheckBox";
dgvChk.HeaderText = "Check Box Field";
dgvChk.Width = 60;

dgv.Columns.Add(dgvChk);

-------------------------------------------------------

As I said, it was all working fine until I changed the datagridview's
editMode to be 'EditOnEnter'.

Any ideas/suggestions would be welcome. Maybe I am too far down the
dev track and just need to scrap this datagridview and start again.

Regards,

Peter
 
G

Guest

Try to use the following code:
Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
Dim cb As New Windows.Forms.DataGridViewCheckBoxCell
Dim cs As New DataGridViewCellStyle
cs.NullValue = False

cb.Style = cs
Me.DataGridView1.Columns.Add(New DataGridViewColumn(cb))
Me.DataGridView1.Rows.Add(5)

Does the problem go away. if you remove the EditMode from EditOnEnter?

Looking at your code, I do think it is any different from what I am doing,
so breaking it down should help identify the cause!
-Rajneesh
 
M

mrstrong

Try to use the following code:
Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
Dim cb As New Windows.Forms.DataGridViewCheckBoxCell
Dim cs As New DataGridViewCellStyle
cs.NullValue = False

cb.Style = cs
Me.DataGridView1.Columns.Add(New DataGridViewColumn(cb))
Me.DataGridView1.Rows.Add(5)

Does the problem go away. if you remove the EditMode from EditOnEnter?

Looking at your code, I do think it is any different from what I am doing,
so breaking it down should help identify the cause!
-Rajneesh

Ok - on further investigation - it seems to be that when the
datagridview has EditMode=EditOnEnter, and when I go to add a new row
by clicking on one of the checkboxes, for some reason it is setting
the 'EditedFormattedValue' of the checkbox to be the first value of
the comboxbox that is the first column in the datagridview. This is
what's causing the dataerrors - the checkbox's 'EditedFormattedValue'
is getting set to a string value, where it can only take a boolean.

Try setting up a datagridview with a combobox as the first column, and
a checkbox as the second column, and see if you get the same thing.

Thanks,

Peter
 
G

Guest

Hi Peter,

I have tried the following code, to add a combo-box column and then a
check-box column and changing their values (tried various combinations) but
it works perfect every time.

Here is the code:
----------------------------CODE STARTS-------------------------------
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
Dim cb As New Windows.Forms.DataGridViewCheckBoxCell
Dim cs As New DataGridViewCellStyle
cs.NullValue = False

cb.Style = cs
Me.DataGridView1.Columns.Add(New DataGridViewColumn(cb))
Me.DataGridView1.Rows.Add(5)
End Sub

'Add a Combobox (Do it before adding CheckBox Column)
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim cb As New Windows.Forms.DataGridViewComboBoxColumn
cb.Items.Add("one")
cb.Items.Add("two")
cb.Items.Add("three")

Me.DataGridView1.Columns.Add(cb)
End Sub

'Clear everything
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Me.DataGridView1.Columns.Clear()
End Sub
End Class
----------------------------CODE ENDS-------------------------------

Please try using the above code and give me exact steps to reproduce the
issue.

Regards,
-Rajneesh
 
M

mrstrong

Hi Peter,

I have tried the following code, to add a combo-box column and then a
check-box column and changing their values (tried various combinations) but
it works perfect every time.

Here is the code:
----------------------------CODE STARTS-------------------------------
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
Dim cb As New Windows.Forms.DataGridViewCheckBoxCell
Dim cs As New DataGridViewCellStyle
cs.NullValue = False

cb.Style = cs
Me.DataGridView1.Columns.Add(New DataGridViewColumn(cb))
Me.DataGridView1.Rows.Add(5)
End Sub

'Add a Combobox (Do it before adding CheckBox Column)
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim cb As New Windows.Forms.DataGridViewComboBoxColumn
cb.Items.Add("one")
cb.Items.Add("two")
cb.Items.Add("three")

Me.DataGridView1.Columns.Add(cb)
End Sub

'Clear everything
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Me.DataGridView1.Columns.Clear()
End Sub
End Class
----------------------------CODE ENDS-------------------------------

Please try using the above code and give me exact steps to reproduce the
issue.

Regards,
-Rajneesh

Gday Rajneesh,

Thanks for your reply - I actually tried a blank project, added a
datagridview with two columns - one a combo box and the other a
checkbox and it worked perfectly aswell.

I do have a method attached to the datagridview for
defaultValuesNeeded, so mabye there is actually something screwy going
on here...

Anyway, I have resolved to use the cellenter method of the
datagridview and call 'SendKeys.Send("{F4}");' if the cell is a combo
and this gives the desired action that the combo will dropdown when it
is first clicked (this is with editmode set back to its default value
of 'EditOnKeystrokeOrF2'). The checkboxes now behave as expected.

I originally had issues with sendkeys, as if you clicked on a
dropdown in to add a new row, and held down the mouse it would keep on
adding new rows. So I added this code which seemed to stop this
happening:

dgv.AllowUserToAddRows = false;
dgv.AllowUserToAddRows = true;

More than one way to skin a cat...

When I get time I'll go back and try to see if the defaultValuesNeeded
method might be causing any issues (currently the datatable it is
bound to has some required fields).

Thanks for your assistance.

Regards,

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top