<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>I Learnt Today... &#187; Select Multiple Rows</title>
	<atom:link href="http://www.ilearnttoday.com/category/information-technology/web-development/aspnet/gridview/select-multiple-rows/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ilearnttoday.com</link>
	<description>This is where I share what I learn day by day...</description>
	<lastBuildDate>Fri, 18 May 2012 13:36:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>C# ASP.NET &#8211; GridView : How to Use Checkbox in Gridview to Select Multiple Rows</title>
		<link>http://www.ilearnttoday.com/c-aspnet-gridview-select-multiple-rows-in-a-grid-view-using-check-boxes</link>
		<comments>http://www.ilearnttoday.com/c-aspnet-gridview-select-multiple-rows-in-a-grid-view-using-check-boxes#comments</comments>
		<pubDate>Tue, 06 Jan 2009 00:38:39 +0000</pubDate>
		<dc:creator>Menol</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#.NET]]></category>
		<category><![CDATA[GridView]]></category>
		<category><![CDATA[Menol]]></category>
		<category><![CDATA[Select Multiple Rows]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[add a template field to a gridview]]></category>
		<category><![CDATA[Add button]]></category>
		<category><![CDATA[Add New Column...]]></category>
		<category><![CDATA[Adding a Checkbox to the Template Column]]></category>
		<category><![CDATA[Adding a Template Column]]></category>
		<category><![CDATA[Adding a Template Column to gridview]]></category>
		<category><![CDATA[ArrayList]]></category>
		<category><![CDATA[Available fields]]></category>
		<category><![CDATA[BoundField]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Checkbox Tasks]]></category>
		<category><![CDATA[checked property of checkbox]]></category>
		<category><![CDATA[columns]]></category>
		<category><![CDATA[Control Control.FindControl(string controlID)]]></category>
		<category><![CDATA[Control Control.FindControl(string id)]]></category>
		<category><![CDATA[data table]]></category>
		<category><![CDATA[DataBinder]]></category>
		<category><![CDATA[DataBinder.Eval]]></category>
		<category><![CDATA[datagrid]]></category>
		<category><![CDATA[datatable]]></category>
		<category><![CDATA[dummy data]]></category>
		<category><![CDATA[Edit Columns...]]></category>
		<category><![CDATA[Edit Templates]]></category>
		<category><![CDATA[Eval]]></category>
		<category><![CDATA[explicit cast]]></category>
		<category><![CDATA[explicitly cast]]></category>
		<category><![CDATA[FindControl(controlID)]]></category>
		<category><![CDATA[for each loop]]></category>
		<category><![CDATA[get value of templatefield from datagrid]]></category>
		<category><![CDATA[get value of templatefield from gridview]]></category>
		<category><![CDATA[Grid View]]></category>
		<category><![CDATA[Grid View Row]]></category>
		<category><![CDATA[GridView Tasks]]></category>
		<category><![CDATA[GridViewRow]]></category>
		<category><![CDATA[GridViewRow class]]></category>
		<category><![CDATA[header text]]></category>
		<category><![CDATA[How to Databind a Template column]]></category>
		<category><![CDATA[How to Databind a Template Field]]></category>
		<category><![CDATA[How to Use Checkbox in Gridview to Select Multiple Rows]]></category>
		<category><![CDATA[Insert Check boxes in grid view]]></category>
		<category><![CDATA[Insert Checkboxes in datagrid]]></category>
		<category><![CDATA[Insert Checkboxes in gridview]]></category>
		<category><![CDATA[Item Template]]></category>
		<category><![CDATA[Populating the GridView]]></category>
		<category><![CDATA[postback]]></category>
		<category><![CDATA[refresh grid]]></category>
		<category><![CDATA[Retrieving the Status of Checkboxes]]></category>
		<category><![CDATA[Select Multiple Rows in a Grid View]]></category>
		<category><![CDATA[Select Multiple Rows in a Grid View Using Check Boxes]]></category>
		<category><![CDATA[Select Multiple Rows in a GridView]]></category>
		<category><![CDATA[Select Multiple Rows in a GridView Using CheckBoxes]]></category>
		<category><![CDATA[template column]]></category>
		<category><![CDATA[template field]]></category>
		<category><![CDATA[templatefield]]></category>
		<category><![CDATA[TemplateField properties]]></category>

		<guid isPermaLink="false">http://ilearnttoday.wordpress.com/?p=221</guid>
		<description><![CDATA[2009-01-06 I wanted to select multiple rows of the grid view using check boxes to select each row. In this<a href="http://www.ilearnttoday.com/c-aspnet-gridview-select-multiple-rows-in-a-grid-view-using-check-boxes" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #c0c0c0;">2009-01-06</span><br />
<script type="text/javascript">// <![CDATA[
    google_ad_client = "ca-pub-5478118713208336"; /* LeadIn */ google_ad_slot = "2638602320"; google_ad_width = 468; google_ad_height = 15;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[</p>
<p>// ]]&gt;</script></p>
<p>I wanted to select multiple rows of the grid view using check boxes to select each row.</p>
<p>In this post I will discus how and what I did to achieve this goal&#8230;</p>
<p>I used a template column which holds a check box to select or de-select a row in the grid view.</p>
<p><strong>Adding a Template Column:</strong></p>
<p>There are several simple ways to add a template column/field to your grid view:</p>
<ol type="1">
<li class="MsoNormal">Select your grid view and then click on the small task button (the small square button located to the top right corner of the grid view control) and select &#8220;Add New Column&#8230;&#8221; from the &#8220;GridView Tasks&#8221; box.
<ol type="1">
<li class="MsoNormal">Use &#8220;TemplateField&#8221; as the field type</li>
<li class="MsoNormal">Provide a header text</li>
<li class="MsoNormal">click ok</li>
</ol>
</li>
<li class="MsoNormal">Or&#8230; Select &#8220;Edit Columns&#8230;&#8221; from the &#8220;GridView Tasks&#8221; box and
<ol type="1">
<li class="MsoNormal">Select &#8220;TemplateField&#8221; from the &#8220;Available fields: &#8221; section</li>
<li class="MsoNormal">Click &#8220;Add&#8221; button</li>
<li class="MsoNormal">Provide a header text in the &#8220;TemplateField properties: &#8221; section</li>
<li class="MsoNormal">click ok</li>
</ol>
</li>
<li class="MsoNormal">Or&#8230; Select &#8220;Columns&#8221; from the grid view properties and follow the steps described in the earlier method (2<sup>nd </sup>one)</li>
</ol>
<p><strong>Adding a Checkbox to the Template Column:</strong></p>
<ol type="1">
<li class="MsoNormal">Select &#8220;Edit Templates&#8221; from the &#8220;GridView Tasks&#8221; box. Then it will show you the inside of the template.</li>
<li class="MsoNormal">Select &#8220;Item Template&#8221; from the &#8220;Display: &#8221; drop down list.</li>
<li class="MsoNormal">Drag and drop a check box in the item template area.</li>
<li class="MsoNormal">Give an id as &#8220;chkid&#8221; for the check box &#8211; You can use a different name if you want but make sure you don&#8217;t confuse when I use this name in later parts of this article.</li>
<li class="MsoNormal">Click &#8220;End Template Editing&#8221; from the &#8220;GridView Tasks&#8221; box.</li>
</ol>
<p>If you have completed the above steps correctly, you should see the expected checkbox column added in to the grid view.</p>
<p><img class="alignnone size-full wp-image-223" title="Design View" src="http://ilearnttoday.files.wordpress.com/2009/01/11.jpg" alt="Design View" width="378" height="265" /><br />
As you can see in the image above, in my example I use a Name column along with the check box column to make this clearer.</p>
<p><strong>How to Databind the Template Field:</strong></p>
<p>I use a datatable to populate the gridview so we have to databind our two columns.</p>
<p>The second column name is a &#8220;BoundField&#8221; so to databind it simply specify the name of the data table column you want to bind to this column in &#8220;DataField&#8221; property under &#8220;Data&#8221; property set in the fields dialog box you get when adding the second column (use one of the ways I described above to add a column).</p>
<p>Databinding the template field is a bit tricky, but not hard.</p>
<ol type="1">
<li class="MsoNormal">Click &#8220;Edit Templates&#8221; from the &#8220;GridView Tasks&#8221; box so that you will see the check box.</li>
<li class="MsoNormal">Click on the small button with a triangle in the upper right corner of the check box which is similar to the one we clicked on the grid view to get the &#8220;Checkbox Tasks&#8221; box and then click on the &#8220;Edit Databindings&#8230;&#8221; link inside that box.</li>
</ol>
<p>You can use this dialog box to bind any property of the control to a data source column</p>
<p>In this example we will bind the &#8220;Checked&#8221; property of our check box to a Boolean column called &#8220;Selected&#8221; of the data source.</p>
<p>For that:</p>
<ul type="disc">
<li class="MsoNormal">Select &#8220;Checked&#8221; from the Bindable Properties section and enter following in the Custom Binding section:
<ul type="circle">
<li class="MsoNormal">DataBinder.Eval(Container, &#8220;DataItem.<span style="color: #ff0000;"><span style="color: red;">Selected</span></span>&#8220;)</li>
</ul>
</li>
<li class="MsoNormal">Note: the word &#8220;Selected&#8221; (in red color) refers to the &#8220;Selected&#8221; field of the data source.</li>
</ul>
<p><strong>Populating the GridView:</strong></p>
<p>As I described earlier I create a simple data table in the page load, populate it with some dummy data and then bind the grid view with it.</p>
<p>Note: I have used several methods to add data to a table to illustrate those to you.</p>
<p>The Code:</p>
<pre class="brush: csharp; title: ; notranslate">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn(&quot;Selected&quot;, System.Type.GetType(&quot;System.Boolean&quot;)));
dt.Columns.Add(new DataColumn(&quot;Name&quot;, System.Type.GetType(&quot;System.String&quot;)));

DataRow dr = dt.NewRow();
dr.ItemArray = new object[] { false, &quot;Cassian&quot; };
dt.Rows.Add(dr);
dt.Rows.Add(new object[] { false, &quot;Menol&quot; });
dt.Rows.Add(new object[] { false, &quot;Razeek&quot; });

this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
</pre>
<p>There are several things that I&#8217;d like to bring to your notice:</p>
<ul type="disc">
<li class="MsoNormal">I have checked for postback to make sure this data population is not occurred in general post backs. The reason is the framework refreshes when the grid view is re-bound.</li>
<li class="MsoNormal">So if we don&#8217;t do this check, whenever we click on a check box, a postback occurs, the grid gets refreshed, the check boxes get cleared, we cannot keep track of what check boxes are selected&#8230;!</li>
<li class="MsoNormal">As illustrate in my code, you can either first populate a datarow and then add it to the datatable</li>
<li class="MsoNormal">Or you can directly add the items using only one line of code.</li>
</ul>
<p><strong>Retrieving the Status of Checkboxes:</strong></p>
<p>Now we have completed the interface and the user now can select several rows of the grid view using the appropriate check box.</p>
<p>The next step is to capture the rows that the user has selected.</p>
<p>In my example, I have used a button, once clicked; it will display the list of names of the selected people.</p>
<p>I will introduce the code first and then explain it to you.</p>
<pre class="brush: csharp; title: ; notranslate">
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList names = new ArrayList();
foreach (GridViewRow gvr in this.GridView1.Rows)
{
if (((CheckBox)gvr.FindControl(&quot;chkid&quot;)).Checked == true)
{
names.Add(gvr.Cells[1].Text);
}
}

this.Label1.Text = string.Empty;
foreach (object itm in names)
{
this.Label1.Text += &quot; &quot; + itm.ToString();
}
}
</pre>
<p>Note following:</p>
<ul type="disc">
<li class="MsoNormal">The code iterates through each row of the grid view using a for each loop, takes the Name field from the selected rows and then adds those names into a arraylist</li>
<li class="MsoNormal">The GridViewRow refers to a single row in a grid view</li>
<li class="MsoNormal"><strong><em>Control Control.FindControl(string controlID)</em></strong> &#8211; GridViewRow class provides this method to find a specific control within a grid view row. This method becomes very handy in situations like this where we cannot directly access the control from the code because those are generated at run time</li>
<li class="MsoNormal">FindControl returns a control so you have to explicitly cast the returned control to the type of it</li>
</ul>
<p><strong> Screen Shot:</strong></p>
<p><strong><a href="http://ilearnttoday.files.wordpress.com/2009/01/21.jpg"><img class="alignnone size-full wp-image-234" title="21" src="http://ilearnttoday.files.wordpress.com/2009/01/21.jpg" alt="21" width="367" height="213" /></a><br />
</strong></p>
<p><strong>Conclusion:</strong></p>
<p>We can use a template field with a checkbox to allow user to select multiple rows using checkboxes associated to each row. When the button is clicked, checkbox of each row is checked for its status and the row is selected if the user has chosen it.</p>
<p><script type="text/javascript">// <![CDATA[
   google_ad_client = "pub-5478118713208336"; /* BelowPost_BlendIn */ google_ad_slot = "0823141458"; google_ad_width = 468; google_ad_height = 60;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[</p>
<p>// ]]&gt;</script></p>
<p style="text-align: justify;"><span style="color: #008000;">Was this post helpful to you? How can I improve? &#8211; </span><span style="color: #008000;">Your comment is highly appreciated!</span></p>
<p style="text-align: justify;"><span style="color: #c0c0c0;">Cassian Menol Razeek</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ilearnttoday.com/c-aspnet-gridview-select-multiple-rows-in-a-grid-view-using-check-boxes/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

