473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
4 21255
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Veeneman | last post by:
How can I set a bound DataGridView control to use a dataset table's column captions, instead of column names? I'm working with a DataGridView control, which I have bound to a table in a dataset....
6
by: Bill | last post by:
I have an unbound DataGridView with a DataGridViewCheckBoxColumn as column 2(configured in the designer). How would I programatically set the checkbox state in, say, row 3 column 2? I need to do...
8
by: Mike | last post by:
Hi all, I have a DataGridViewCheckBoxColumn as one of my columns in a DataGridView. I want this checkbox to only be checked, but not unchecked - it's used by the user to audit that they have...
2
by: Kiran_Juikar | last post by:
How to add the header checkbox in the datagridview checkbox column? Is there any property available to add a header checkboxor one need to write the custom logic for this?
1
by: ursridhu | last post by:
hi guys, i am new to vc++, How to add a checkbox column to a datagrid. is it possible to add a checkbox in datagrid?? if yes please send me how we can do it. Thanx in advance its ur'sridhu
2
by: Bill Nguyen | last post by:
I'm learning DatagridView. How do I define a checkbox column type? Thanks Bill
2
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and...
1
by: trak77 | last post by:
Hi All, I'm relatively new to vb .net and am probably making a simple mistake and would appreciate any help. I have a tab control with 4 tabs. Each tab has a DataGridView and I'm populating...
9
by: obrienkev | last post by:
Hi, I have a Checkbox column in the DataGridView. I am unable to change the state of the checkbox. Why is this??? What do I need to set in Properties? Using Visual Studio 2005
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.