Dynamically set the checkbox column in a gridview

R

Robert Smith

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
 
B

Bruce

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/issues/06/05/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.
 
M

Mohamad Elarabi [MCPD]

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]
,
, [c]
, CASE
WHEN [SourceID] > 0 THEN 1
ELSE 0
END AS [d]
FROM myTable
WHERE [a] = 2

Hope this helps.
 
M

Mohamad Elarabi [MCPD]

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 said:
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]
,
, [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 said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top