tags:

views:

561

answers:

1

We've got a number of gridviews that we're populating with data from an ADABAS datasource via some middleware that, unfortunately, gives us back lots of arrays of stringBuilder that we turn into a datatable, set as the datasource for a gridview, then bind.

It's convoluted, but I'm in a situation where I need to be able to update the gridview (say, drop a row), without going back to the mainframe, deleting the row, and regenerating the datatable, as I would if we had an Oracle datasource.

So, I'm trying to hack something together where on serverside I extract the data from the gridview, build a datatable, delete the given row, then use that datatable as the datasource for the gridview, and rebind.

I can do it on a case by case basis, so long as I know the layout of the gridview. But we're going to be doing this over and over on this current project, I'm trying to figure out a way to abstract it so that I can use it for any gridview I throw at it.

The problem I'm getting is that most of the cols in the gridview are templatefields with asp:labels, where the data is put in with the databinder.eval thing:

<asp:TemplateField HeaderText="Code">
    <ItemTemplate>
        <asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>

So, when building my 'fake' datasource, I need to be able to extract from somewhere in the gridview those column names.

Is that information available in the gridview?

+1  A: 

Yikes. You are going to need to store the fake datasource column names within the GridView along with the column values. One suggestion would be to add a HiddenField along with each fake datasource field value. In the example below, display the code (value) in the Label and the column (name) in which code maps in the HiddenField:

<asp:TemplateField HeaderText="Code"
    <ItemTemplate>
        <asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label>
        <asp:HiddenField ID="hfCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "codefield")%>'></asp:HiddenField>
    </ItemTemplate>
</asp:TemplateField>

It's ugly, but the column names will always be available to you -- no matter what they might be. Best of luck.

Ben Griswold
Thanks bro. Makes since - since I'm having to rig it to start with, seems reasonable that I'll have to go whole hog like that.Thanks again.
aape
It's an interesting problem. I glad you submitted it and I'm happy to help.
Ben Griswold