Gridview - Adding Columnshttp://forums.asp.net/t/1468326.aspx/1?Gridview+Adding+ColumnsThu, 10 Sep 2009 07:06:59 -040014683263394230http://forums.asp.net/p/1468326/3394230.aspx/1?Gridview+Adding+ColumnsGridview - Adding Columns <p></p> &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Hello,&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;My question is: Can I add in new column(S) in the gridview if the column is not found in the database?&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp;- E.g. An indicator if a certain lecturer is a ML/Lec (roles).&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;I have a gridview that is &quot;extracted&quot; out from the database.&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;But I want to add in a new row which is not in the database but links to the data in the database.&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;For example, in the database, I know that a certain lecturer is a module leader, staff.&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;But I want to have an extra column which can indicate in itself (probably a tick/mark) that the following name (prev column) is a module leader or staff; and i am not allowed to add new column in the database.&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Any help appreciated.&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Need me to further explain my queries, let me know. :)&lt;/div&gt; <p></p> <p>Hello,</p> <p>My question is: Can I add in new column(S) in the gridview if the column is not found in the database?</p> <p>&nbsp;&nbsp;- E.g. An indicator if a certain lecturer is a ML/Lec (roles).</p> <p><br> </p> <p>I have a gridview that is &quot;extracted&quot; out from the database.</p> <p>But I want to add in a new row which is not in the database but links to the data in the database.</p> <p>For example, in the database, I know that a certain lecturer is a module leader, staff.</p> <p>But I want to have an extra column which can indicate in itself (probably a tick/mark) that the following name (prev column) is a module leader or staff; and i am not allowed to add new column in the database.</p> <p><br> </p> <p>Any help appreciated.</p> <p>Need me to further explain my queries, let me know. :)</p> <p></p> <p></p> 2009-09-09T01:29:31-04:003394340http://forums.asp.net/p/1468326/3394340.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p></p> <blockquote><span class="icon-blockquote"></span> <h4>husna_</h4> &quot;extracted&quot; out from the database.</blockquote> - meaning AutoGeneratedColumns=&quot;true&quot; ?? <p></p> <p>If yes, you can <i>introduce</i> a column in the sql itself, for example:<br> <br> &quot;SELECT Col1, Col2, Col3, Col4, Col5, '' As Leader<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM sometablename <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE .........&quot;<br> <br> and then manipulate Cells[5].Text further with any sign or symbol in the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx"> RowDataBound</a> event as the rows are bound. As you want to compare the previous column you can check the underlying data for that column with the DataItem in the GridViewRowEventArgs e passed in to the RowDataBound event,<br> <br> &nbsp;&nbsp;&nbsp;&nbsp; DataRowView drv = e.Row.DataItem as DataRowView;<br> &nbsp;&nbsp;&nbsp;&nbsp; //and then do the check like:<br> &nbsp;&nbsp;&nbsp;&nbsp; if (drv[&quot;<i>LeaderColumnName</i>&quot;] == &quot;ModuleLeader&quot;)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //set e.Row.Cells[5].Text = &quot;Yes&quot; ;<br> &nbsp;&nbsp;&nbsp; } <br> <br> that would be simple enough to do...maybe add color to the cell or other formatting etc<br> <br> </p> <p>And IF you have AutoGeneratedColumns=&quot;false&quot; then you can always add another TemplateField with an ItemTemplate which would allow you any control like a Label or ImageButton etc. Again, you'd use the RowDataBound event except that this time you'll do a FindControl to get to the templated control and then set its properties. simple example: <a href="http://www.codekeep.net/snippets/61a14e9c-c14f-4611-814e-99146e1f80e2.aspx"> http://www.codekeep.net/snippets/61a14e9c-c14f-4611-814e-99146e1f80e2.aspx</a></p> <p><br> </p> <p><br> </p> <p>p.s. if you use vb.net then you can convert code <a href="http://www.developerfusion.com/tools/convert/csharp-to-vb/"> here</a><br> </p> 2009-09-09T03:23:29-04:003394388http://forums.asp.net/p/1468326/3394388.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>&nbsp;Another approach would be to calculate the data in the SQL Query itself. </p> <p>SELECT a, b, a*b/5 AS c FROM MathTable</p> <p>Here c is a calculated column.</p> 2009-09-09T03:59:36-04:003394411http://forums.asp.net/p/1468326/3394411.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello Peter,</p> <p>Nope nope. It's not really auto generated. Erm.. how do i say..<br> This is from one of the page(some of the codes) where users can filter what they want to search from.</p> <p><br> </p> 2009-09-09T04:22:03-04:003394419http://forums.asp.net/p/1468326/3394419.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello Vijayst,</p> <p>Erm. I am not computing actually.</p> <p>I just want to show out the column which is derived from another column.</p> <p>Let's say If blabla is a module leader, it will show out in a new row (called role) as ML.</p> 2009-09-09T04:31:26-04:003394420http://forums.asp.net/p/1468326/3394420.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>I was refering to the AutoGeneratedColumns property on the GridView; I'm still not sure whether you have it set or not. for example:<br> <br> &lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; <b>AutoGenerateColumns=&quot;false&quot;</b> DataSourceID=&quot;XmlDataSource2&quot;&gt;......&lt;/asp:GridView&gt;</p> <p><br> </p> 2009-09-09T04:31:31-04:003394431http://forums.asp.net/p/1468326/3394431.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello Peter,</p> <p>Nope, I did not set it to true.</p> <p><br> </p> <p></p> <pre class="prettyprint">&lt;asp:GridView ID=&quot;gridAMDisplay&quot; runat=&quot;server&quot; BorderColor=&quot;Black&quot; BorderStyle=&quot;Solid&quot; AutoGenerateColumns=&quot;False&quot; style=&quot;border-right: #000000 3px solid; border-top: #000000 3px solid; border-left: #000000 3px solid; border-bottom: #000000 3px solid&quot; Width=&quot;993px&quot;&gt; &lt;RowStyle BorderColor=&quot;White&quot; Font-Bold=&quot;True&quot; /&gt;&lt;Columns&gt; &lt;asp:BoundField HeaderText=&quot;Module&quot; DataField=&quot;ModuleCode&quot; &gt; &lt;HeaderStyle BorderColor=&quot;Black&quot; BorderStyle=&quot;Solid&quot; BorderWidth=&quot;3px&quot; /&gt; &lt;ItemStyle BorderColor=&quot;Black&quot; BorderStyle=&quot;Solid&quot; /&gt; &lt;/asp:BoundField&gt; .................. ................. .............. &lt;/asp:GridView&gt;</pre> <p><br> </p> <p>Do i have to set it to true?</p> <p>What is the difference whether i put it as false or true?&nbsp;</p> <p>Husna</p> 2009-09-09T04:40:31-04:003394445http://forums.asp.net/p/1468326/3394445.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p><b>when AutoGenerateColumns is true</b>, Bound Field generate automatically. So you don't have to mention Bound Field expictly.&nbsp;</p> <p><b>When AutoGenerateColumns is false</b>, Bound Field does not generate. So you have to mention Bound Field.</p> <p>I think in ur case let AutoGenerateColumns should be False</p> 2009-09-09T04:52:29-04:003394453http://forums.asp.net/p/1468326/3394453.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello Manoj Karkera,</p> <p>If that's the case, setting it to false, am i able to still use peter's suggestion?</p> 2009-09-09T04:55:39-04:003394457http://forums.asp.net/p/1468326/3394457.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p></p> <blockquote><span class="icon-blockquote"></span> <h4>husna_</h4> Do i have to set it to true?</blockquote> <p></p> <p>no you don't <i>have</i> to set it to true; just FYI: we do that when we don't know the database columns that are being included in the results OR when the columns themselves are dynamic (sometimes col1 and col2 are fetched while some other time col3 and col4 are); when it is set to &quot;true&quot; the GridView generates columns accordingly, refer: <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx"> http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx</a><br> </p> <p><br> </p> <p>Like I mentioned earlier, you can take the second option and, say, use a Label control for the extra column that you want:</p> <p><span><span id="ctl00_contentContainer_ctl48_ctl00_ctl02"></span></span></p> <p>wire up the RowDataBound event: <span><span><br> &lt;asp:GridView&nbsp;ID=</span><span>&quot;gridAMDisplay&quot;</span><span>&nbsp;runat=</span><span>&quot;server&quot;</span><span>&nbsp;BorderColor=</span><span>&quot;Black&quot;</span><span> </span></span><span>BorderStyle=<span>&quot;Solid&quot;</span><span>&nbsp;AutoGenerateColumns=</span><span>&quot;False&quot;</span><span>&nbsp;style=</span><span>&quot;border-right:&nbsp;#000000&nbsp;3px&nbsp;solid;&nbsp;border-top:&nbsp;#000000&nbsp;3px&nbsp;solid;&nbsp;border-left:&nbsp;#000000&nbsp;3px&nbsp;solid;&nbsp;border-bottom:&nbsp;#000000&nbsp;3px&nbsp;solid&quot;</span><span>&nbsp;Width=</span><span>&quot;993px&quot;&nbsp;</span></span> OnRowDataBound<span style="color:maroon"><span style="color:maroon">=&quot;</span></span><span><span>gridAMDisplay_RowDataBound&quot;</span></span><span style="color:maroon"><span style="color:maroon"></span></span><span><span></span><span>&gt;</span></span></p> <p><span><span>add a templatefield to the existing &lt;Columns&gt;, example:</span></span></p> <p>&lt;asp:TemplateField HeaderText=&quot;Leader&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ItemTemplate&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;&quot; /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ItemTemplate&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/asp:TemplateField&gt;</p> <p><br> </p> <p><span><span>and in the code behind the handler:<br> </span></span>protected void <span style="color:maroon"><span style="color:maroon"></span></span><span><span>gridAMDisplay_RowDataBound</span></span>(object sender, GridViewRowEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (e.Row.RowType == DataControlRowType.DataRow)<br> &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; //find the Label control using FindControl and set its Text on some column value<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataRowView row = (DataRowView)e.Row.DataItem;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (row[&quot;<i>previouscolumnname</i>&quot;] == &quot;ModuleLeader&quot;)<br> &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; Label lbl = e.Row.FindControl(&quot;Label1&quot;) as Label;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (lbl != null)<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lbl.Text = &quot;Yes&quot;; &nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> </p> <p>&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; } <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> </p> <ol start="1"> </ol> <p>replace previouscolumnname with your actual 'previous' column name ...as also the actual value you're comparing to etc.<br> </p> 2009-09-09T04:59:32-04:003394464http://forums.asp.net/p/1468326/3394464.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>yes you can use peter's suggestion.</p> 2009-09-09T05:01:57-04:003394499http://forums.asp.net/p/1468326/3394499.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>&nbsp;Hi,</p> <p>Regarding adding a new colum... You can dynamically add Template Field</p> <p>protected void Button1_Click(object sender, System.EventArgs e)<br> {<br> &nbsp;&nbsp;&nbsp; TemplateField Field = new TemplateField();<br> &nbsp;&nbsp;&nbsp; Field.HeaderText = &quot;New Column&quot;;<br> &nbsp;&nbsp;&nbsp; DataControlField Col = Field;<br> &nbsp;&nbsp;&nbsp; GridView1.Columns.Add(Col);<br> }</p> <p>in this column you can also add controls</p> <p>basheerkal</p> <p>&nbsp;</p> 2009-09-09T05:19:29-04:003394506http://forums.asp.net/p/1468326/3394506.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <pre class="prettyprint">&lt;%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/MasterPage2.master&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;AddCol.aspx.cs&quot; Inherits=&quot;AddCol&quot; Title=&quot;Untitled Page&quot; %&gt; &lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;MainContent&quot; Runat=&quot;Server&quot;&gt; &lt;asp:GridView ID=&quot;grdViewErrorLog&quot; runat=&quot;server&quot; AllowPaging=&quot;True&quot; PageSize=&quot;30&quot; AutoGenerateColumns=&quot;False&quot; BorderStyle=&quot;None&quot; TabIndex=&quot;1&quot; CssClass=&quot;GridViewStyle&quot; DataKeyNames=&quot;EmployeeID&quot; Width=&quot;100%&quot; DataSourceID=&quot;XmlDataSource1&quot; &gt; &lt;FooterStyle BackColor=&quot;#C6C3C6&quot; ForeColor=&quot;Black&quot; /&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&quot;Employee ID&quot; SortExpression=&quot;EmployeeID&quot;&gt; &lt;HeaderStyle CssClass=&quot;GridViewHeaderStyle&quot; /&gt; &lt;ItemStyle CssClass=&quot;GridViewItemStyle&quot; Width=&quot;25%&quot; /&gt; &lt;ItemTemplate &gt; &lt;asp:Label ID=&quot;lblEmployeeID&quot; Text='&lt;%# Eval(&quot;EmployeeID&quot;) %&gt;' runat=&quot;server&quot;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&quot;EmployeeName&quot; SortExpression=&quot;EmployeeName&quot;&gt; &lt;HeaderStyle CssClass=&quot;GridViewHeaderStyle&quot; /&gt; &lt;ItemStyle CssClass=&quot;GridViewItemStyle&quot; Width=&quot;25%&quot; /&gt; &lt;ItemTemplate &gt; &lt;asp:Label ID=&quot;lblEmployeeName&quot; Text='&lt;%# Eval(&quot;EmployeeName&quot;) %&gt;' runat=&quot;server&quot;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&quot;Department&quot; SortExpression=&quot;Department&quot;&gt; &lt;HeaderStyle CssClass=&quot;GridViewHeaderStyle&quot; /&gt; &lt;ItemStyle CssClass=&quot;GridViewItemStyle&quot; Width=&quot;5%&quot; /&gt; &lt;ItemTemplate &gt; &lt;asp:Label ID=&quot;lblDepartment&quot; Text='&lt;%# Eval(&quot;Department&quot;) %&gt;' runat=&quot;server&quot;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&quot;Native Language&quot; SortExpression=&quot;nativelanguage&quot;&gt; &lt;HeaderStyle CssClass=&quot;GridViewHeaderStyle&quot; /&gt; &lt;ItemStyle CssClass=&quot;GridViewItemStyle&quot; Width=&quot;5%&quot; /&gt; &lt;ItemTemplate &gt; &lt;asp:Label ID=&quot;lblnativelanguage&quot; Text='&lt;%# ((Eval(&quot;Department&quot;).ToString() == &quot;IT&quot;) ? &quot;IT&quot; : &quot;Non IT&quot;) %&gt;' runat=&quot;server&quot;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:CommandField ButtonType=&quot;Image&quot; HeaderText=&quot;Details&quot; SelectImageUrl=&quot;~/Images/Modify.gif&quot; SelectText=&quot;&quot; ShowSelectButton=&quot;True&quot;&gt; &lt;HeaderStyle CssClass=&quot;GridViewHeaderStyle&quot; /&gt; &lt;ItemStyle CssClass=&quot;GridViewItemStyle&quot; Width=&quot;5%&quot; Wrap=&quot;true&quot; /&gt; &lt;/asp:CommandField&gt; &lt;/Columns&gt; &lt;EmptyDataTemplate&gt; &lt;p class=&quot;NoDataMessageStyle&quot;&gt;No Records found&lt;/p&gt; &lt;/EmptyDataTemplate&gt; &lt;RowStyle BackColor=&quot;#DEDFDE&quot; ForeColor=&quot;Black&quot; CssClass=&quot;GridViewItemClass&quot; /&gt; &lt;SelectedRowStyle BackColor=&quot;#9471DE&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt; &lt;HeaderStyle BackColor=&quot;#4A3C8C&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;#E7E7FF&quot; /&gt; &lt;PagerStyle BackColor=&quot;#E7E7FF&quot; ForeColor=&quot;#4A3C8C&quot; HorizontalAlign=&quot;Right&quot; /&gt; &lt;PagerSettings PageButtonCount=&quot;30&quot; /&gt; &lt;/asp:GridView&gt; &lt;asp:XmlDataSource ID=&quot;XmlDataSource1&quot; runat=&quot;server&quot; DataFile=&quot;Employee.xml&quot;&gt;&lt;/asp:XmlDataSource&gt; &lt;/asp:Content&gt;</pre> <P><BR>The XML file would be</P><pre class="prettyprint">&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;Employees&gt; &lt;Employee EmployeeID="9758" EmployeeName="Thirumalai Muniswamy" Department="IT" ManagerName="Sivakumar" Date="October 21 2003" MainPhone="1 (880) 555-1111" /&gt; &lt;Employee EmployeeID="9759" EmployeeName="Raveendra Dhaka" Department="Finanace" ManagerName="Naveendra Reddy" Date="October 21 2003" MainPhone="1 (880) 555-2222" /&gt; &lt;Employee EmployeeID="9760" EmployeeName="Hemnath Manoharan" Department="Sales" ManagerName="Manish Kumar" Date="October 21 2003" MainPhone="1 (880) 555-3333"/&gt; &lt;/Employees&gt;</pre> <p><br> &nbsp;</p> <p>Regards, Thirumalai M</p> 2009-09-09T05:22:47-04:003396539http://forums.asp.net/p/1468326/3396539.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello Peter,&nbsp;</p> <p>The row did came out. :)<br> But, I have a so called warning?</p> <p>Erm.. Although the row did came out, i dont understand why the &quot;Yes&quot; didnt show out.&nbsp;</p> 2009-09-10T01:39:58-04:003396544http://forums.asp.net/p/1468326/3396544.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello manoj0682,</p> <p>Thank you for the reply. :)</p> 2009-09-10T01:41:51-04:003396552http://forums.asp.net/p/1468326/3396552.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello basheerkal,</p> <p>Thank you for your help. But i had used peter's suggestion. :)<br> But I'll take note. Thanks for your time. :)</p> <p>&nbsp;</p> 2009-09-10T01:49:25-04:003396555http://forums.asp.net/p/1468326/3396555.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>hey, glad you made it!</p> <p>about the error: you need to compare strings, so try doing this:</p> <p>row[&quot;ModuleLeader&quot;].ToString() == &quot;ModuleLeader&quot;</p> <p><br> </p> <p>and that's assuming that the value for column ModuleLeader will <i>never</i> be null, else you've got to check for !DBNull.Value.Equals(row[&quot;ModuleLeader&quot;])</p> 2009-09-10T01:51:59-04:003396583http://forums.asp.net/p/1468326/3396583.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello&nbsp;</p> <p>Is this the part that i can add columns when the contains depends on another column?</p> <p></p> <pre class="prettyprint">&lt;asp:TemplateField HeaderText=&quot;Native Language&quot; SortExpression=&quot;nativelanguage&quot;&gt; &lt;HeaderStyle CssClass=&quot;GridViewHeaderStyle&quot; /&gt; &lt;ItemStyle CssClass=&quot;GridViewItemStyle&quot; Width=&quot;5%&quot; /&gt; &lt;ItemTemplate &gt; &lt;asp:Label ID=&quot;lblnativelanguage&quot; Text='&lt;%# ((Eval(&quot;Department&quot;).ToString() == &quot;IT&quot;) ? &quot;IT&quot; : &quot;Non IT&quot;) %&gt;' runat=&quot;server&quot;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt;</pre> <p><br> <br> </p> <p></p> 2009-09-10T02:19:03-04:003396606http://forums.asp.net/p/1468326/3396606.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>Hello peter,</p> <p>heehee. Thank you. :)<br> But, i have more queries. whooops. &gt;.&lt;</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>PeteNet</h4> assuming that the value for column ModuleLeader will <i>never</i> be null, else you've got to check for !DBNull.Value.Equals(row[&quot;ModuleLeader&quot;])</blockquote> <p></p> <p>What do you mean by this?<br> Is it like this?<br> </p> <pre class="prettyprint">protected void gridAMDisplay_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //find the Label control using FindControl and set its Text on some column value DataRowView row = (DataRowView)e.Row.DataItem; if (row[&quot;ModuleLeader&quot;].ToString() == &quot;ModuleLeader&quot;) { Label lbl = e.Row.FindControl(&quot;Label1&quot;) as Label; if (lbl != null) { lbl.Text = &quot;ML&quot;; } else if (lbl = null) { !DBNull.Value.Equals(row[&quot;ModuleLeader&quot;]); } } } }</pre> <p><br> </p> <p>But by doing this way, I've got another error.<br> 1)&nbsp;Cannot implicitly convert type 'System.Web.UI.WebControls.Label' to 'bool'<br> So, would like to ask, is it by doing else if statement, it becomes boolean?</p> <p>2)<span class="Apple-style-span" style="white-space:pre">&nbsp;</span>Only assignment, call, increment, decrement, and new object expressions can be used as a statement<br> And for this error, I really dont understand what it is saying.</p> 2009-09-10T02:35:46-04:003396631http://forums.asp.net/p/1468326/3396631.aspx/1?Re+Gridview+Adding+ColumnsRe: Gridview - Adding Columns <p>what I meant was simply this: IF you're expecting the database to have null values for the table column 'ModuleLeader' then it would be needed to first check for null and then do a compare (as a .ToString() would result in an error) </p> <p>if you're not sure, at present you may start with this:</p> <p><pre class="prettyprint">protected void gridAMDisplay_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //find the Label control using FindControl and set its Text on some column value DataRowView row = (DataRowView)e.Row.DataItem; if (row[&quot;ModuleLeader&quot;].ToString() == &quot;ModuleLeader&quot;) { Label lbl = e.Row.FindControl(&quot;Label1&quot;) as Label; if (lbl != null) { lbl.Text = &quot;ML&quot;; } } } }</pre><br> <br> </p> 2009-09-10T02:55:47-04:00