?
Solved

Customize Gridview header

Posted on 2006-03-31
10
Medium Priority
?
985 Views
Last Modified: 2008-01-09
Is it possible to customize the gridview header row to the extent of putting a template field with a panel full of controls in it?  If so, how might I access that row for that purpose?
0
Comment
Question by:codequest
  • 6
  • 4
10 Comments
 
LVL 3

Accepted Solution

by:
spdude earned 1500 total points
ID: 16348610
Hi codequest

      I don’t think gridview is that flexible if you need a flexible control then try datalist instead of gridview. you can customize the header of datalist by clicking on the smart tags (small play button on top right of control) .and select header template

<asp:DataList ID="DataList1" runat="server" DataKeyField="empid" DataSourceID="AccessDataSource1"
                RepeatDirection="Horizontal" RepeatLayout="Flow" OnItemDataBound="DataList1_ItemDataBound">
               
<HeaderTemplate>
                    <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
                        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></asp:Panel>
                    <table>
                        <tr>
                            <td>empid:</td>
                            <td>name:</td>
                            <td>age:</td>
                            <td>dob:</td>
                            <td>salary:</td>
                        </tr>
                    </table>
                </HeaderTemplate>

 <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <asp:Label ID="empidLabel" runat="server" Text='<%# Eval("empid") %>'></asp:Label></td>
                            <td>
                                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
                            <td>
                                <asp:Label ID="ageLabel" runat="server" Text='<%# Eval("age") %>'></asp:Label></td>
                            <td>
                                <asp:Label ID="dobLabel" runat="server" Text='<%# Eval("dob") %>'></asp:Label></td>
                            <td>
                                <asp:Label ID="salaryLabel" runat="server" Text='<%# Eval("salary") %>'></asp:Label></td>
                        </tr>
                    </table>
                </ItemTemplate>
                <AlternatingItemStyle BackColor="Aqua" Font-Bold="False" Font-Italic="False" Font-Overline="False"
                    Font-Strikeout="False" Font-Underline="False" />
               
            </asp:DataList>


To acccess the contorls in header try following code


protected void Button1_Click(object sender, EventArgs e)
    {
       ((Label) DataList1.Controls[0].FindControl("label1")).Text="Hello Snehal";
    }
 

Regard
spdude
0
 
LVL 2

Author Comment

by:codequest
ID: 16351088
Thanks for input.   I'll investigate and get back to you.
0
 
LVL 2

Author Comment

by:codequest
ID: 16356677
spdude:  Actually, I found gridview has the same capability.   In the gridview smarttag, choose edit templates, and choose template header.   That starts it, and you can either edit there or directly in source.  

Since you got me started, if you'll respond acknowledging this post, I'll accept that as the answer.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:spdude
ID: 16390126
Hi codequest

I tried your suggestion but I was not able to find header template for gridview. I don’t know how you got template header in edit templates.

But I you still wanna use grid view then I got one more trick
Make the ShowHeader="false" FOR gridview
Take a table add 2 rows in first row add the column header with panel
And in second row put the grid view


Good luck
spdude
      
      
0
 
LVL 2

Author Comment

by:codequest
ID: 16395212
Try

>  new asp.net website
>  new webform
>  add gridview
>  add new column > template field
>  add element to template field  (or not, didn't check)
>  then in template editing
    >  gridview tasks
    >  template editing mode
    >  display
        >  dropdown shows header template

the dropdown didn't show the header template until I added the new template column

0
 
LVL 3

Expert Comment

by:spdude
ID: 16403059

hey codequest

thanks for showing me the exact steps

i just love this site

what to do you think of my table solution ?


0
 
LVL 2

Author Comment

by:codequest
ID: 16403342
dude:  ditton on the site.  Table solution is good;  I started building my rows in vb.net code behind, though, and I'm hooked!
0
 
LVL 3

Expert Comment

by:spdude
ID: 16417347
can u paste some of your code ?
0
 
LVL 2

Author Comment

by:codequest
ID: 16419156
Mon pleasure.  Here's a simplified example (and note on why not perfect, yet)

http://forums.asp.net/thread/1252566.aspx

Luck!
0
 
LVL 2

Author Comment

by:codequest
ID: 16419266
Here's the setup in the datarowbound event for the gridview

Protected Sub XYZGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles XYZGridView.RowDataBound

        Select Case e.Row.RowType
            Case DataControlRowType.DataRow              
                'get the row
                Dim wrkRow As DataRowView = CType(e.Row.DataItem, DataRowView)
               '  find the panel, and get it's controls  

                Dim wrkPanel As Panel = e.Row.Cells(0).Controls(0).FindControl("XZYPanel")
                Dim wrkPanelControls As ControlCollection = wrkPanel.Controls

                '  instatiate the build menu class
                Dim exeControls As New MyRowControls   ' a custom class

                ' put the row fields that will be used in the data structure  
               Dim gRow As New RowStructure
                exeControls.FillRowStructure(gRow, wrkRow)

                '  build the row
                exeControls.BuildRow(gRow, wrkPanelControls)

            Case DataControlRowType.Header
                (similar stuff, though no data structure is needed because the header row is built from static info)

The BuildRow method sorts out several cases, but eventually gets to the "add control" code cited in the message above.

RowStructure is a data structure I built so I can use the intellisense in the called methods, since DataRowView can't be typed.   I'm sure it's going to cost me when I scale up to 10,000,000 users.... :o)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

840 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