Display dynamic data using GridView/DataList
I need to build a form that shows data is the following format:
M(1 Nov), T(4 Nov), S(7 Nov) etc
user 1 yes/no yes/no yes/no yes/no
user 2 yes/no yes/no yes/no yes/no
The yes/no will be represented by a check bo开发者_开发技巧x and there will be Monday, Thursday and Sunday columns for each occurrence within the month - dynamically worked out. I was planning on using Anonymous Type's as explained here to represent the data. Is this a sound approach? My problem is how to display this using a GridView or DataList control. If the data structure is dynamic, how can I configure the GridView/DataList control to bind to the data?
Is there a better approach to what I'm trying?
Use a DataTable stored in the ViewState to carry the data and bind to a GridView / DataList.
public DataTable MyDynamicDataTable {
get { return (DataTable)ViewState["MyDynamicDataTable"]; }
set { ViewState["MyDynamicDataTable"] = value; }
}
...
public void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
MyDynamicDataTable = BuildMyDynamicDataTable();
MyGridView1.DataSource = MyDynamicDataTable;
MyGridView1.DataBind();
}
}
...
public DataTable BuildMyDynamicDataTable() {
DataTable dt = new DataTable();
foreach (obj in myDayCollection) {
dt.Columns.Add(...);
}
foreach (obj in myCollection) {
dt.Rows.Add(...);
}
return dt;
}
When the user makes changes, it should persist to the DataTable structure. Once the changes are complete and successfully validated, you can simply foreach (DataRow in MyDynamicDataTable) to process the selections that the user made and persist them to database, or whatever.
精彩评论