473,407 Members | 2,326 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,407 software developers and data experts.

Dynamically set the checkbox column in a gridview

Hi,
I have a GridView with a checkbox column in it called FromInsight,
however this
is not bound to the dataset, its value is based on another column from the
dataset called sourceid
For each row FromInsight = true if (SourceId 0). I try to loop around the
gridview and add the value of the checkbox but the column seems to have no
checked value, please can you help with this.
Thanx in advance
Robert

Mar 6 '08 #1
3 8995
I try to loop around the
gridview and add the value of the checkbox but the column seems to have no
checked value
I ran into the same problem where I had a CheckBox column in a DataGrid not
bound to the DataSet. Upon submitting the form none of the check box values
were retained. It turns out that including a check box in each row of the
GridView the way I was trying to do it wouldn't work. As explained in
Checking All CheckBoxes in a GridView
(http://aspnet.4guysfromrolla.com/articles/052406-1.aspx) by Scott Mitchell
and Extending the GridView Control
(http://msdn.microsoft.com/msdnmag/is...5/CuttingEdge/) by Dino
Esposito, the CheckBoxes either need to be bound to the same data source as
the GridView or the CheckBoxes need to be part of a TemplateField column.

I tried the TemplateField approach but I couldn't get it to work. The check
boxes were inexplicably dropped from the GridView after the form was
submitted. There is probably some simple mistake that I was making.

Next I tried using the sample code from the Esposito article which derives a
new GridView control and adds the necessary infrastructure to get the
checkboxes working. It adds extra functionality that I don't need but it did
work. I could iterate through the rows of the GridView and identify those
check boxes which had been checked.

Mar 7 '08 #2
The simple way to do this is to incorporate that into your SQL so your data
really reflects what your grid is showing. You will have to change the
SelectCommand of your sqlDataSource to include an imaginary field. so if your
select statement is "select sourceID, a, b, c from myTable where a=2" you
will change that and add the imaginary field like so "select sourceID, a, b,
c, Case When [sourceID] 0 Then 1 Else 0 End as d from myTable where a=2".

This guarantees the return of an additional field called d the value of
which is set to 1 if sourceID is 0 and d would be 0 if sourceID isn't 0.
You will bind to that field normally when you're displaying your grid and
then you can read it when it is submitted. How you use that in the submit is
entirely up to you. Just keep in mind that there is no field called d in
myTable so it won't go anywhere if you don't handle it in the code.

A clean way to show you that SQL statement is the following

SELECT [SourceID]
, [a]
, [b]
, [c]
, CASE
WHEN [SourceID] 0 THEN 1
ELSE 0
END AS [d]
FROM myTable
WHERE [a] = 2

Hope this helps.

--
Mohamad Elarabi
MCP, MCTS, MCPD.
"Robert Smith" wrote:
Hi,
I have a GridView with a checkbox column in it called FromInsight,
however this
is not bound to the dataset, its value is based on another column from the
dataset called sourceid
For each row FromInsight = true if (SourceId 0). I try to loop around the
gridview and add the value of the checkbox but the column seems to have no
checked value, please can you help with this.
Thanx in advance
Robert
Mar 8 '08 #3
Forgot to add that [d] in your case is really [FromInsight] and you should
have your checkbox column bound to it and it should all jig.

--
Mohamad Elarabi
MCP, MCTS, MCPD.
"Mohamad Elarabi [MCPD]" wrote:
The simple way to do this is to incorporate that into your SQL so your data
really reflects what your grid is showing. You will have to change the
SelectCommand of your sqlDataSource to include an imaginary field. so if your
select statement is "select sourceID, a, b, c from myTable where a=2" you
will change that and add the imaginary field like so "select sourceID, a, b,
c, Case When [sourceID] 0 Then 1 Else 0 End as d from myTable where a=2".

This guarantees the return of an additional field called d the value of
which is set to 1 if sourceID is 0 and d would be 0 if sourceID isn't 0.
You will bind to that field normally when you're displaying your grid and
then you can read it when it is submitted. How you use that in the submit is
entirely up to you. Just keep in mind that there is no field called d in
myTable so it won't go anywhere if you don't handle it in the code.

A clean way to show you that SQL statement is the following

SELECT [SourceID]
, [a]
, [b]
, [c]
, CASE
WHEN [SourceID] 0 THEN 1
ELSE 0
END AS [d]
FROM myTable
WHERE [a] = 2

Hope this helps.

--
Mohamad Elarabi
MCP, MCTS, MCPD.
"Robert Smith" wrote:
Hi,
I have a GridView with a checkbox column in it called FromInsight,
however this
is not bound to the dataset, its value is based on another column from the
dataset called sourceid
For each row FromInsight = true if (SourceId 0). I try to loop around the
gridview and add the value of the checkbox but the column seems to have no
checked value, please can you help with this.
Thanx in advance
Robert
Mar 8 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
3
by: keithb | last post by:
My code dynamically adds template fields to a GridView control. Everything seems to work OK, except when updating, because I haven't found a way to reference the dynamically added textboxes....
1
by: Bill44077 | last post by:
I am dynamically adding a checkbox in a gridview and I find there are several things that I cannot figure out how to address. 1. The width of the checkbox column is very wide. I've tried adding...
4
by: bryan | last post by:
Hi all, I am working on a .net 2.0 web app. On page load I display a gridview with a drop down list (Employees), when the user selects from the drop down list, the grid view is filled with...
0
by: New2ASP | last post by:
Thanks everyone in advance for your help. I am fairly new to web development but an experienced window-based developer. Here's the structure of my Gridview Column 1 : Checkbox with SelectAll...
1
by: Ben | last post by:
Hi, I'm designing a c# page and trying to add a checkbox column to a GridView, I've added a Template Row (as described at: http://aspnet.4guysfromrolla.com/articles/052406-1.aspx) and in the...
3
by: =?Utf-8?B?UGxhdGVyaW90?= | last post by:
I have link buttons in a Gridview that, depending on the value in another column (Not the Key column) will need to be disabled. For example, (Column2 has link buttons) Column1 ...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
1
by: janetb | last post by:
I have a gridview with an update capabilities - a textbox column (roomName), a dropdownlist(orgID), a dropdownlist(roomTypeID),a checkbox column (dialOut), a checkbox column (dialIn). When I try to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.