Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Dynamically set the checkbox column in a gridview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Smith - 06 Mar 2008 15:49 GMT
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
Bruce - 07 Mar 2008 01:13 GMT
> 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.
Mohamad Elarabi [MCPD] - 08 Mar 2008 04:40 GMT
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.

Signature

Mohamad Elarabi
MCP, MCTS, MCPD.

> Hi,
>    I have a GridView with a checkbox column in it called FromInsight,
[quoted text clipped - 6 lines]
> Thanx in advance
> Robert
Mohamad Elarabi [MCPD] - 08 Mar 2008 04:46 GMT
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.

Signature

Mohamad Elarabi
MCP, MCTS, 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
[quoted text clipped - 35 lines]
> > Thanx in advance
> > Robert

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.