0

I have made a web app which query's a database and returns the results in a Datalist.

The user can specify which columns to return. For the Datalist I have done this:

        <asp:DataList runat="server" ID="list">
        <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, columns[0])%>
            &nbsp &nbsp &nbsp
            <%# DataBinder.Eval(Container.DataItem, columns[1])%>
            &nbsp &nbsp &nbsp
            <%# DataBinder.Eval(Container.DataItem, columns[2])%>
            &nbsp &nbsp &nbsp
            <%# DataBinder.Eval(Container.DataItem, columns[3]) %>
            &nbsp &nbsp &nbsp
            <%# DataBinder.Eval(Container.DataItem, columns[4]) %>
            &nbsp &nbsp &nbsp
            <%# DataBinder.Eval(Container.DataItem, columns[5]) %>
        </ItemTemplate>
    </asp:DataList>

This would work if the user selected 7 columns to return it would only show 6 and if the user selected 5 columns to return it would throw a ArgumentOutOfRangeExpection.

Is there a way of putting a if statement to check if it needs that column ?

Thanks

user1826413
  • 133
  • 1
  • 12

1 Answers1

0

When binding data to a datalist/gridview/repeater its easier to just create as list of objects and bind that to the control. Here's an example using a gridview:

<asp:GridView ID="GridView1" runat="server" AllowSorting="False"
  AutoGenerateColumns="false" BackColor="White" 
  BorderWidth="2px" BorderStyle="Solid"
  CellPadding="4" ForeColor="#333333" GridLines="both" 
  EmptyDataText="No Log Messages">

     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
     <Columns>                                  
       <asp:TemplateField Visible="false" ItemStyle-HorizontalAlign="Center" HeaderText="ID" HeaderStyle-ForeColor="white">
         <ItemTemplate>
            <asp:Label ID="lblId" runat="Server" Text=' <%#Eval("ID")%>' />
             </ItemTemplate>
        </asp:TemplateField>  
<asp:TemplateField Visible="false" ItemStyle-HorizontalAlign="Center" HeaderText="Address" HeaderStyle-ForeColor="white">
         <ItemTemplate>
            <asp:Label ID="lblAddress" runat="Server" Text=' <%#Eval("Address1")%>' />
             </ItemTemplate>
        </asp:TemplateField>                   
     </Columns>
 </asp:GridView>

This example creates a table with two columns. Each column is expecting a coresponding data source to have columns with names: ID and Address1. On the server side, you create a list of objects which have columns with these names and you bind the datasource to the control. Like this:

First, create a class with your properties:

Public Class myClassAddress
    Public Property ID  As String
    Public Property Address1  As String
End Class

Now, in the code behind where you have the gridview, instantiate that class and fill it:

    Dim myList As New List(Of myClassAddress)
    Dim newClass As myClassAddress

    newClass = New myClassAddress
    newClass.Address1 = "some address"
    newClass.ID = "1"
    myList.Add(newClass)

    newClass = New myClassAddress
    newClass.Address1 = "some address2"
    newClass.ID = "12"
    myList.Add(newClass)

Of course, in this example I am just hard coding the values. You can fill a class with data from a database, xml file, etc. The above example creates two instance of your myClassAddress class. It also creates a list of these classes and adds each instantiation to that list. Now, just bind the list to your control:

me.gridView1.datasource = myList
me.gridview1.bindData()

I know this may seem like a lot but once you get use to it, it's the easiest way to bind data to these types of controls. If you have any questions, let me know.

jason
  • 3,821
  • 10
  • 63
  • 120
  • You code gave me a bunch of errors, so I changed it a bit to <% foreach (var c in columns){ %><%# DataBinder.Eval(Container.DataItem, c) %><% } %> but i need to make the data binder use the variable 'c' – user1826413 Mar 05 '13 at 12:50
  • Sorry, I wrote this for VB.Net. Are you getting an error when trying to use C? – jason Mar 05 '13 at 12:56
  • I'm going to edit my response. There's an easier way to bind data to gridviews/repeaters/data lists. – jason Mar 05 '13 at 13:02
  • This is a much more complicated way, and also unable to do this as the name of the columns can change, and the number of columns to be binded to the grid view will change every time, so I am unable to hard code any labels in. – user1826413 Mar 05 '13 at 14:27