Reputation: 135
I have created an array of lists. The array has seven rows who represents the days of week and the lists contain the available slots for doctor appointments. I am trying to bind it with a GridView
or a DataList
(whatever is more appropriate) without success.
I have declared the list:
List<string>[] list=new List<string>[7]; //An array of 7 lists
for (int i = 0; i < 7; i++)
{
list[i]=new List<string>();
}
I have filled in the lists with strings that represent the available slots for appointments with a doctor.
The result that I want to achieve is the availability for one of the doctors as it is depicted at this site: link
Upvotes: 2
Views: 5186
Reputation: 135
Thank you very much. It was really helpful, I spent some time trying to understand the logic behind this transformation and as a newbie I end up with a little angler piece of code:
private System.Data.DataTable CreateDataTable(List<string> columnDefinitions, List<List<string>> rows)
{
DataTable table = new DataTable();
foreach (string colDef in columnDefinitions)
{
DataColumn column;
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = colDef;
table.Columns.Add(column);
}
for (int i = 0; i < rows[0].Count; i++)
{
table.Rows.Add(rows[0][i], rows[1][i], rows[2][i], rows[3][i], rows[4][i], rows[5][i], rows[6][i]);
}
return table;
}
private List<string> GetColDefsFromBackend()
{
List<string> cols = new List<string>();
cols.Add("Monday");
cols.Add("Tuesday");
cols.Add("Wednesday");
cols.Add("Thursday");
cols.Add("Friday");
cols.Add("Saturday");
cols.Add("Sunday");
return cols;
}
Upvotes: 1
Reputation: 7062
Turn the List into a datatable and then bind. Array of array's are little complicated for binding clearly.
Upvotes: 0
Reputation: 102448
You could change the array to a List<List<string>>
like this:
List<List<string>> list = new List<List<string>>();
Then you can bind it to a GridView
in the following way (just adapt to your case):
protected void Page_Load(object sender, EventArgs e)
{
List<string> cols = GetColDefsFromBackend();
List<List<string>> rows = GetDataFromBackend();
GridView1.DataSource = CreateDataTable(cols, rows);
GridView1.DataBind();
}
private System.Data.DataTable CreateDataTable(List<string> columnDefinitions, List<List<string>> rows)
{
DataTable table = new DataTable();
foreach (string colDef in columnDefinitions)
{
DataColumn column;
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = colDef;
table.Columns.Add(column);
}
// Create DataRow and Add it to table
foreach (List<string> rowData in rows)
{
DataRow row = table.NewRow();
// rowData is in same order as columnDefinitions
for (int i = 0; i < rowData.Count; i++)
{
row[i] = rowData[i];
}
table.Rows.Add(row);
}
return table;
}
/// <summary>
/// Simulates a StoredProcedureCall which returns
/// the data in a List with Lists of strings
/// </summary>
private List<List<string>> GetDataFromBackend()
{
List<List<string>> myData = new List<List<string>>();
myData.Add(Row(1));
myData.Add(Row(2));
myData.Add(Row(3));
return myData;
}
private List<string> Row(int p)
{
List<string> row = new List<string>();
for (int i = 0; i < 4; i++)
{
row.Add(string.Format("Column {0}/{1}", p, i));
}
return row;
}
private List<string> GetColDefsFromBackend()
{
List<string> cols = new List<string>();
cols.Add("Col1");
cols.Add("Col2");
cols.Add("Col3");
cols.Add("Col4");
return cols;
}
Source: Use List<List<string>>
as GridView - DataSource
Upvotes: 1