SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    East of Ottawa, ON
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Gridview boundfield

    How do you programmatically bind a gridview with a generic list?

    I have the following code, I'm trying to get the bound the fields in the for each but it gets to the databind at the end I get an error.

    "A field or property with the name 'Smith' was not found on the selected data source."

    Code HTML4Strict:
    <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" EmptyDataText="No Data">
            <Columns>
            </Columns>
    </asp:GridView>

    Code Csharp:
    if (!IsPostBack)
            {
                List<names> ln = new List<names>();
                dbconnect n = new dbconnect();
     
                ln = n.ReadDataNames();
     
                BoundField fname = new BoundField();
                fname.HeaderText="First Name";
     
                BoundField lname = new BoundField();
                lname.HeaderText = "Last Name";
     
                foreach(names nm in ln)
                {
                    fname.DataField = nm.firstName;
                    lname.DataField = nm.lastName;  
     
                }
     
                gv.Columns.Add(fname);
                gv.Columns.Add(lname);
     
                gv.DataSource = ln;
                gv.DataBind();
            }

  2. #2
    SitePoint Guru pinch's Avatar
    Join Date
    Mar 2005
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geargolum View Post
    How do you programmatically bind a gridview with a generic list?

    I have the following code, I'm trying to get the bound the fields in the for each but it gets to the databind at the end I get an error.

    "A field or property with the name 'Smith' was not found on the selected data source."

    Code HTML4Strict:
    <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" EmptyDataText="No Data">
            <Columns>
            </Columns>
    </asp:GridView>

    Code Csharp:
    if (!IsPostBack)
            {
                List<names> ln = new List<names>();
                dbconnect n = new dbconnect();
     
                ln = n.ReadDataNames();
     
                BoundField fname = new BoundField();
                fname.HeaderText="First Name";
     
                BoundField lname = new BoundField();
                lname.HeaderText = "Last Name";
     
                foreach(names nm in ln)
                {
                    fname.DataField = nm.firstName;
                    lname.DataField = nm.lastName;  
     
                }
     
                gv.Columns.Add(fname);
                gv.Columns.Add(lname);
     
                gv.DataSource = ln;
                gv.DataBind();
            }
    The DataField property of the columns should match one of the properties in your 'name' collection. However, it looks like you're loading multiple values in each of the DataField. You should probably have something like:

    Code ASP:
    BoundField fname = new BoundField();
                fname.HeaderText="First Name";
                fname.DataField = "FirstName";
     
                BoundField lname = new BoundField();
                lname.HeaderText = "Last Name";
                lname.DataField = "LastName";

    assuming of course 'FirstName' and 'LastName' are properteis of the 'names' class.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •