Solved

Datagrid to have thead, tbody and tfooter tags to be included in datagrid

Posted on 2010-11-11
10
2,058 Views
Last Modified: 2012-05-10
Datagrid to have thead, tbody and tfooter tags to be included in datagrid.
I will prefer it to be done by a custom datagrid control inherting the asp.net server control.
Imports System.Reflection

Imports System.Web.UI

Imports System.Web.UI.WebControls

Public Class DataGrid
    Inherits System.Web.UI.WebControls.DataGrid


    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)


        Dim table As Table = TryCast(Controls(0), Table)



        If table IsNot Nothing AndAlso table.Rows.Count > 0 Then


            table.Rows(0).TableSection = TableRowSection.TableHeader

            table.Rows(table.Rows.Count - 1).TableSection = TableRowSection.TableFooter


            Dim field As FieldInfo = GetType(WebControl).GetField("tagKey", BindingFlags.Instance Or BindingFlags.NonPublic)



            For Each cell As TableCell In table.Rows(0).Cells


                field.SetValue(cell, HtmlTextWriterTag.Th)

            Next
        End If


        MyBase.OnPreRender(e)
    End Sub

End Class

Open in new window

This code is converted from c# to vb and taken from
http://nmarian.blogspot.com/2007/08/aspnet-datagrid-rendered-with-thead.html

The above code gives me the following error:
Specified argument was out of the range of valid values.
Parameter name: index
Quick reply is appreciated.
0
Comment
Question by:vickytaurus
  • 5
  • 5
10 Comments
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34111626
It sounds like one of the arrays in your code (i.e. either Controls() or Rows()) is throwing an error when you are trying to access an element that doesn't exist.  The code looks like a good conversion from C# to VB. Which specific line of code is actually throwing the error?  
0
 

Author Comment

by:vickytaurus
ID: 34111686
Dim table As Table = TryCast(Controls(0), Table)
Line no 14
0
 

Author Comment

by:vickytaurus
ID: 34113315
I figured it out, the code is causing problems in master page, but the issue now is the <thead></thead> tags are empty.
0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34114126
Right because the DataGrid is designed to to load all of it's data (including header and footer info) into regular <td> row tags inside of the <tbody> tag.  Even though you're adding <thead> and <tfooter> tags, the datagrid is still not looking to add info inside these tags.

What exactly is the result you're trying to accomplish by adding a <thead> and <tfooter> tags to a datagrid?

Also, are you working on a legacy application using the .net 1.1 framework?

If not, I would recommended using the <asp:GridView> control which came out in the .net 2.0 framework as the replacement for the <asp:DataGrid> control.
0
 

Author Comment

by:vickytaurus
ID: 34122442
We have a big application already using datagrid, and now as we are using a new template(css and jquery based) it requires thead, tbody and tfooter to format the table properly.

However if the datagrid control doesnt work then we will use grid view as advised by you.
We use add, edit,delete in datagrid so can you advice if gridview is better control to do that.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34124761
Have you already tried using the attributes "HeaderStyle-CssClass" "FooterStyle-CssClass"?
Even though your CSS template has styles defined for <thead> and <tfooter> you can modify the css template and add your own custom classes that have the exact same styling that is in the thead and tfooter styles.

So for example, on your css template you have:

thead { foo: bar; foo: bar; foo: bar; }
tfooter { foo: bar; foo: bar; foo: bar; }

Then just add your own custom classes to the css template and copy/paste the style:

.gridHeader { foo: bar; foo: bar; foo: bar; }
.gridFooter { foo: bar; foo: bar; foo: bar; }

Then apply the new classes to the DataGrid:

<asp:DataGrid ID="DataGrid1" runat="server" HeaderStyle-CssClass="gridHeader" FooterStyle-CssClass="gridFooter">


As far as switching from DataGrid to GridView, I probably wouldn't if all you're doing is changing the styling.  The GridView does offer more functionality and allows you to code some things faster (as it does some things automatically for you compared to the DataGrid) but I don't think you'd see any real performance boost as far as you're concerned for the user experience on your website.
0
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 500 total points
ID: 34124929
Ok, looks like I was wrong.

You do in fact want to change from DataGrid to GridView for the styling in your case.  One of the major fixes that was added to the GridView was the ability to add <thead> and <tfooter> tags :-)

The change should be too hard.  The GridView control is extremely similar to the DataGrid control, and many of the methods and attributes are exactly the same.  If there is any trouble in your conversion, don't hesitate to ask, but you shouldn't have much trouble finding answers in the mountain of info there is on the GridView control on the internet.

Anyway, have a look at these sites for adding <thead> and <tfooter> to a table rendered by the GridView control:

 - http://madskristensen.net/post/Make-the-GridView-control-accessible.aspx
 - http://www.dotnetcurry.com/ShowArticle.aspx?ID=255
0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34124959
Well let add just one more thing.

Try adding the custom styles to your css template first and see if you can get it to work that way, as this would be less work than converting to GridView.

However, my gut is telling me that the JQuery may be looking for <thead> <tfooter> elements as well, so it may just be safer to convert to GridView.
0
 

Author Comment

by:vickytaurus
ID: 34127005
Thank you for the suggestions and info, i will get back very soon after implementing these.
0
 

Author Comment

by:vickytaurus
ID: 34127093
Thank You very much for your time and help, i am going to use grid view and the above 2 links to make it work .
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 39
ASP.Net Session State alternatives 3 57
Achieve json result 2 65
ASP.net VB.net Load contents of a GridView  to Excel 2 27
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now