424,303 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

How to Add *AND* Process Checkbox Column in a DataGridView?

P: n/a
Hello All,

I'd like to find out the best way to add a cb column to a dgv and process efficiently. I see at least two ways of doing it.

-------------------------------
1) Add a cb to the dgv, iterate thru the dgv and update the bound fields if the cb has been checked. Then do the update and accept changes. How do I
access the cb and its checked status? How to iterate thru dgv?

Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

For Each dgv1????row in dgv1.rows
if dgv1????row("RecSel").Checked then
...do some update process....
end if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()

-------------------------------
2) Create an alias boolean field in the SELECT statement when creating the datatable. Then test the checkbox and update as applicable. How do I
change the cell style to checkbox (of "RecSel")? How to test if checked?
ENQrySel = "SELECT True as RecSel, ENDate, ... "
"FROM ... WHERE ... ORDER BY ..."
daEN.Fill(dtEN)
dgv1.DataSource = dtEN

DIM drEN as DataRow
For Each drEN in dtEN.Rows
If drEN("RecSel") ???Selected??? then
...do some update process....
End If
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
Which way is best/doable? Other suggestions?

Thanks,

Hexman

Dec 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I would choose #1. I assume you're binding the column to
a boolean field.

Here's some info from Brian Noyes's book on databinding.
I'm assuming you're not using ThreeState mode (allows
True, False, and Indeterminate).

The column with checkboxes in it contains cells of type
DataGridViewCheckBoxCell.

A value of Null or False will leave the checkbox unchecked.
A value of true will check the box.

The cell's value property can be set explicitly through
programmatic code that accesses the cell in the Cells
collection of the row, or it can be set through data binding.

In another part of his code, he is going through the grid
and checking the value of the checkbox for true/false
like this:

For Each row As DataGridViewRow In m_Grid.Rows
Dim include As DataGridViewCheckBoxCell = _
TryCast(row.Cells("IncludeRate"), DataGridViewCheckBoxCell)
If include IsNot Nothing AndAlso include.Value IsNot Nothing _
AndAlso (DirectCast(include.Value, Boolean)) = True Then
'Do something with the checkbox value; it's checked
End If
Next

Hope this helps.
Robin S.
----------------------------------------------

"Hexman" <He****@binary.comwrote in message
news:n0********************************@4ax.com...
Hello All,

I'd like to find out the best way to add a cb column to a dgv and
process efficiently. I see at least two ways of doing it.

-------------------------------
1) Add a cb to the dgv, iterate thru the dgv and update the bound
fields if the cb has been checked. Then do the update and accept
changes. How do I
access the cb and its checked status? How to iterate thru dgv?

Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

For Each dgv1????row in dgv1.rows
if dgv1????row("RecSel").Checked then
...do some update process....
end if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()

-------------------------------
2) Create an alias boolean field in the SELECT statement when creating
the datatable. Then test the checkbox and update as applicable. How
do I
change the cell style to checkbox (of "RecSel")? How to test if
checked?
ENQrySel = "SELECT True as RecSel, ENDate, ... "
"FROM ... WHERE ... ORDER BY ..."
daEN.Fill(dtEN)
dgv1.DataSource = dtEN

DIM drEN as DataRow
For Each drEN in dtEN.Rows
If drEN("RecSel") ???Selected??? then
...do some update process....
End If
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
Which way is best/doable? Other suggestions?

Thanks,

Hexman

Dec 10 '06 #2

P: n/a
Yes Robin, it did help - I got the app running how I wanted it to work. I just have a question about the code you presented. I don't understand what
the "Dim include as...." statement is doing. Could you explain.

My code that works now looks like this....and I didn't have to do any typecasting, etc. Am I going to regret the way I've done it later? Thanks also
for the reference to Brian Noyes' book. I've got a lot of reading to do.

Hexman

---------------------------------------------------------------------
'Add checkbox to dgv
Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

'Process each row in dgv
For Each row As DataGridViewRow In dgv1.Rows
If row.Cells("RecSel").Value = True Then
...do some update process....
End if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
---------------------------------------------------------------------

On Sun, 10 Dec 2006 11:31:43 -0800, "RobinS" <Ro****@NoSpam.yah.nonewrote:
>I would choose #1. I assume you're binding the column to
a boolean field.

Here's some info from Brian Noyes's book on databinding.
I'm assuming you're not using ThreeState mode (allows
True, False, and Indeterminate).

The column with checkboxes in it contains cells of type
DataGridViewCheckBoxCell.

A value of Null or False will leave the checkbox unchecked.
A value of true will check the box.

The cell's value property can be set explicitly through
programmatic code that accesses the cell in the Cells
collection of the row, or it can be set through data binding.

In another part of his code, he is going through the grid
and checking the value of the checkbox for true/false
like this:

For Each row As DataGridViewRow In m_Grid.Rows
Dim include As DataGridViewCheckBoxCell = _
TryCast(row.Cells("IncludeRate"), DataGridViewCheckBoxCell)
If include IsNot Nothing AndAlso include.Value IsNot Nothing _
AndAlso (DirectCast(include.Value, Boolean)) = True Then
'Do something with the checkbox value; it's checked
End If
Next

Hope this helps.
Robin S.
----------------------------------------------

"Hexman" <He****@binary.comwrote in message
news:n0********************************@4ax.com.. .
>Hello All,

I'd like to find out the best way to add a cb column to a dgv and
process efficiently. I see at least two ways of doing it.

-------------------------------
1) Add a cb to the dgv, iterate thru the dgv and update the bound
fields if the cb has been checked. Then do the update and accept
changes. How do I
access the cb and its checked status? How to iterate thru dgv?

Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

For Each dgv1????row in dgv1.rows
if dgv1????row("RecSel").Checked then
...do some update process....
end if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()

-------------------------------
2) Create an alias boolean field in the SELECT statement when creating
the datatable. Then test the checkbox and update as applicable. How
do I
change the cell style to checkbox (of "RecSel")? How to test if
checked?
ENQrySel = "SELECT True as RecSel, ENDate, ... "
"FROM ... WHERE ... ORDER BY ..."
daEN.Fill(dtEN)
dgv1.DataSource = dtEN

DIM drEN as DataRow
For Each drEN in dtEN.Rows
If drEN("RecSel") ???Selected??? then
...do some update process....
End If
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
Which way is best/doable? Other suggestions?

Thanks,

Hexman
Dec 10 '06 #3

P: n/a
This section is just a way to iterate through the rows
in the DataGridView and check the value of a checkbox.

Dim include... casts the IncludeRate cell on that row
as a DataGridViewCheckBoxCell. TryCast returns Nothing
if it couldn't cast it as that type. So the next line
checks to see if it's not nothing AndAlso the value
is filled in AndAlso the value is a boolean and it's
true, and if all of those conditions are true, then the
checkbox is checked.
>>For Each row As DataGridViewRow In m_Grid.Rows
Dim include As DataGridViewCheckBoxCell = _
TryCast(row.Cells("IncludeRate"), DataGridViewCheckBoxCell)
If include IsNot Nothing AndAlso include.Value IsNot Nothing _
AndAlso (DirectCast(include.Value, Boolean)) = True Then
'Do something with the checkbox value; it's checked
End If
Next
I think the way you have it is fine. My guess is that
Mr. Noyes was trying to be really type-safe. Theoretically,
he should know if the column "IncludeRate" is a checkbox or
not, but I guess technically someone else could change it
in the DataGridView definitions. (Like if they changed
it to a 1-character field that held Y/N.) If that happened,
his code wouldn't break.

His book is really good; he had a good DataGridView example
where he shows several of the different column types. He
does a lot of parent/child stuff, too, and talks about
making custom controls bindable.

One thing to note: The examples in the book are in C#.
However, the downloadable code is available in both C#
and VB. The bright side is that after working through it,
I can now read C# a lot more proficiently. The other side
is that it made the book a tad more challenging.

Good luck.
Robin S.
------------------------------------

"Hexman" <He****@binary.comwrote in message
news:pe********************************@4ax.com...
Yes Robin, it did help - I got the app running how I wanted it to
work. I just have a question about the code you presented. I don't
understand what
the "Dim include as...." statement is doing. Could you explain.

My code that works now looks like this....and I didn't have to do any
typecasting, etc. Am I going to regret the way I've done it later?
Thanks also
for the reference to Brian Noyes' book. I've got a lot of reading to
do.

Hexman

---------------------------------------------------------------------
'Add checkbox to dgv
Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

'Process each row in dgv
For Each row As DataGridViewRow In dgv1.Rows
If row.Cells("RecSel").Value = True Then
...do some update process....
End if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
---------------------------------------------------------------------

On Sun, 10 Dec 2006 11:31:43 -0800, "RobinS" <Ro****@NoSpam.yah.none>
wrote:
>>I would choose #1. I assume you're binding the column to
a boolean field.

Here's some info from Brian Noyes's book on databinding.
I'm assuming you're not using ThreeState mode (allows
True, False, and Indeterminate).

The column with checkboxes in it contains cells of type
DataGridViewCheckBoxCell.

A value of Null or False will leave the checkbox unchecked.
A value of true will check the box.

The cell's value property can be set explicitly through
programmatic code that accesses the cell in the Cells
collection of the row, or it can be set through data binding.

In another part of his code, he is going through the grid
and checking the value of the checkbox for true/false
like this:

For Each row As DataGridViewRow In m_Grid.Rows
Dim include As DataGridViewCheckBoxCell = _
TryCast(row.Cells("IncludeRate"), DataGridViewCheckBoxCell)
If include IsNot Nothing AndAlso include.Value IsNot Nothing _
AndAlso (DirectCast(include.Value, Boolean)) = True Then
'Do something with the checkbox value; it's checked
End If
Next

Hope this helps.
Robin S.
----------------------------------------------

"Hexman" <He****@binary.comwrote in message
news:n0********************************@4ax.com. ..
>>Hello All,

I'd like to find out the best way to add a cb column to a dgv and
process efficiently. I see at least two ways of doing it.

-------------------------------
1) Add a cb to the dgv, iterate thru the dgv and update the bound
fields if the cb has been checked. Then do the update and accept
changes. How do I
access the cb and its checked status? How to iterate thru dgv?

Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

For Each dgv1????row in dgv1.rows
if dgv1????row("RecSel").Checked then
...do some update process....
end if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()

-------------------------------
2) Create an alias boolean field in the SELECT statement when
creating
the datatable. Then test the checkbox and update as applicable.
How
do I
change the cell style to checkbox (of "RecSel")? How to test if
checked?
ENQrySel = "SELECT True as RecSel, ENDate, ... "
"FROM ... WHERE ... ORDER BY ..."
daEN.Fill(dtEN)
dgv1.DataSource = dtEN

DIM drEN as DataRow
For Each drEN in dtEN.Rows
If drEN("RecSel") ???Selected??? then
...do some update process....
End If
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
Which way is best/doable? Other suggestions?

Thanks,

Hexman

Dec 10 '06 #4

P: n/a
Thanks,

Hexman

On Sun, 10 Dec 2006 15:30:21 -0800, "RobinS" <Ro****@NoSpam.yah.nonewrote:
>This section is just a way to iterate through the rows
in the DataGridView and check the value of a checkbox.

Dim include... casts the IncludeRate cell on that row
as a DataGridViewCheckBoxCell. TryCast returns Nothing
if it couldn't cast it as that type. So the next line
checks to see if it's not nothing AndAlso the value
is filled in AndAlso the value is a boolean and it's
true, and if all of those conditions are true, then the
checkbox is checked.
>>>For Each row As DataGridViewRow In m_Grid.Rows
Dim include As DataGridViewCheckBoxCell = _
TryCast(row.Cells("IncludeRate"), DataGridViewCheckBoxCell)
If include IsNot Nothing AndAlso include.Value IsNot Nothing _
AndAlso (DirectCast(include.Value, Boolean)) = True Then
'Do something with the checkbox value; it's checked
End If
Next

I think the way you have it is fine. My guess is that
Mr. Noyes was trying to be really type-safe. Theoretically,
he should know if the column "IncludeRate" is a checkbox or
not, but I guess technically someone else could change it
in the DataGridView definitions. (Like if they changed
it to a 1-character field that held Y/N.) If that happened,
his code wouldn't break.

His book is really good; he had a good DataGridView example
where he shows several of the different column types. He
does a lot of parent/child stuff, too, and talks about
making custom controls bindable.

One thing to note: The examples in the book are in C#.
However, the downloadable code is available in both C#
and VB. The bright side is that after working through it,
I can now read C# a lot more proficiently. The other side
is that it made the book a tad more challenging.

Good luck.
Robin S.
------------------------------------

"Hexman" <He****@binary.comwrote in message
news:pe********************************@4ax.com.. .
>Yes Robin, it did help - I got the app running how I wanted it to
work. I just have a question about the code you presented. I don't
understand what
the "Dim include as...." statement is doing. Could you explain.

My code that works now looks like this....and I didn't have to do any
typecasting, etc. Am I going to regret the way I've done it later?
Thanks also
for the reference to Brian Noyes' book. I've got a lot of reading to
do.

Hexman

---------------------------------------------------------------------
'Add checkbox to dgv
Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

'Process each row in dgv
For Each row As DataGridViewRow In dgv1.Rows
If row.Cells("RecSel").Value = True Then
...do some update process....
End if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
---------------------------------------------------------------------

On Sun, 10 Dec 2006 11:31:43 -0800, "RobinS" <Ro****@NoSpam.yah.none>
wrote:
>>>I would choose #1. I assume you're binding the column to
a boolean field.

Here's some info from Brian Noyes's book on databinding.
I'm assuming you're not using ThreeState mode (allows
True, False, and Indeterminate).

The column with checkboxes in it contains cells of type
DataGridViewCheckBoxCell.

A value of Null or False will leave the checkbox unchecked.
A value of true will check the box.

The cell's value property can be set explicitly through
programmatic code that accesses the cell in the Cells
collection of the row, or it can be set through data binding.

In another part of his code, he is going through the grid
and checking the value of the checkbox for true/false
like this:

For Each row As DataGridViewRow In m_Grid.Rows
Dim include As DataGridViewCheckBoxCell = _
TryCast(row.Cells("IncludeRate"), DataGridViewCheckBoxCell)
If include IsNot Nothing AndAlso include.Value IsNot Nothing _
AndAlso (DirectCast(include.Value, Boolean)) = True Then
'Do something with the checkbox value; it's checked
End If
Next

Hope this helps.
Robin S.
----------------------------------------------

"Hexman" <He****@binary.comwrote in message
news:n0********************************@4ax.com ...
Hello All,

I'd like to find out the best way to add a cb column to a dgv and
process efficiently. I see at least two ways of doing it.

-------------------------------
1) Add a cb to the dgv, iterate thru the dgv and update the bound
fields if the cb has been checked. Then do the update and accept
changes. How do I
access the cb and its checked status? How to iterate thru dgv?

Dim cbSelCol As New DataGridViewCheckBoxColumn

dgv1.Columns.Insert(0, cbSelCol)
With cbSelCol
.HeaderText = "Select"
.Name = "RecSel"
.DisplayIndex = 0
.Frozen = True
End With

For Each dgv1????row in dgv1.rows
if dgv1????row("RecSel").Checked then
...do some update process....
end if
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()

-------------------------------
2) Create an alias boolean field in the SELECT statement when
creating
the datatable. Then test the checkbox and update as applicable.
How
do I
change the cell style to checkbox (of "RecSel")? How to test if
checked?
ENQrySel = "SELECT True as RecSel, ENDate, ... "
"FROM ... WHERE ... ORDER BY ..."
daEN.Fill(dtEN)
dgv1.DataSource = dtEN

DIM drEN as DataRow
For Each drEN in dtEN.Rows
If drEN("RecSel") ???Selected??? then
...do some update process....
End If
Try
daEN.Update(dtEN)
Catch ex As Exception
'An exception occurred
End Try
Next
dtEN.AcceptChanges()
Which way is best/doable? Other suggestions?

Thanks,

Hexman

Dec 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.