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.”


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


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:


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.