GridView has a List<> Collection as DataSource but does not view the collection content

Last post 07-05-2009 7:48 AM by bmwz9. 1 replies.

Sort Posts:

  • GridView has a List<> Collection as DataSource but does not view the collection content

    07-05-2009, 6:16 AM
    • Member
      2 point Member
    • deepSea
    • Member since 04-12-2008, 6:12 PM
    • Posts 16

     hello every body ... and thanx for yor time...

    i have a gridview that it takes its List<> Collection datasource at runtime but does not view the collection content if the property:

    AutoGenerateColumns assigned to "False"

    the code is:

     

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating" Width="210px">
            <Columns>
                <asp:BoundField HeaderText="product id" />
                <asp:BoundField HeaderText="name" />
                <asp:CommandField ButtonType="Button" EditText="Change" InsertVisible="False"
                    ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
            <EmptyDataTemplate>
                Your Cart Is Empty.
            </EmptyDataTemplate>
            <HeaderStyle BackColor="#8AB82E" ForeColor="White" />
        </asp:GridView>

     

    and

     

    public partial class Default2 : System.Web.UI.Page
    {
        ShoppingCart cart;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["cart"] == null)
            {
                cart = new ShoppingCart();
                Session["cart"] = cart;
            } 
            else
            {
                cart = (ShoppingCart)Session["cart"];
            }
    
            GridView1.DataSource = cart.CartItems;//here CartItems is a List<CartItem>
    
            //GridView1.AutoGenerateColumns = true; //if i uncomment this line that solve the problem partialy ... cuz all collumns of that collection will appear
            
            //if (!Page.IsPostBack)
                GridView1.DataBind();


    any help is approciated ...

     

  • Re: GridView has a List<> Collection as DataSource but does not view the collection content

    07-05-2009, 7:48 AM
    Answer
    • Participant
      1,262 point Participant
    • bmwz9
    • Member since 10-04-2007, 11:34 PM
    • Egypt
    • Posts 186

    Set DataField property of columns as

    <Columns>
                <asp:BoundField HeaderText="product id" DataField="ProductID"  />
                <asp:BoundField HeaderText="name" DataField="Name" />
                <asp:CommandField ButtonType="Button" EditText="Change" InsertVisible="False"
                    ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>


    Mohamed Abb@s



    ------------------------

    Please remember to click “Mark as Answer” on the post(s) which helps you !
Page 1 of 1 (2 items)