0

I am currently redesigning an application for the company I work for. All the data is displayed through a DataGrid, which is fine, except for when I try to incorporate a JQuery library called "Footable" to make all the grids collapse when on a smaller device as explained here. In the tutorial it uses a GridView. I made the demo, it worked well and it's exactly what I want, but I'm having trouble doing the same thing with a DataGrid. My first idea was to switch my DataGrid for a Gridview and implement it as in the demo, but I'd have to rewire everything in the code behind, which is vast, and would be very labour intensive. So I was wondering if it's possible to implement the FooTable JQuery plugin with a DataGrid instead of the GridView

There are only a couple of lines in the code behind that actually trigger the JQuery, but it doesn't seem to work on a DataGrid. Here is what the demo does with the GridView (in C#):

//Attribute to show the Plus Minus Button.
GridView1.HeaderRow.Cells[0].Attributes["data-class"] = "expand";

//Attribute to hide column in Phone.
GridView1.HeaderRow.Cells[2].Attributes["data-hide"] = "phone";
GridView1.HeaderRow.Cells[3].Attributes["data-hide"] = "phone";

//Adds THEAD and TBODY to GridView.
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;

Here is my attempt at it through datagrid (in VB.NET), but no success:

'Attribute to show the Plus Minus Button.
e.Item.Cells(0).Attributes("data-class") = "expand"

'Attribute to hide column in Phone.
e.Item.Cells(2).Attributes("data-hide") = "phone"

'Adds THEAD and TBODY to GridView.
e.Item.TableSection = TableRowSection.TableHeader

From what I have been reading a GridView is basically a super DataGrid with extra attributes, so I'm skeptic if I can actually implement the FooTable plugin with a DataGrid. Any help would be great, I'm not expecting anybody to hold my hand through this, just a point in the right direction.

actaram
  • 2,038
  • 4
  • 28
  • 49
Johnn5er
  • 97
  • 4
  • 15
  • Just to make sure I understand correctly: you're not actually trying to make a working datagrid code work with a gridview, but the other way around? – actaram Jun 11 '15 at 12:41
  • Thanks for the response, originally in the application it uses datagrid, and was wondering could it be swithced to a grid view without re wiring the code behind, to suit the Footable JQuery – Johnn5er Jun 11 '15 at 13:31
  • I'm still confused. Do you want to change the implemented datagrid to a gridview? I thought you were saying you didn't want to do that, since you would have to rewrite the code. What I understood is that you wanted to make the code proposed [here](http://www.aspsnippets.com/Articles/Bootstrap-Responsive-GridView-for-Mobile-Phone-Tablet-and-Desktop-display-in-ASPNet-using-jQuery.aspx) work with your datagrid. If that's the case, I have a solution for you. Is that the case? – actaram Jun 11 '15 at 13:35
  • So sorry if I wasnt being clear. I wasnt sure if i had to rewire code or not, which was part of the question. If you have a solution it would be great BishopBarber :-) – Johnn5er Jun 11 '15 at 13:40
  • Do you mind if I modify your question to make it easier for future people in need to find it? – actaram Jun 11 '15 at 14:38
  • Sure go right ahead, thanks for your time Bishop – Johnn5er Jun 12 '15 at 10:30

1 Answers1

1

Even if it's harder to implement the FooTable plugin with the DataGrid than with a GridView, it does not means it's not possible. If you read a little bit the FooTable introduction page, you realize that this plugin works with the HTML table element. So, no matter the control you are using with whatever technology, as long as it generates a table, it should work.

The code proposed in the above example which works with the Gridview control, renders the following table:

<table class="footable" cellspacing="0" rules="all" border="1" id="MainContent_GridView1" style="border-collapse:collapse;max-width: 500px">
    <thead>
        <tr>
            <th data-class="expand" scope="col">Customer Name</th><th scope="col">Customer Id</th><th data-hide="phone" scope="col">Country</th><th data-hide="phone" scope="col">Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>John Hammond</td><td>1</td><td>United States</td><td>70000</td>
        </tr>
        <tr>
            <td>Mudassar Khan</td><td>2</td><td>India</td><td>40000</td>
        </tr>
        <tr>
            <td>Suzanne Mathews</td><td>3</td><td>France</td><td>30000</td>
        </tr>
        <tr>
            <td>Robert Schidner</td><td>4</td><td>Russia</td><td>50000</td>
        </tr>
    </tbody>
</table>

So let's try to render the same code with the DataGrid. It should be similar to this in your aspx page:

<asp:DataGrid runat="server" ID="dataGrid1" ShowHeader="true" AutoGenerateColumns="false" UseAccessibleHeader="true">
    <Columns>
        <asp:BoundColumn DataField="Name" HeaderText="Customer Name" />
        <asp:BoundColumn DataField="Id" HeaderText="Customer Id" />
        <asp:BoundColumn DataField="Country" HeaderText="Country" />
        <asp:BoundColumn DataField="Salary" HeaderText="Salary" />
    </Columns>
</asp:DataGrid>

A DataGrid must have asp:BoundColumns instead of asp:BoundFields (in the GridView). Also, be sure that ShowHeader="true", other else the DataGrid won't generate your header. UseAccessibleHeader="true" is important as well, because without it, your header cells will be generated as tr instead of th.

The principal difficulty in implementing the header attributes (e.g. data-class= "expand") is accessing your header. But it is feasible. In your code behind, in the Page_Load event, add the following code (after the DataGrid's DataBind):

' That's how you can access to your header
Dim dataGridHeader As DataGridItem = dataGrid1.Controls(0).Controls(0)

' I fyou need to access to your footer, do the following:
' MyGrid.Controls(0).Controls(MyGrid.Controls(0).Con trols.Count - 1)

dataGridHeader.Cells(0).Attributes("data-class") = "expand"

'Attribute to hide column in Phone.
dataGridHeader.Cells(2).Attributes("data-hide") = "phone"
dataGridHeader.Cells(3).Attributes("data-hide") = "phone"

' Make the header row part of a `thead` instead of `tbody` (by default)
dataGridHeader.TableSection = TableRowSection.TableHeader

If you followed all the instructions, you should get the same HTML Table as the one specified earlier. Keep in mind that no matter what you're trying to do with the FooTable plugin, it's all as possible with the DataGrid as it is with the GridView, since both generate an HTML Table, the element aimed by the plugin.

actaram
  • 2,038
  • 4
  • 28
  • 49
  • Bishop thanks very much for your time and effort, im out of the office today, but i will try it on monday and let you know, your a star, regards JC – Johnn5er Jun 12 '15 at 10:44
  • Keep getting error "Specified argument was out of the range of valid values. Parameter name: index for line "Dim datagridHeader As DataGridItem = dgCart.Controls(0).Controls(0)", there are 25 items in the data grid – Johnn5er Jun 12 '15 at 11:05
  • @Johnn5er This error will be thrown if you put that line before the DataSource's bind. Make sure you put it after. – actaram Jun 12 '15 at 11:35