asp.net - Editable gridview based on list -
is possible create gridview based on list? have following list:
id = 1 name = john zip = 33141 id = 2 name = tim zip = 33139
i want able create editable gridview list
when bind grid view, seems put everyting in 1 column, , can't figure out how seperate different columns
here code setting datasource
of gridview
:
datatable table = convertlisttodatatable(personlist); gridview1.datasource = table; gridview1.databind(); static datatable convertlisttodatatable(list<string> list) { // new table. datatable table = new datatable(); // max columns. int columns = 7; // add columns. (int = 0; < columns; i++) { table.columns.add(); } // add rows. foreach (var rd in list) { table.rows.add(rd); } return table; }
here example:
private class person { int m_iid; string m_sname; string m_szip; public int id { { return m_iid; } } public string name { { return m_sname; } } public string zip { { return m_szip; } } public person(int iid, string sname, string szip) { m_iid = iid; m_sname = sname; m_szip = szip; } } private list<person> m_people; private void convertlisttodatatable(list<person> people) { datatable table = new datatable(); datacolumn col1 = new datacolumn("id"); datacolumn col2 = new datacolumn("name"); datacolumn col3 = new datacolumn("zip"); col1.datatype = system.type.gettype("system.string"); col2.datatype = system.type.gettype("system.string"); col3.datatype = system.type.gettype("system.string"); table.columns.add(col1); table.columns.add(col2); table.columns.add(col3); foreach (person person in people) { datarow row = table.newrow(); row[col1] = person.id; row[col2] = person.name; row[col3] = person.zip; table.rows.add(row); } gridview1.datasource = table; gridview1.databind(); }
Comments
Post a Comment