简体   繁体   中英

DataBind and DataSource on a Gridview

I have done gridviews before, where I bind all SQL data from data safe reader or specify column names and binds at the javascript level. This is a bit different and I am a bit stumped on how to proceed.

I have a business class that handles all SQL data queries.

This class is called to Fetch a LIST. This list contains collections and child collections.

var _InfoList = BusinessObjectClass.Get(_CriteriaKey);

I can access the list as such:-

Txtbox1.Text = _InfoList.ID#.ToString();

Now I am trying to bind one of the collections in the LIST to a gridview.:-

C#:-

gvwMembers.DataSource = _InfoList.Members;
gvwMembers.DataBind();

Where Members is a collection...

But this syntax doesn't add any thing to the gridview...The gridview is empty.

Second methodology:-

I also tried doing something like this:-

 List<BusinessObjectClass> Members = new List<BusinessObjectClass>();
 Members = _InfoList.Members;

 gvwVendors.DataSource = Members;
 gvwVendors.DataBind();

But to no avail.. this is because the problem lies in the 2nd statement:-

Members = _InfoList.Members .... this is not a valid assignment...can anyone help with this?

After fleshing out some details in the comments, it is perfectly fine to have a simple GridView with no columns defined, but make sure AutoGenerateColumns is not false. The default is true. This will create a column for you, based on each property of the object being bound.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>

And in order to pick and choose which properties to display, define them in the <Columns> section.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Property1" HeaderText="Property1" />
        <asp:TemplateField HeaderText="Property2">
            <ItemTemplate>
                <asp:Label ID="lblProperty2" runat="server" Text='<%# Eval("Property2") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM