<?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>Three2Tango &#187; GridView Tips</title>
	<atom:link href="http://www.three2tango.com/category/techcorner/aspnet/gridview-tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.three2tango.com</link>
	<description>Points To Ponder : The Latest news from the TechWorld,Automobiles,CellPhones and lots of useful Code Snippets</description>
	<lastBuildDate>Fri, 27 Jan 2012 09:03:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Bind Images from Database in a GridView</title>
		<link>http://www.three2tango.com/techcorner/how-to-bind-images-from-database-in-a-gridview.html/</link>
		<comments>http://www.three2tango.com/techcorner/how-to-bind-images-from-database-in-a-gridview.html/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 14:22:28 +0000</pubDate>
		<dc:creator>Vivek</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[GridView Tips]]></category>
		<category><![CDATA[Tech Corner]]></category>
		<category><![CDATA[Code# Code Snippets]]></category>
		<category><![CDATA[How to Bind Images from Database in a GridView]]></category>

		<guid isPermaLink="false">http://www.three2tango.com/?p=2733</guid>
		<description><![CDATA[Binding Images to a Gridview has been explained in this article. For this , first start a new application with ASP.NET and drop a GridView control onto the aspx page.Now add a new template Column to the GridView.In addition,if you want to display anything else, add bound fields or template fields according to your needs.In here, i have used a bound fied to display the image Id and the template field to display the Image.Now In the template field,add an Image control as the item template.For this just click on ...]]></description>
			<content:encoded><![CDATA[<p>Binding Images to a Gridview has been explained in this article. For this , first start a new application with ASP.NET and drop a GridView control onto the aspx page.Now add a new template Column to the GridView.In addition,if you want to display anything else, add bound fields or template fields according to your needs.In here, i have used a bound fied to display the image Id and the template field to display the Image.Now In the template field,add an Image control as the item template.For this just click on Edit Item Template and add an Image as Itemtemplate. Dont forget to uncheck the &#8216;AutoGenerateColumns&#8217; checkbox.The complete code for the GridView is as follows.</p>
<pre language="c-sharp" name="code">&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"&gt;
&lt;Columns&gt;
&lt;asp:BoundField HeaderText="ProductID" /&gt;
&lt;asp:TemplateField HeaderText="Image"&gt;
&lt;ItemTemplate&gt;
&lt;asp:Image ID="Image1" runat="server" /&gt;
&lt;/ItemTemplate&gt;
&lt;/asp:TemplateField&gt;
&lt;/Columns&gt;
&lt;/asp:GridView&gt;</pre>
<p>Now to display images. Here in the database,only the file names is there and hence if the images are in some directory, you have to somehow pre pend it with the filename.Here i am using the appsettings in web.config to provide the path of the images directory.</p>
<pre language="c-sharp" name="code">&lt;appSettings&gt;
&lt;add key="IMG" value="http://your full path"/&gt;
&lt;/appSettings&gt;</pre>
<p>Now to the C# code for populating the GridView. Here is the entire method i created for binding this GridView.</p>
<pre language="c-sharp" name="code">private void bindGrid()
{
string strconn= ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strconn);
con.Open();
string cmdText = "Select productId,Filename from Images"
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.CommandType = CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

for (int i = 0; i &lt; dt.Rows.Count; i++)
{
GridView1.Rows[i].Cells[0].Text = dt.Rows[i]["ProductId"].ToString();
GridView1.Rows[i].Cells[2].Text = dt.Rows[i]["Filename"].ToString();
Image PImage = (Image)GridView1.Rows[i].FindControl("Image1");
PImage.ImageUrl = ConfigurationManager.AppSettings["IMG"].ToString() + dt.Rows[i]["FileName"].ToString();
}
con.Close();
}</pre>
<p>Though this method will be slow if you want to populate huge amount of data, it is quite effective. Now here in the first line, i am taking the connection string from the  web.config.The next few steps are self explanatory as you can see.You may be wondering why i have binded the Grid and after that i am populating it again. That is because, when the for loop runs, if the GridView is not binded, the Index was out of range index error will be shown.<br />
Now in the for loop, the first cell, which contains the bound field, text is set to the ImageId. The second line in the loop, we are finding the image control we had added before as the template field in the GridView.&#8221;Image1&#8243; is the ID of the Image Control.Now all that is remaining is setting the ImageUrl property of the found image control. Before that we prepend the FileName with the directory path.</p>
<p>Hope this helps.As i said, this method may be slow if you have many images to be populated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.three2tango.com/techcorner/how-to-bind-images-from-database-in-a-gridview.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dropdownlist inside a Gridview</title>
		<link>http://www.three2tango.com/techcorner/dropdownlist-inside-a-gridview.html/</link>
		<comments>http://www.three2tango.com/techcorner/dropdownlist-inside-a-gridview.html/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 07:05:15 +0000</pubDate>
		<dc:creator>LG</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[GridView Tips]]></category>
		<category><![CDATA[Tech Corner]]></category>
		<category><![CDATA[edit]]></category>
		<category><![CDATA[gridviews]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.three2tango.com/?p=427</guid>
		<description><![CDATA[How to use a dropdownlist in a gridview??
I will try explain this with best of my knowledge, the use of a dropdownlist inside comes when the case of edit update in a gridview comes. Consider an Eg:
A gridview that displays a “name” and a “Country”, and an option to edit the Country of a person. This is done from a dropdownlist.

Initial Requirements:
1)
Tables(Ms sqlserver2008 is the back end database used in here)
Table name:- country_lloyd
CountryId 	       		int
CountryName        		varchar(50)
Table name:- ...]]></description>
			<content:encoded><![CDATA[<p><strong>How to use a dropdownlist in a gridview??</strong><br />
I will try explain this with best of my knowledge, the use of a dropdownlist inside comes when the case of edit update in a gridview comes. Consider an Eg:<br />
A gridview that displays a “name” and a “Country”, and an option to edit the Country of a person. This is done from a dropdownlist.<br />
<img class="alignleft size-full wp-image-485" title="ScreenShot" src="http://www.three2tango.com/wp-content/uploads/2009/04/shhot1.bmp" alt="ScreenShot" /><br />
<strong>Initial Requirements:</strong><br />
<strong>1)</strong><br />
Tables(Ms sqlserver2008 is the back end database used in here)</p>
<p><strong>Table name</strong>:- country_lloyd<br />
CountryId 	       		int<br />
CountryName        		varchar(50)</p>
<p><strong>Table name</strong>:- Student<br />
Id	       		int<br />
Name	       	varchar(50)<br />
CountryId	       	int</p>
<p><strong>2)</strong><br />
•	Add a gridview (GridView1) to the page “Default.aspx”.<br />
•	It is filled with two fields, name and country name (for this an inner join is used),set the “Id” of the Student as datakey.<a href="http://www.three2tango.com/archives/89">How to set a dataKey??</a><br />
•	Remove “AutoGenerate Columns” of gridview.<br />
•	Add two bound fields convert it to template field.<br />
•	Add a Command Field “Edit Update Cancel”.</p>
<p><strong>How to bind the values from the table to a “Template column” in the GridView???</strong></p>
<p><strong>1.</strong> Firstly “Edit templates” of the gridview (GridView1).Now in the “ItemTemplate” of Column [0]-Name drag and drop a label(name it “Label1”).And in the “ItemTemplate”  of Column[1]-Country drag and drop another label(name it “lblCountry”).<br />
<strong>2.</strong> Now in the “Edit Item Template” of Column [1]-Country drag and drop a dropdownlist (name it “ddl_CountryList”).<br />
<strong>3.</strong> Now “End Template Editing”.<br />
<strong>4.</strong> Now go to the html section of your page(“Default.aspx”),there  you can find the html code of the gridview and the 3 controls we dragged in to the gridview, Now just check my code (<strong>“Default.aspx”</strong>) to understand what you need to do.This is how a value field is bound to a template column(Text=&#8217;&lt;%# Bind(&#8220;Name&#8221;) %&gt;&#8217;)</p>
<blockquote><p>&#8220;Never   loose   faith  if  you find this a  bit confusing, go on you will understand it.&#8221;</p></blockquote>
<p><strong>“Default.aspx”</strong></p>
<blockquote><p>&lt;asp:GridView ID=&#8221;GridView1&#8243; runat=&#8221;server&#8221; AutoGenerateColumns=&#8221;False&#8221;<br />
DataKeyNames=&#8221;Id&#8221; onrowcancelingedit=&#8221;GridView1_RowCancelingEdit&#8221;<br />
onrowediting=&#8221;GridView1_RowEditing&#8221; onrowupdating=&#8221;GridView1_RowUpdating&#8221;&gt;<br />
&lt;Columns&gt;<br />
&lt;asp:TemplateField HeaderText=&#8221;Name&#8221;&gt;<br />
&lt;ItemTemplate&gt;<br />
&lt;asp:Label ID=&#8221;Label1&#8243; runat=&#8221;server&#8221; Text=&#8217;&lt;%# Bind(&#8220;Name&#8221;) %&gt;&#8217;&gt;&lt;/asp:Label&gt;<br />
&lt;/ItemTemplate&gt;<br />
&lt;/asp:TemplateField&gt;<br />
&lt;asp:TemplateField HeaderText=&#8221;Country&#8221;&gt;<br />
&lt;EditItemTemplate&gt;<br />
&lt;asp:DropDownList ID=&#8221;ddl_CountryList&#8221; runat=&#8221;server&#8221;&gt;<br />
&lt;/asp:DropDownList&gt;<br />
&lt;/EditItemTemplate&gt;<br />
&lt;ItemTemplate&gt;<br />
&lt;asp:Label ID=&#8221;lblCountry&#8221; runat=&#8221;server&#8221; Text=&#8217;&lt;%# Bind(&#8220;CountryName&#8221;) %&gt; &#8216;&gt;&lt;/asp:Label&gt;<br />
&lt;/ItemTemplate&gt;<br />
&lt;/asp:TemplateField&gt;<br />
&lt;asp:CommandField ShowEditButton=&#8221;True&#8221; /&gt;<br />
&lt;/Columns&gt;<br />
&lt;/asp:GridView&gt;</p></blockquote>
<p>Given below is the code segment of the &#8220;Default.aspx&#8221; page.<br />
<strong>&#8220;Default.aspx.cs&#8221;</strong></p>
<blockquote><p>using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;</p>
<p>public partial class _Default : System.Web.UI.Page<br />
{<br />
DBAccess dba = new DBAccess();<br />
<strong>void FillGrid()</strong><br />
<strong>{</strong><br />
dba.GetTable(&#8220;select s.Name,s.Id,cl.CountryName from Student s inner join country_lloyd cl on cl.CountryId = s.CountryId&#8221;);<br />
GridView1.DataSource = dba.dt;<br />
GridView1.DataBind();<br />
<strong>}</strong><br />
<strong>void fillCombo(DropDownList ddl)</strong><br />
<strong>{</strong><br />
dba.GetTable(&#8220;select * from country_lloyd&#8221;);<br />
ddl.DataSource = dba.dt;<br />
ddl.DataTextField = dba.dt.Columns[1].ToString();<br />
ddl.DataValueField = dba.dt.Columns[0].ToString();<br />
ddl.DataBind();<br />
ListItem lstItem = new ListItem(&#8220;select&#8221;, &#8220;0&#8243;);<br />
ddl.Items.Insert(0, lstItem);<br />
<strong>}</strong><br />
protected void Page_Load(object sender, EventArgs e)<br />
{<br />
if (!IsPostBack)<br />
{<br />
FillGrid();<br />
}</p>
<p>}<br />
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)<br />
{<br />
string countryname = ((Label)GridView1.Rows[e.NewEditIndex].FindControl(&#8220;lblCountry&#8221;)).Text.ToString();<br />
GridView1.EditIndex = e.NewEditIndex;<br />
FillGrid();</p>
<p>DropDownList ddlCountryName = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl(&#8220;ddl_CountryList&#8221;);<br />
fillCombo(ddlCountryName);<br />
ddlCountryName.SelectedIndex = ddlCountryName.Items.IndexOf(ddlCountryName.Items.FindByText(countryname));</p>
<p>}<br />
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)<br />
{<br />
GridView1.EditIndex = -1;<br />
FillGrid();<br />
}<br />
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)<br />
{<br />
DropDownList ddlCountryName = (DropDownList)GridView1.Rows[e.RowIndex].FindControl(&#8220;ddl_CountryList&#8221;);<br />
string str= ddlCountryName.SelectedItem.Value.ToString();<br />
string str1 = GridView1.DataKeys[e.RowIndex].Value.ToString();<br />
dba.ExecuteQuery(&#8220;update Student set CountryId=&#8217;&#8221;+str+&#8221;&#8216; where Id=&#8217;&#8221;+ str1 +&#8221;&#8216;&#8221;);<br />
GridView1.EditIndex = -1;<br />
FillGrid();<br />
}<br />
}</p></blockquote>
<p><strong><em>Notes:</em></strong><br />
1) <em>FillGrid()</em> is method to fill the grid with values.<br />
2) <em>void fillCombo(DropDownList ddl)</em> is to fill the dropdownlist inside the gridview.<br />
3) <a href="http://www.three2tango.com/archives/89">DBAccess()</a> is the class used for database connection.<br />
4)GridView row editing,row upadating,row canceling events for explained in the<a href="http://www.three2tango.com/archives/89"> &#8220;gridview edit update cancel&#8221; </a>post</p>
]]></content:encoded>
			<wfw:commentRss>http://www.three2tango.com/techcorner/dropdownlist-inside-a-gridview.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filling Gridview without using database</title>
		<link>http://www.three2tango.com/techcorner/filling-gridview-without-using-database.html/</link>
		<comments>http://www.three2tango.com/techcorner/filling-gridview-without-using-database.html/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 06:43:51 +0000</pubDate>
		<dc:creator>Vivek</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[GridView Tips]]></category>
		<category><![CDATA[Tech Corner]]></category>
		<category><![CDATA[Filling gridview without using database]]></category>
		<category><![CDATA[gridview]]></category>
		<category><![CDATA[Temperory datatable]]></category>

		<guid isPermaLink="false">http://www.three2tango.com/?p=125</guid>
		<description><![CDATA[Sometimes we might be in a situation where we need to fill a gridview from the Client itself and not from the database
for eg:-filling a gridview with values entered into a textbox.
For this we need to provide as data source a datatable that will fill based on the values we enter.
Declare as global or in the BLL(if you are using N-Tier Structure)
public static DataTable TempTable;
Global Varaible
DataRow Dtr;
Create Structure of the datatable
private void CreateStructure()
{
TempTable = new DataTable(&#8220;DatatableName&#8221;);
TempTable.Columns.Add(&#8220;ColumnName&#8221;, typeof(datatype));//eg-TempTable.Columns.Add(&#8220;Name&#8221;, typeof(string))
..add more..
}
Now to Fill the GridView
private void FillGrid()
{
Dtr = TempTable.NewRow();
Dtr["ColumnName"] = Textbox1.text;
..add the ...]]></description>
			<content:encoded><![CDATA[<p>Sometimes we might be in a situation where we need to fill a <a href="http://www.three2tango.com/archives/89"></a>gridview from the Client itself and not from the database<br />
for eg:-filling a gridview with values entered into a textbox.<br />
For this we need to provide as data source a datatable that will fill based on the values we enter.</p>
<p>Declare as global or in the BLL(if you are using N-Tier Structure)<br />
public static DataTable TempTable;</p>
<blockquote><p>Global Varaible<br />
DataRow Dtr;</p></blockquote>
<p>Create Structure of the datatable</p>
<blockquote><p>private void CreateStructure()<br />
{<br />
TempTable = new DataTable(&#8220;DatatableName&#8221;);<br />
TempTable.Columns.Add(&#8220;ColumnName&#8221;, typeof(datatype));//eg-TempTable.Columns.Add(&#8220;Name&#8221;, typeof(string))<br />
..add more..<br />
}</p></blockquote>
<p>Now to Fill the GridView</p>
<blockquote><p>private void FillGrid()<br />
{<br />
Dtr = TempTable.NewRow();<br />
Dtr["ColumnName"] = Textbox1.text;<br />
..add the rest..<br />
TempTable.Rows.Add(Dtr);<br />
GridView1.DataSource = TempTable;<br />
GridView1.DataBind();<br />
}</p></blockquote>
<p>Call CreateStructure() at pageLoad<br />
Call FillGrid() at the event you want,eg button_Click<br />
Note that you have to create boundfields for the gridview,ie the datafield of each field in the gridview,as those in the createstructure()</p>
]]></content:encoded>
			<wfw:commentRss>http://www.three2tango.com/techcorner/filling-gridview-without-using-database.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Edit,Update,Cancel In a GridView</title>
		<link>http://www.three2tango.com/techcorner/editupdatecancel-in-a-gridview.html/</link>
		<comments>http://www.three2tango.com/techcorner/editupdatecancel-in-a-gridview.html/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 11:09:03 +0000</pubDate>
		<dc:creator>LG</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[GridView Tips]]></category>
		<category><![CDATA[Tech Corner]]></category>
		<category><![CDATA[gridview]]></category>

		<guid isPermaLink="false">http://www.three2tango.com/?p=89</guid>
		<description><![CDATA[I am posting this one with a notion that the readers do have a little bit knowledge about dot net.Hope it helps.
Pre-requisites
Add a gridview(gdvw_Sample) to your ‘Default.aspx’ page,

Bind the gridview with values from a table in the database.
 Remove “AutoGenerate Columns” of gridview.
Add  two  “boundfields”.
Set the primary key of the table as the “datakey” of the gridview.
Add a “command field-Edit Update Cancel”.

How to set the datakey and BoundFields???
Copying the Html code below will create a gridview with 2 boundfields  Name ,Address and datakey as Id.
&#60;asp:GridView ID=&#8221;gdvw_Sample&#8221; runat=&#8221;server&#8221; ...]]></description>
			<content:encoded><![CDATA[<blockquote><p>I am posting this one with a notion that the readers do have a little bit knowledge about dot net.Hope it helps.</p></blockquote>
<p><strong>Pre-requisites</strong><br />
Add a gridview(gdvw_Sample) to your ‘Default.aspx’ page,</p>
<ul>
<li>Bind the gridview with values from a table in the database.</li>
<li> Remove “AutoGenerate Columns” of gridview.</li>
<li>Add  two  “boundfields”.</li>
<li>Set the primary key of the table as the “datakey” of the gridview.</li>
<li>Add a “command field-Edit Update Cancel”.</li>
</ul>
<p><strong>How to set the datakey and BoundFields???</strong><br />
Copying the Html code below will create a gridview with 2 boundfields  Name ,Address and datakey as Id.</p>
<blockquote><p>&lt;asp:GridView ID=&#8221;gdvw_Sample&#8221; runat=&#8221;server&#8221; AutoGenerateColumns=&#8221;False&#8221;<br />
DataKeyNames=&#8221;Id&#8221; onrowcancelingedit=&#8221;gdvw_Sample_RowCancelingEdit&#8221;<br />
onrowediting=&#8221;gdvw_Sample_RowEditing&#8221; onrowupdating=&#8221;gdvw_Sample_RowUpdating&#8221;&gt;<br />
&lt;Columns&gt;<br />
&lt;asp:BoundField DataField=&#8221;Name&#8221; HeaderText=&#8221;Name&#8221; /&gt;<br />
&lt;asp:BoundField DataField=&#8221;Address&#8221; HeaderText=&#8221;Address&#8221; /&gt;<br />
&lt;asp:CommandField ShowEditButton=&#8221;True&#8221; /&gt;<br />
&lt;/Columns&gt;<br />
&lt;/asp:GridView&gt;</p></blockquote>
<ul>
<li>BoundFields can be set directly from the design also ,by &#8220;edit columns&#8221; of the gridview(taken from top right corner of the gridview itself).</li>
<li>Datakeys can be set from the &#8220;property&#8221;&gt;&gt; &#8220;DataKeyNames&#8221;.</li>
</ul>
<blockquote><p><strong>“default.aspx.cs”</strong><br />
using System;<br />
using System.Configuration;<br />
using System.Data;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.HtmlControls;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Xml.Linq;</p>
<p>public partial class _Default : System.Web.UI.Page<br />
{<br />
DBAccess dbobj = new DBAccess();<br />
protected void Page_Load(object sender, EventArgs e)<br />
{<br />
if (!IsPostBack)<br />
{<br />
bindgridview();<br />
}<br />
}<br />
protected void gdvw_Sample_RowEditing(object sender, GridViewEditEventArgs e)<br />
{<br />
gdvw_Sample.EditIndex = e.NewEditIndex;<br />
bindgridview();<br />
}</p>
<p>private void bindgridview()<br />
{<br />
dbobj.GetTable(&#8220;select * from Student&#8221;);<br />
gdvw_Sample.DataSource = dbobj.dt;<br />
gdvw_Sample.DataBind();<br />
}<br />
protected void gdvw_Sample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)<br />
{<br />
gdvw_Sample.EditIndex = -1;<br />
bindgridview();<br />
}<br />
protected void gdvw_Sample_RowUpdating(object sender, GridViewUpdateEventArgs e)<br />
{<br />
string t1 = ((TextBox)(gdvw_Sample.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString();<br />
string t2 = ((TextBox)(gdvw_Sample.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();<br />
string id = gdvw_Sample.DataKeys[e.RowIndex].Value.ToString();<br />
dbobj.ExecuteQuery(&#8220;update Student set Name=&#8217;&#8221;+t1+&#8221;&#8216;,Address=&#8217;&#8221;+t2+&#8221;&#8216;where Id=&#8217;&#8221;+id+&#8221;&#8216;&#8221;);<br />
gdvw_Sample.EditIndex = -1;<br />
bindgridview();<br />
}<br />
}</p></blockquote>
<p><em><strong>Notes:</strong></em><br />
1)	The method “bindgridview()” is used to bind the grid with values from a table.<br />
2)	“DBAccess” is a class wrote for accessing database.</p>
<p>To add this class,</p>
<p>Solution Explorer &gt;&gt;Rightclick on the website name&gt;&gt;Add newItem&gt;&gt;From the new window that opens,select CLASS,name it “DBAccess.cs”&gt;&gt;Paste the entire code below.</p>
<p><strong>&#8220;DBAccess.cs&#8221;</strong></p>
<blockquote><p>using System;<br />
using System.Data;<br />
using System.Configuration;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Web.UI.HtmlControls;<br />
using System.Data.Sql;<br />
using System.Data.SqlClient;<br />
using System.Data.Common;<br />
using System.Web.Configuration;</p>
<p>public class DBAccess<br />
{<br />
public SqlConnection sqlCon = new SqlConnection();<br />
public SqlCommand sqlCmd = new SqlCommand();<br />
public SqlDataAdapter sqlDa = new SqlDataAdapter();<br />
public DataTable dt;</p>
<p>public DBAccess()<br />
{<br />
sqlCon.ConnectionString = &#8220;server=.;uid=sa;pwd=admin;database=test&#8221;;<br />
}</p>
<p>private SqlConnection GetConnection()<br />
{<br />
try<br />
{<br />
if (sqlCon.State == ConnectionState.Open)<br />
{ sqlCon.Close(); }<br />
sqlCon.Open();<br />
}<br />
catch (Exception ex)<br />
{ sqlCon.Close(); }<br />
return sqlCon;<br />
}</p>
<p>public DataTable GetTable(string sql)<br />
{<br />
try<br />
{<br />
sqlCmd.Connection = GetConnection();<br />
sqlCmd.CommandType = CommandType.Text;<br />
sqlCmd.CommandText = sql;<br />
sqlDa.SelectCommand = sqlCmd;<br />
dt = new DataTable();<br />
sqlDa.Fill(dt);<br />
if (sqlCon.State == ConnectionState.Open)<br />
{ sqlCon.Close(); }<br />
}<br />
catch (Exception ex)<br />
{<br />
if (sqlCon.State == ConnectionState.Open)<br />
{ sqlCon.Close(); }<br />
}<br />
return dt;<br />
}</p>
<p>public int ExecuteQuery(string sql)<br />
{<br />
int rslt = 0;<br />
try<br />
{<br />
sqlCmd.Connection = GetConnection();<br />
sqlCmd.CommandType = CommandType.Text;<br />
sqlCmd.CommandText = sql;<br />
rslt = sqlCmd.ExecuteNonQuery();<br />
if (sqlCon.State == ConnectionState.Open)<br />
{ sqlCon.Close(); }<br />
}<br />
catch (Exception ex)<br />
{<br />
if (sqlCon.State == ConnectionState.Open)<br />
{ sqlCon.Close(); }<br />
}<br />
return rslt;<br />
}<br />
}</p></blockquote>
<p><strong><em>Notes:</em></strong><br />
1)	Don’t forget to edit the connection string in the constructor portion of the “DBAccess.cs” and also the database name,uid and password of sqlserver.</p>
<p>The Table design for the the table used above “Student”</p>
<p>Id		int<br />
Name  	varchar(50)<br />
Address	varchar(50)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.three2tango.com/techcorner/editupdatecancel-in-a-gridview.html/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

