dyarosh
asked on
Can't set the Header Text in my GridView in ASP.NET C#
I am using ASP.NET 4.0 C# and am trying to programmatically set the Header Text for the GridView on my page. I am getting an Index was out of range error and can't figure out what I am doing wrong. Here is the GridView on my page:
And here is the code where I am trying to set the HeaderText:
dtWarnings is a datatable with 3 columns in it.
Any help is greatly appreciated!
<p><b>Warnings</b></p>
<asp:GridView ID="gvWarnings" runat="server">
</asp:GridView>
And here is the code where I am trying to set the HeaderText:
gvWarnings.DataSource = dtWarnings;
gvWarnings.CellPadding = 3;
gvWarnings.CellSpacing = 3;
gvWarnings.Columns[0].HeaderText = "Warning";
gvWarnings.Columns[1].HeaderText = "Date";
gvWarnings.Columns[2].HeaderText = "Title";
gvWarnings.DataBind();
dtWarnings is a datatable with 3 columns in it.
Any help is greatly appreciated!
You might have null records in your query! I would recommand that you check null in your coding - put exception or something.
ASKER
The table definitely has data. It displays on the screen if I comment out the code where I am trying to set the Header Column Names.
I know the table has data and you might have one missing records out from your table! Have you check all individual rows using SQL script?
what is your dtWarnings looks like in code behind or SQL script/stored procedure? The index error meant that you either reference wrong columns or with some missing data!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here is the SQL:
sql = "SELECT WarningType, WarningDate, TitleDesc " +
"FROM EMP_Warnings " +
"JOIN EMP_WarningType ON EMP_WarningType.WarningTyp eID = EMP_Warnings.WarningTypeID " +
"WHERE EmployeeID = " + employeeID.ToString();
sql = "SELECT WarningType, WarningDate, TitleDesc " +
"FROM EMP_Warnings " +
"JOIN EMP_WarningType ON EMP_WarningType.WarningTyp
"WHERE EmployeeID = " + employeeID.ToString();
ASKER
I ended up setting the column headings in the RowDataBound event. Thanks for the help.
Here is how I coded my datagrid!
---------------------- aspx page ---------------------- (UserName from database column)
<tr>
<td>
<asp:datagrid id="dgTest" runat="server" OnPageIndexChanged="dgTest _OnPageInd exChanged" >
<asp:TemplateColumn HeaderText="Name">
<ITEMTEMPLATE>
<%#DataBinder.Eval(Contain er.DataIte m,"UserNam e")%>
</ITEMTEMPLATE>
</asp:TemplateColumn>
</asp:datagrid>
</td>
</tr>
------------------------- code behind page ------------------------
public void dgTest_OnPageIndexChanged( Object sender, DataGridItemEventArgs e)
{
---- open db connection ---
.....
---- select sql syntax here
}
---------------------- aspx page ---------------------- (UserName from database column)
<tr>
<td>
<asp:datagrid id="dgTest" runat="server" OnPageIndexChanged="dgTest
<asp:TemplateColumn HeaderText="Name">
<ITEMTEMPLATE>
<%#DataBinder.Eval(Contain
</ITEMTEMPLATE>
</asp:TemplateColumn>
</asp:datagrid>
</td>
</tr>
------------------------- code behind page ------------------------
public void dgTest_OnPageIndexChanged(
{
---- open db connection ---
.....
---- select sql syntax here
}
or look at the msdn example!
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatecolumn(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatecolumn(v=vs.71).aspx