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

27 comments:

Anonymous said...

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

Anonymous said...

Stickpin the duo of hundreds of satisfied clients and self-control tranquillity up on us move you beyond smashing to guaranteed gorgeous.

[url=http://minichill.com/FreeMC/]free condom samples[/url]

Yoke the team of hundreds of satisfied clients and let us take you beyond appealing to guaranteed gorgeous.
We specialize in forging of arhitectural textured beaker, melted magnifying lens, callousness and native to doorlights, railings, frameless doors, decorative windows, dividers, skylights, magnifying sun-glasses floors, countertops, sprinkling units, back-painted opera-glasses, displays, lorgnette signs, barometer fireplaces, boloney, glassware sinks, mirrors, waterfalls, fountains, accessories, etc.
[url=http://www.hammerpackaging.com/pressure_labels.asp]pressure sensitive labels[/url]

[url=http://minichill.com/FreeMC/]free samples australia[/url]
[url=http://minichill.com/FreeMC/]freebies free samples[/url]
Join the set of hundreds of satisfied clients and charter out us get you beyond beautiful to guaranteed gorgeous.
We specialize in composition of arhitectural textured barometer, melted lorgnette, purpose and apparent doorlights, railings, frameless doors, decorative windows, dividers, skylights, magnifying beaker floors, countertops, sprinkling units, back-painted field-glasses, displays, lorgnette signs, lens fireplaces, shit, window-pane sinks, mirrors, waterfalls, fountains, accessories, etc.
[url=http://www.powerpublicrelations.com/]dallas pr firms[/url]
[url=http://www.powerpublicrelations.com/]dallas pr firm[/url]
[url=http://www.minichill.com]relaxation beverage[/url]
[url=http://www.minichill.com]relaxation[/url]
[url=http://www.minichill.com/ChillRecipes.html]alcohol[/url]
[url=http://www.minichill.com]relaxation shot[/url]
[url=http://www.minichill.com]relaxation shots[/url]
[url=http://minichill.com/FreeMC/]free product samples canada[/url]
[url=http://minichill.com/FreeMC/]mcat sample questions[/url]

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...

blu-ray software for sony vaio http://buysoftwareonline.co.uk/it/product-14725/SpamSieve-2-6-Mac blender cad software [url=http://buysoftwareonline.co.uk/product-35015/Quantumwise-Atomistix-Toolkit-11-2-x64]seniority software[/url] windows key generating software
[url=http://buysoftwareonline.co.uk/product-13902/Lock-Folder-XP-3-5]Lock Folder XP 3.5 - Software Store[/url] bma users group software bcr
[url=http://buysoftwareonline.co.uk/category-2/Business?page=14][img]http://buyoem.co.uk/image/6.gif[/img][/url]

Anonymous said...

whos in software http://buysoftwareonline.co.uk/es/product-31547/LittleSnapper-1-5-MAC computer journal software [url=http://buysoftwareonline.co.uk/it/category-100-109/Antivirus-e-Securiti?page=2]sobot software[/url] account business company manager sales software
[url=http://buysoftwareonline.co.uk/it/category-200-208/Herramientas-de-la-Oficina]Herramientas de la Oficina - Cheap Legal OEM Software, Software Sale, Download OEM[/url] adr software
[url=http://buysoftwareonline.co.uk/it/product-34607/Adobe-Creative-Suite-5-5-Production-Premium][img]http://buyoem.co.uk/image/1.gif[/img][/url]

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...

Ahaa, its good dialogue about this piece of writing here at this weblog,
I have read all that, so at this time me also commenting here.


Here is my web site xxx-fuck.net

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...

Very descriptive post, I liked that bit. Will there be a part 2?



Feel free to surf to my site ... donne in webcam

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