<?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>Naresh.Jois &#187; GridView</title>
	<atom:link href="http://www.nareshjois.com/tag/gridview/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nareshjois.com</link>
	<description>Home of a professional developer &#38; a aspiring photographer</description>
	<lastBuildDate>Fri, 03 Feb 2012 16:57:10 +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>Using a ASP.net Grid view using code</title>
		<link>http://www.nareshjois.com/2007/11/using-a-aspnet-grid-view-using-code/</link>
		<comments>http://www.nareshjois.com/2007/11/using-a-aspnet-grid-view-using-code/#comments</comments>
		<pubDate>Sun, 18 Nov 2007 04:28:41 +0000</pubDate>
		<dc:creator>naresh</dc:creator>
				<category><![CDATA[.net Programming]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[GridView]]></category>
		<category><![CDATA[VB.net]]></category>

		<guid isPermaLink="false">http://nareshjois.wordpress.com/2007/11/18/using-a-aspnet-grid-view-using-code/</guid>
		<description><![CDATA[We have all used grid view along with data sources but some time you need to to that from code, the problem is most of us do not know how to use this from code, but Its not that difficult to that. Here&#8217;s a simple way to do that. A Simple grid  First column is [...]]]></description>
			<content:encoded><![CDATA[<p>We have all used grid view along with data sources but some time you need to to that from code, the problem is most of us do not know how to use this from code, but Its not that difficult to that. Here&#8217;s a simple way to do that.</p>
<p>A Simple grid  First column is a readonly templated field, second one a readonly bound field and third a templated entry field. At the Edit the edit command Field</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:consolas, monaco;">&lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot;  CssSelectorClass=&quot;PrettyGridView&quot; PageSize=&quot;50&quot; AllowSorting=&quot;True&quot;   AutoGenerateColumns=&quot;False&quot; EnableSortingAndPagingCallbacks=&quot;True&quot;&gt;
   &lt;columns&gt;
   &lt;asp:templatefield HeaderText=&quot;Center Id&quot; SortExpression=&quot;center_Id&quot;&gt;
   &lt;edititemtemplate&gt;
   &lt;asp:Label ID=&quot;Label10&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Eval<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind1&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/edititemtemplate&gt;
   &lt;itemtemplate&gt;
   &lt;asp:Label ID=&quot;Label10&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Bind<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind1&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/itemtemplate&gt;
   &lt;/asp:templatefield&gt;
   &lt;asp:boundfield DataField=&quot;City_Name&quot; HeaderText=&quot;City Name&quot;            ReadOnly=&quot;True&quot; SortExpression=&quot;City_Name&quot;&gt;&lt;/asp:boundfield&gt;
   &lt;asp:templatefield HeaderText=&quot;Rate&quot;&gt;
   &lt;edititemtemplate&gt;
   &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Bind<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind2&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/edititemtemplate&gt;
   &lt;itemtemplate&gt;
   &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Bind<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind2&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/itemtemplate&gt;
   &lt;/asp:templatefield&gt;
   &lt;asp:commandfield ShowEditButton=&quot;True&quot;&gt;&lt;/asp:commandfield&gt;
   &lt;/columns&gt;
&lt;/asp:GridView&gt;</pre></div></div>

<p>First Write a Function for loading data which will be reused on every action. I call it BindData()</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;"><span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>        conn <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> OleDbConnection<span style="color: #000000;">&#40;</span>GetJetConnectionString<span style="color: #000000;">&#41;</span>
       adap <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> OleDbDataAdapter<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Custom SQL Query&quot;</span>, conn<span style="color: #000000;">&#41;</span>
       <span style="color: #0600FF;">Dim</span> datatable1 <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> DataTable
       adap.<span style="color: #0000FF;">Fill</span><span style="color: #000000;">&#40;</span>datatable1<span style="color: #000000;">&#41;</span>
       dataset1 <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> DataSet
       dataset1.<span style="color: #0000FF;">Tables</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>datatable1<span style="color: #000000;">&#41;</span>
       <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">GridView1</span>.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> dataset1
       <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">GridView1</span>.<span style="color: #0000FF;">DataBind</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>Now since the gridview is binded from code we have to handle all its events. So When the user clicks on edit. Here I have done two things  one is enable editing mode, then I store the data of textbox in a viewstate, so that it can be accessed later when updating, please remember that the regular method of e.OldValues and e.NewValues Will not work because we have to do that manually, (which can be done, but for our purposes we will use viewstate)</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;">Protected <span style="color: #0600FF;">Sub</span> GridView1_RowEditing<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>,          <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">GridViewEditEventArgs</span><span style="color: #000000;">&#41;</span>          <span style="color: #FF8000;">Handles</span> GridView1.<span style="color: #0000FF;">RowEditing</span>
    GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">NewEditIndex</span>
    BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Dim</span> row <span style="color: #FF8000;">As</span> GridViewRow <span style="color: #008000;">=</span> GridView1.<span style="color: #0000FF;">Rows</span><span style="color: #000000;">&#40;</span>GridView1.<span style="color: #0000FF;">EditIndex</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">If</span> row <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
        <span style="color: #0600FF;">Dim</span> t <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> TryCast<span style="color: #000000;">&#40;</span>row.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;TextBox1&quot;</span><span style="color: #000000;">&#41;</span>, TextBox<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">If</span> t <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
            ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;OldRate&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> t.<span style="color: #0000FF;">Text</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>Now for the cancel part</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;">Protected <span style="color: #0600FF;">Sub</span> GridView1_RowCancelingEdit<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>,          <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">GridViewCancelEditEventArgs</span><span style="color: #000000;">&#41;</span>         <span style="color: #FF8000;">Handles</span> GridView1.<span style="color: #0000FF;">RowCancelingEdit</span>
   GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
   BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
 <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>Now for the update part. First new value is found out, if it is blank nothing is done, if old value does not Exist INSERT command is used if old value does exist UPDATE command is used. Here I did a Simple SQL execution but I think you will figure out a better way</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;">Protected <span style="color: #0600FF;">Sub</span> GridView1_RowUpdating<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>,          <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">GridViewUpdateEventArgs</span><span style="color: #000000;">&#41;</span>          <span style="color: #FF8000;">Handles</span> GridView1.<span style="color: #0000FF;">RowUpdating</span>
    <span style="color: #0600FF;">Dim</span> new_rate, old_rate <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
    old_rate <span style="color: #008000;">=</span> ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;OldRate&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Dim</span> row <span style="color: #FF8000;">As</span> GridViewRow <span style="color: #008000;">=</span> GridView1.<span style="color: #0000FF;">Rows</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">RowIndex</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">If</span> row <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
        <span style="color: #0600FF;">Dim</span> t <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> TryCast<span style="color: #000000;">&#40;</span>row.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;TextBox1&quot;</span><span style="color: #000000;">&#41;</span>, TextBox<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">If</span> t <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
            new_rate <span style="color: #008000;">=</span> t.<span style="color: #0000FF;">Text</span>
            <span style="color: #0600FF;">If</span> new_rate <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span>
                GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
                BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Exit</span> <span style="color: #0600FF;">Sub</span>
            <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
            <span style="color: #0600FF;">If</span> new_rate <span style="color: #008000;">=</span> old_rate <span style="color: #FF8000;">Then</span>
                GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
                BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Exit</span> <span style="color: #0600FF;">Sub</span>
            <span style="color: #FF8000;">Else</span>
                <span style="color: #0600FF;">Dim</span> label1 <span style="color: #FF8000;">As</span> Label <span style="color: #008000;">=</span> TryCast<span style="color: #000000;">&#40;</span>row.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;label10&quot;</span><span style="color: #000000;">&#41;</span>, Label<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Dim</span> center_id <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> label1.<span style="color: #0000FF;">Text</span>
                <span style="color: #0600FF;">Dim</span> conn <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> OleDbConnection<span style="color: #000000;">&#40;</span>ConnectionString<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Dim</span> cmd <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> OleDbCommand
                cmd.<span style="color: #0000FF;">Connection</span> <span style="color: #008000;">=</span> conn
                cmd.<span style="color: #0000FF;">CommandType</span> <span style="color: #008000;">=</span> CommandType.<span style="color: #0000FF;">Text</span>
                <span style="color: #0600FF;">Dim</span> SQL <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
                <span style="color: #0600FF;">If</span> old_rate <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span>
                    SQL <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;INSERT INTO QUERY&quot;</span>
                <span style="color: #FF8000;">Else</span>
                    SQL <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;UPDATE QUERY&quot;</span>
                <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
                cmd.<span style="color: #0000FF;">CommandText</span> <span style="color: #008000;">=</span> SQL
                conn.<span style="color: #0600FF;">Open</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                cmd.<span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                conn.<span style="color: #0600FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
                BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
        <span style="color: #FF8000;">Else</span>
            GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
            BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nareshjois.com/2007/11/using-a-aspnet-grid-view-using-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

