Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Thursday, October 25, 2007

Adding a Checkbox Column to a GridView and checking if it is checked.

In my project I bind a DataTable to a Gridview. I have two fields in my DataTable: (bool) isChecked and (string) Value. Below I bind the data to those fields in the first two columns. The last Column I used a TemplateField and ItemTemplate to create a CheckBox row that is editable.



<asp:GridView ID="GridView1" runat="server"
OnDataBound="GridView1_DataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
AutoGenerateColumns="False" >
<Columns>
<asp:CheckBoxField DataField="isChecked" HeaderText="Select" />
<asp:BoundField DataField="Value" HeaderText="Value" />
<asp:TemplateField HeaderText="Custom Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Once the user edits the Custom Select checkboxes and pushes the button (btnSumbit), I read which fields are checked. If checked, I read from a Hashtable the value associated with the key.


protected void btnSubmit_Click(object sender, EventArgs e)
{
string sQueryString = "http://foo?";
// load hashtable with key values
Hashtable vhtTags = (Hashtable)Cache["htTags"];
bool isChecked;
// parse through to see what values are selected in gridview
foreach (GridViewRow vRow in GridView1.Rows)
{
isChecked = ((CheckBox)vRow.FindControl("chkSelect")).Checked;
if (isChecked)
{
string skey = vRow.Cells[1].Text.Trim();
sQueryString += vhtTags[skey].ToString();
}
}
lblResult.Text = sQueryString;
}
}

Notes: Since the first check column is only for demonstration and the last column is not bound because the default values are false. For the sake of brevity, error checking is left out.
Reference: link

22 comments:

Anonymous said...

Thanks for the code but the last "{" is useless.

Anonymous said...

Howdy! I could have sworn I've been to this website before but after reading through some of the post I realized it's new to me.
Anyhow, I'm definitely delighted I found it and I'll
be bookmarking and checking back often!
My site : good house renovation in winter garden

Anonymous said...

Thanks , I have just been searching for information approximately this subject for ages
and yours is the greatest I've discovered so far. However, what in regards to the bottom line? Are you positive in regards to the source?
Look into my weblog how to seo website

Anonymous said...

Hi there to every single one, it's genuinely a pleasant for me to go to see this web page, it includes priceless Information.
My web page :: search engine optimization india

Anonymous said...

in our time free ebook http://audiobookscollection.co.uk/Confucianism/p190110/ light on yoga iyengar ebook [url=http://audiobookscollection.co.uk/de/Dream-Worlds-Production-Design-for-Animation/p225469/]ajax patterns and best practices ebook[/url] illegally thin ebook

Anonymous said...

I will immediately snatch your rss feed as I
can not find your email subscription link or e-newsletter service.
Do you have any? Please allow me recognize so that I may subscribe.
Thanks.

Visit my website ... trademark attorney

Anonymous said...

My coder is trying to persuade me to move to .net from PHP.
I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on numerous websites
for about a year and am worried about switching to another platform.
I have heard great things about blogengine.net.
Is there a way I can import all my wordpress content into it?
Any kind of help would be really appreciated!

Visit my site: Shabby Chic Moebel Karlsruhe

Anonymous said...

With havin so much content do you ever run into any issues of plagorism
or copyright infringement? My website has a lot of completely
unique content I've either written myself or outsourced but it looks like a lot of it is popping it up all over the web without my permission. Do you know any solutions to help prevent content from being stolen? I'd truly appreciate it.


my page :: How to get your ex back

Anonymous said...

Informative article, exactly what I needed.

Also visit my web blog cheapest california auto insurance

Anonymous said...

you're truly a just right webmaster. The site loading pace is incredible. It kind of feels that you're doing any unique trick.
Moreover, The contents are masterwork. you've performed a magnificent task in this topic!

Visit my blog: www.7sultans.eu

Anonymous said...

Awesome blog! Is your theme custom made or did you download it from
somewhere? A theme like yours with a few simple adjustements would really make my blog shine.
Please let me know where you got your theme.
Thanks a lot

Feel free to visit my website: d'nest

Anonymous said...

Simply desire to say your article is as astounding. The clearness in your post is simply excellent and i can assume you're an expert on this subject. Well with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please carry on the gratifying work.

my weblog :: amsterdam escorts

Anonymous said...

WOW just what I was searching for. Came here by searching for registry
fix

my homepage ... escort amsterdam

Anonymous said...

Every weekend i used to go to see this web site, as i wish for enjoyment,
as this this website conations truly fastidious funny information too.


My site; Californian love

Anonymous said...

You really make it appear so easy together with your presentation however I find this
topic to be really something that I think I'd never understand. It sort of feels too complicated and very large for me. I am looking forward to your next submit, I will attempt to get the hang of it!

Feel free to visit my homepage; Work From Home Opportunities

Anonymous said...

free dating in lansing runaway bride sound track dads against daughters dating window decal
who's dating who john krasinski http://freeinternetdating.info/friendship/friendship-upholstery-company-pa dating canada for muslims
dating agencies in uk dating age in ny hermaphrodites dating

Anonymous said...

I really love your website.. Excellent colors & theme.
Did you build this web site yourself? Please reply back as I'm attempting to create my own website and would love to find out where you got this from or just what the theme is called. Kudos!

my blog; xxx video fix

Anonymous said...

daily telegraph on line dating http://loveepicentre.com/testimonials/ roatan dating
dating african girls [url=http://loveepicentre.com/map/]dating ski jumping olympic romances[/url] who's dating who famous
louisiana dating lines [url=http://loveepicentre.com/faq/]married dating meet personals[/url] dating quizzes ee [url=http://loveepicentre.com/user/hecklementen/]hecklementen[/url] dating cougars

Anonymous said...

bookmarked!!, I really like your site!

Visit my web page www.jnmassage.info

Anonymous said...

It's very simple to find out any matter on web as compared to textbooks, as I found this piece of writing at this web page.

Also visit my web site :: cfnm fever tube

Anonymous said...

Wow that wass unusual. I just wrkte an very long commment
buut after I clicked submit my comment didn't show up.
Grrrr... well I'm not writing all that over again. Anyways, just wantd to say superb blog!


my website: tile that looks like wood

Anonymous said...

Good day I am so grateful I found your weblog, I really found you
by mistake, while I was looking on Google for something else, Anyways I am here now and would just like to say many thanks
for a tremendous post and a all round thrilling blog (I also
love the theme/design), I don’t have time to read it all at
the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do
keep up the fantastic b.

Here is my web page: cartier love bracelet