Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can't set the Header Text in my GridView in ASP.NET C#

Posted on 2013-06-24
9
Medium Priority
?
723 Views
Last Modified: 2013-06-25
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:

            <p><b>Warnings</b></p>
            <asp:GridView ID="gvWarnings" runat="server">
            </asp:GridView>

Open in new window


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();

Open in new window


dtWarnings is a datatable with 3 columns in it.  

Any help is greatly appreciated!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 2

Expert Comment

by:eli411
ID: 39273390
You might have null records in your query!  I would recommand that you check null in your coding - put exception or something.
0
 

Author Comment

by:dyarosh
ID: 39273412
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.
0
 
LVL 2

Expert Comment

by:eli411
ID: 39273423
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:eli411
ID: 39273425
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!
0
 
LVL 2

Accepted Solution

by:
eli411 earned 1500 total points
ID: 39273442
0
 

Author Comment

by:dyarosh
ID: 39273482
Here is the SQL:

            sql = "SELECT WarningType, WarningDate, TitleDesc " +
                  "FROM EMP_Warnings " +
                  "JOIN EMP_WarningType ON EMP_WarningType.WarningTypeID = EMP_Warnings.WarningTypeID " +
                  "WHERE EmployeeID = " + employeeID.ToString();
0
 

Author Closing Comment

by:dyarosh
ID: 39275254
I ended up setting the column headings in the RowDataBound event.  Thanks for the help.
0
 
LVL 2

Expert Comment

by:eli411
ID: 39275261
Here is how I coded my datagrid!

---------------------- aspx page ---------------------- (UserName from database column)

<tr>
     <td>
              <asp:datagrid id="dgTest" runat="server" OnPageIndexChanged="dgTest_OnPageIndexChanged">
                   <asp:TemplateColumn HeaderText="Name">
                          <ITEMTEMPLATE>
                                 <%#DataBinder.Eval(Container.DataItem,"UserName")%>
                          </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

}
0
 
LVL 2

Expert Comment

by:eli411
ID: 39275268
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question