Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AutoGenerated DataGrid Column Headers Issue

Status
Not open for further replies.

LittlBUGer

Programmer
Apr 26, 2006
81
US
Hello. I've been trying to capture the column header information in my autogenerated datagrid all day and nothing I try gets any information at all. I can easily change each header, but the data coming from our database, where each column header is created dynamically, I just can't seem to capture it. I've tried things in both the ItemDataBound and ItemCreated events and still no luck. I usually use BoundColumns so I'm not familiar if there is a trick to do this. Is there a concrete fool-proof way to get the header for each column in an autogenerated datagrid in asp.net 1.1? I'm willing to try anything. Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Not sure what you mean, what info do you want, the column header text?
 
what do you mean get the header?
in the RowDataBound event you can check the rowtype
Code:
switch(e.Row.RowType)
{
   case RowType.Header:
     e.Row.Cells[x];
     break;
   case RowType.Footer:
     break;
   case RowType.Pager:
     break;
   case RowType.Item:
     break;
   case RowType.AlternateItem:
     break;
}
or do you mean the datatable header information
Code:
foreach (DataColumn c in DataSet.Tables[x].Colummns)
{
    c.ColumnName;
    c.Type;
    c.Expression;
    ...
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yes, I mean the column header text. I'm binding the datagrid from data in a database. Basically it's as simple as:

Code:
newquery = "SELECT * FROM Display_AR5 order by " & strSortField & " " & strSortDirection
cmdSelect = New SqlCommand( newquery, sqlconn )
dispreport5a.DataSource = cmdSelect.ExecuteReader()
dispreport5a.DataBind()

Then on one of the datagrid events I'm trying to grap the column header text information but haven't been able to do so thus far. Does this help? Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
this will write the text of each cell in the header to the output stream
Code:
protected void GridView_OnDataBound(....)
{
   foreach (TableCell cell in myGrid.HeaderRow.Cells)
   {
      Response.Write(cell.Text);
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
That doesn't work. Please realize I'm using ASP.NET 1.1 for this particular code. I'm using a DataGrid, not a GridView. Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
If you are using a DataGrid, you'll have to slightly tweak Jason's code but the logic still remains the same e.g.
Code:
[Blue]Protected[/Blue] [Blue]Sub[/Blue] DataGrid1_ItemDataBound([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.Web.UI.WebControls.DataGridItemEventArgs) [Blue]Handles[/Blue] DataGrid1.ItemDataBound
        [Blue]If[/Blue] e.Item.ItemType = ListItemType.Header [Blue]Then[/Blue]
            [Blue]For[/Blue] i [Blue]As[/Blue] [Blue]Integer[/Blue] = 0 [Blue]To[/Blue] e.Item.Cells.Count - 1
                Response.Write(e.Item.Cells(i).Text)
            [Blue]Next[/Blue]
        [Blue]End[/Blue] [Blue]If[/Blue]
    [Blue]End[/Blue] [Blue]Sub[/Blue]



____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
No, I'm sorry. That does not work either. That was one of the first things I tried. There's no data or something or the grid hasn't been bound yet, I don't know. I know the headers are there somewhere, but that code does not reveal them. Thanks for the help. Any other suggestions? :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
what do you mean there is no data in the header? you mean the name of the column, or data retrieved from the db.

Data retrieved from the db will not be in the header row. just as it won't be in the footer row. all you have access to is the current row (1 record if an item or alternating item).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
No, no, sorry. I was rambling there about why it might not be working. Since it's grabbing the data from a database and binding the data with the datagrid property autogenerated set to true, it makes the column headers the same as they are in the database (SQL Server 2000). Meaning it's taking the database field names and putting them in as my datagrid headers. So, it's grabbing those names from the database and putting them into my datagrid. Now do I grab that information now then? It must be located somewhere, otherwise it wouldn't show up in the datagrid correct, or no? Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
I'm not sure what you are asking. The code I posted has been tested and works fine so there is something else you are doing wrong. However, just to prove that it works, try this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataGrid ID="DataGrid1" runat="server">
        </asp:DataGrid>
    </div>
    </form>
</body>
</html>
Code:
[Blue]Protected[/Blue] [Blue]Sub[/Blue] Page_Load([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] [Blue]Me[/Blue].Load
        [Green]' Check for a postback  [/Green]
        [Blue]If[/Blue] Not Page.IsPostBack [Blue]Then[/Blue]
            [Green]' Bind the DataGrid with some sample data  [/Green]
            DataGrid1.DataSource = GetData()
            DataGrid1.DataBind()
        [Blue]End[/Blue] [Blue]If[/Blue]

    [Blue]End[/Blue] [Blue]Sub[/Blue]

    [Blue]Private[/Blue] [Blue]Function[/Blue] GetData() [Blue]As[/Blue] DataTable
        [Green]' Declarations  [/Green]
        [Blue]Dim[/Blue] dt [Blue]As[/Blue] [Blue]New[/Blue] DataTable
        [Blue]Dim[/Blue] dr [Blue]As[/Blue] DataRow

        [Green]' Add some columns  [/Green]
        dt.Columns.Add([Red]"Column1"[/Red])
        dt.Columns.Add([Red]"Column2"[/Red])

        [Green]' Add some test data  [/Green]
        [Blue]For[/Blue] i [Blue]As[/Blue] [Blue]Integer[/Blue] = 0 [Blue]To[/Blue] 10
            dr = dt.NewRow
            dr([Red]"Column1"[/Red]) = i
            dr([Red]"Column2"[/Red]) = [Red]"Some Text "[/Red] & i
            dt.Rows.Add(dr)
        [Blue]Next[/Blue]

        [Green]' Return the DataTable  [/Green]
        [Blue]Return[/Blue] dt
    [Blue]End[/Blue] [Blue]Function[/Blue]

    [Blue]Protected[/Blue] [Blue]Sub[/Blue] DataGrid1_ItemDataBound([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.Web.UI.WebControls.DataGridItemEventArgs) [Blue]Handles[/Blue] DataGrid1.ItemDataBound
        [Blue]If[/Blue] e.Item.ItemType = ListItemType.Header [Blue]Then[/Blue]
            [Blue]For[/Blue] i [Blue]As[/Blue] [Blue]Integer[/Blue] = 0 [Blue]To[/Blue] e.Item.Cells.Count - 1
                Response.Write(e.Item.Cells(i).Text)
            [Blue]Next[/Blue]
        [Blue]End[/Blue] [Blue]If[/Blue]
    [Blue]End[/Blue] [Blue]Sub[/Blue]


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Well, you are using a DataTable to fill your datagrid. I'm binding mine directly from the database so it's pulling the headers from the database fields. If you do it your way, sure it works fine, but since I'm using a different method to "fill" my datagrid, it does not work. Does anyone have any suggestions for my situation? Thanks. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
It makes no difference what data I put in my GetData method or where I retrieve it from. I could put a call to my database in instead and it will still work exactly as it does above.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
LittlBUGer, i'd suggest you research the technology your trying to use. a datagrid can bind to any IEnumerable object; DataTable, ArrayList, SqlDataReader, string[].

try filling a datatable with the results from the datareader and bind the datatable to the gridview.

if your going to get arrogant about the subject at least have the knowledge to back it up.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I'm sorry, I'm not trying to be arrogant or rude or anything at all. I'm just saying that with my datagrid (NOT gridview), the e.item.cells thing does not work, no matter what I've tried with it. I've tried doing it that way many times with no luck, hence why I thought it was with how I was binding my data. If it really doesn't matter how I bind the data, then that's my bad for saying it did and now that makes me completely stumped on what's going on or what to do. :(

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
ok, stepping back from the cells problem...
does your sql query pull the correct data?
could you define the columns manually instead of auto generating?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Did you try my example? Did it work? If so, try putting your database code inside of my GetData function (rather than creating the test data like I have done). Once you have that working, go back to your application and see what you have done wrong.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Yes, the SQL query does pull the correct data as I have the generated datagrid and it's correct. I can even look at the table in the database that I'm pulling from and the information is correct. The problem is I can't define anything manually (unless you mean something else...) as the table in the database is creating dynamically by a stored procedure. It could have 10 columns, it could have 100, it all depends. This is why I'm having issues as I've rarely had to mess with a "dynamic" datagrid before and haven't ran into issues like this before. Any other ideas? Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
@ca8msm
So you want me to add the following code to your GetData function and have my datagrid bind from that?

Code:
newquery = "SELECT * FROM Display_AR5 order by " & strSortField & " " & strSortDirection
cmdSelect = New SqlCommand( newquery, sqlconn )

I guess I can give it a try... :) Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top