How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind?http://forums.asp.net/t/1632321.aspx/1?How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Fri, 13 Apr 2012 16:19:41 -040016323214207318http://forums.asp.net/p/1632321/4207318.aspx/1?How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Hi, i'm new to this and I've been stuck for&nbsp;a while on this one.<br> I would like to retreive the data key (au_id)&nbsp;when&nbsp;the&nbsp;rowType =&nbsp;DataRow, so I can do a Sub query with the datakey Id.&nbsp;</p> <p>thank you for your help.</p> <p>&nbsp;</p> <p>I'm using this Microsoft&nbsp;Example:</p> <p>&lt;%@ Page language=<span style="color:#a31515">&quot;C#&quot;</span> %&gt; &lt;script runat=<span style="color:#a31515">&quot;server&quot;</span>&gt;<br> &nbsp; <br> &nbsp; <span style="color:green">// Create a template class to represent a dynamic template column.</span><br> &nbsp; <span style="color:blue">public</span> <span style="color:blue">class</span> GridViewTemplate : ITemplate<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; <span style="color:blue">private</span> DataControlRowType templateType;<br> &nbsp;&nbsp;&nbsp; <span style="color:blue">private</span> <span style="color:blue">string</span> columnName;<br> &nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <span style="color:blue">public</span> GridViewTemplate(DataControlRowType type, <span style="color:blue">string</span> colname)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; templateType = type;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; columnName = colname;<br> &nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp; <span style="color:blue">public</span> <span style="color:blue">void</span> InstantiateIn(System.Web.UI.Control container)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Create the content for the different row types.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">switch</span>(templateType)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">case</span> DataControlRowType.Header:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Create the controls to put in the header</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// section and set their properties.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Literal lc = <span style="color:blue">new</span> Literal();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lc.Text = <span style="color:#a31515">&quot;&lt;B&gt;&quot;</span> &#43; columnName &#43; <span style="color:#a31515"> &quot;&lt;/B&gt;&quot;</span>;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Add the controls to the Controls collection</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// of the container.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Controls.Add(lc);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">break</span>;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">case</span> DataControlRowType.DataRow:</p> <p>&nbsp;</p> <p><span style="color:green">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; GET&nbsp;au_id DataKey Here</span></p> <p>&nbsp;</p> <p><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Create the controls to put in a data row</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// section and set their properties.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Label firstName = <span style="color:blue">new</span> Label();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Label lastName = <span style="color:blue">new</span> Label();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Literal spacer = <span style="color:blue">new</span> Literal();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spacer.Text = <span style="color:#a31515">&quot; &quot;</span>;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// To support data binding, register the event-handling methods</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// to perform the data binding. Each control needs its own event</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// handler.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; firstName.DataBinding &#43;= <span style="color:blue">new</span> EventHandler(<span style="color:blue">this</span>.FirstName_DataBinding);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lastName.DataBinding &#43;= <span style="color:blue">new</span> EventHandler(<span style="color:blue">this</span>.LastName_DataBinding);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Add the controls to the Controls collection</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// of the container.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Controls.Add(firstName);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Controls.Add (spacer);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.Controls.Add(lastName);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">break</span>;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Insert cases to create the content for the other </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// row types, if desired.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">default</span>:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Insert code to handle unexpected values.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">break</span>; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <span style="color:blue">private</span> <span style="color:blue">void</span> FirstName_DataBinding(Object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Get the Label control to bind the value. The Label control</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// is contained in the object that raised the DataBinding </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// event (the sender parameter).</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Label l = (Label)sender;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Get the GridViewRow object that contains the Label control. </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridViewRow row = (GridViewRow)l.NamingContainer;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Get the field value from the GridViewRow object and </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// assign it to the Text property of the Label control.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l.Text = DataBinder.Eval(row.DataItem, <span style="color:#a31515">&quot;au_fname&quot;</span>).ToString();<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <span style="color:blue">private</span> <span style="color:blue">void</span> LastName_DataBinding(Object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp; { <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Get the Label control to bind the value. The Label control</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// is contained in the object that raised the DataBinding </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// event (the sender parameter).</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Label l = (Label)sender;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Get the GridViewRow object that contains the Label control.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridViewRow row = (GridViewRow)l.NamingContainer;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Get the field value from the GridViewRow object and </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// assign it to the Text property of the Label control.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l.Text = DataBinder.Eval(row.DataItem, <span style="color:#a31515">&quot;au_lname&quot;</span>).ToString();<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp; }<br> <br> &nbsp; <span style="color:blue">void</span> Page_Load(Object sender, EventArgs e)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <span style="color:green">// The field columns need to be created only when the page is</span><br> &nbsp;&nbsp;&nbsp; <span style="color:green">// first loaded. </span><br> &nbsp;&nbsp;&nbsp; <span style="color:blue">if</span> (!IsPostBack)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Dynamically create field columns to display the desired</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// fields from the data source. Create a TemplateField object </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// to display an author's first and last name.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TemplateField customField = <span style="color:blue">new</span> TemplateField();<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Create the dynamic templates and assign them to </span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// the appropriate template property.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; customField.ItemTemplate = <span style="color:blue">new</span> GridViewTemplate(DataControlRowType.DataRow, <span style="color:#a31515">&quot;Author Name&quot;</span>);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; customField.HeaderTemplate = <span style="color:blue">new</span> GridViewTemplate(DataControlRowType.Header, <span style="color:#a31515">&quot;Author Name&quot;</span>);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// Add the field column to the Columns collection of the</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// GridView control.</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthorsGridView.Columns.Add(customField);<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp; <br> &nbsp; }<br> <br> &lt;/script&gt;<br> <br> &lt;html&gt;<br> &nbsp; &lt;body&gt;<br> &nbsp;&nbsp;&nbsp; &lt;form runat=<span style="color:#a31515">&quot;server&quot;</span>&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;h3&gt;TemplateField Constructor Example&lt;/h3&gt;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:gridview id=<span style="color:#a31515">&quot;AuthorsGridView&quot;</span> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; datasourceid=<span style="color:#a31515">&quot;AuthorsSqlDataSource&quot;</span> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; autogeneratecolumns=<span style="color:#a31515">&quot;False&quot;<br> </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; datakeynames=<span style="color:#a31515">&quot;au_id&quot;</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; runat=<span style="color:#a31515">&quot;server&quot;</span>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/asp:gridview&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- This example uses Microsoft SQL Server and connects --&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- to the Pubs sample database.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:sqldatasource id=<span style="color:#a31515">&quot;AuthorsSqlDataSource&quot;</span>&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; selectcommand=<span style="color:#a31515">&quot;SELECT [au_id], [au_fname], [au_lname] FROM [authors]&quot;</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionstring=<span style="color:#a31515">&quot;server=localhost;database=pubs;integrated security=SSPI&quot;</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; runat=<span style="color:#a31515">&quot;server&quot;</span>&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/asp:sqldatasource&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &lt;/form&gt;<br> &nbsp; &lt;/body&gt;<br> &lt;/html&gt;<br> <br> &nbsp;</p> 2010-12-12T16:28:26-05:004207393http://forums.asp.net/p/1632321/4207393.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>You can get DataKey as</p> <pre style="font-family:consolas">GridView1.DataKeys(your&nbsp;row&nbsp;Index).Value.ToString()</pre> <p>You may get the row index as <i style="text-decoration:underline">row.RowIndex </i>in your code.</p> 2010-12-12T18:42:52-05:004209486http://forums.asp.net/p/1632321/4209486.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Yes, please don't forget to set DataKeyNames property&nbsp;for your GridView.&nbsp;</p> 2010-12-14T02:38:32-05:004211195http://forums.asp.net/p/1632321/4211195.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Hi Decker dong,</p> <p>I have set the dataeynames for the gridview,</p> <p>but I can't retreive the datakey in&nbsp;&nbsp;the '<span style="color:blue">case</span> DataControlRowType.DataRow:'</p> 2010-12-14T22:52:11-05:004211204http://forums.asp.net/p/1632321/4211204.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Get the value from the DataItem Directly</p> <p></p> <pre class="prettyprint">string dataKeyValue = DataBinder.Eval(e.Row.DataItem, &quot;DataKeyID Name&quot;).ToString();</pre> <p></p> 2010-12-14T23:05:38-05:004211220http://forums.asp.net/p/1632321/4211220.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Hi Santhosh,<br> thank you for your reply.</p> <p>here's what i did, but i'm getting this error (the name 'e' does not exist in the current context)<br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void InstantiateIn(System.Web.UI.Control container)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create the content for the different row types.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch (templateType)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case DataControlRowType.Header:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create the controls to put in the header</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case DataControlRowType.DataRow:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create the controls to put in a data row<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>string dataKeyValue = DataBinder.Eval(e.Row.DataItem, &quot;au_id&quot;).ToString(); </strong></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case DataControlRowType.Footer:</p> <p><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Insert code to handle unexpected values.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;</p> 2010-12-14T23:35:22-05:004211226http://forums.asp.net/p/1632321/4211226.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>I don't think you can get the DataItem there in that InstantiateIn Method. You have to add DataBinding Handler for the control and get the value from there</p> <p><br> </p> <pre class="prettyprint">public void InstantiateIn(System.Web.UI.Control container) { DataControlRowType templateType = DataControlRowType.DataRow; // Create the content for the different row types. switch (templateType) { case DataControlRowType.Header: // Create the controls to put in the header break; case DataControlRowType.DataRow: // Create the controls to put in a data row Label lblName = new Label(); lblName.DataBinding &#43;= new EventHandler(lblName_DataBinding); container.Controls.Add(lblName); break; case DataControlRowType.Footer: default: // Insert code to handle unexpected values. break; } } void lblName_DataBinding(object sender, EventArgs e) { Label lblName = (Label)sender; GridViewRow container = (GridViewRow)lblName.NamingContainer; lblName.Text = DataBinder.Eval(container.DataItem, &quot;my column name here&quot;).ToString(); }</pre><p><br>Probably, you can try</p><p><blockquote><span class="icon-blockquote"></span><h4>heyyou</h4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span mce_name="strong" mce_style="font-weight: bold;" style="font-weight: bold;" class="Apple-style-span" mce_fixed="1">string dataKeyValue = DataBinder.Eval(e.Row.DataItem, "au_id").ToString();</span></blockquote></p><pre class="prettyprint">string strDataKeyValue=DataBinder.Eval(((GridViewRow)container).DataItem,"datakey column name").ToString();</pre> 2010-12-14T23:47:12-05:004211243http://forums.asp.net/p/1632321/4211243.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Hi Santhosh,<br> i've added your&nbsp;code and now i'm getting this error</p> <p>InvalidCastException was unhandled by user code (Unable to cast&nbsp;an objet&nbsp;of type 'System.Web.UI.WebControls.DataControlFieldCell'&nbsp;to a&nbsp;type 'System.Web.UI.WebControls.GridViewRow'.)</p> <p>Alain</p> 2010-12-15T00:06:30-05:004211252http://forums.asp.net/p/1632321/4211252.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>heyyou</h4> <br> I would like to retreive the data key (au_id)&nbsp;when&nbsp;the&nbsp;rowType =&nbsp;DataRow, so I can do a Sub query with the datakey Id.&nbsp;</blockquote> <p></p> <p>You want to do your query in the RowDataBound event of the GridView instead (OR, do a join in the &quot;original&quot; query for the GridView itself). The code you're using simply creates <i>a </i>templatefield (a couple of labels making up the &quot;Author Name&quot; column) and ensures data for those controls gets bound (the comments there do explain).</p> 2010-12-15T00:19:13-05:004211270http://forums.asp.net/p/1632321/4211270.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Hi Peter,<br> your right, at the moment my code is basic.</p> <p>here's what i'm trying to do, I have a gridview with&nbsp;1 column called name, &nbsp;that will never change. I'm using the 'GridViewTemplate' to add dynamic column based on a query. Inside each DataRow for the dynamic column I&nbsp;would like&nbsp;to run&nbsp;subqueries based on the DataKey.</p> <p>I don't know if there's an other way of doing this.</p> <p>Alain</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2010-12-15T01:00:04-05:004211286http://forums.asp.net/p/1632321/4211286.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>Again, that piece of code will and is meant to allow you create <i><u>a</u></i> field, say, name; for other fields I guess you're trying to use a sub-query - I'm suggesting you to do it either: 1. at the datasource level OR 2. at the GridView level (event: RowDataBound)</p> <p>so, you could create a join and fetch all the data you'd want <i>before</i> you create <i>any </i>columns or execute a query in the RowDataBound event of the GridView, where you would be able to get all the column values using e.Row.DataItem as has been shown you already.</p> 2010-12-15T01:20:20-05:004931524http://forums.asp.net/p/1632321/4931524.aspx/1?Re+How+do+I+get+the+datakey+for+a+DataControlRowType+DataRow+when+a+TemplateField+is+created+in+code+behind+Re: How do I get the datakey for a DataControlRowType.DataRow when a TemplateField is created in code behind? <p>i know i am replying abit late here..i just want to know why this particular code is being used...</p> 2012-04-13T16:19:41-04:00