I have a variable defined as follows:
Dim iRows As List(Of String())
I've also been converting that to a list of lists just to make it easier to work with.
Dim iRows As List(Of IList(Of String))
I would like to bind that list to a GridView using the contents of the nested array/list to dynamically define the columns. I don't know ahead of开发者_StackOverflow time how many columns there will be, but I do know that they are all the same throughout the list.
I'm just not real sure how to go about it. Thoughts?
Seems to me, that without knowing how your datasource looks like / how many columns there will be, there is no way to put each column into a different gridview column.
That means, fixed markup is not possible => You need to set the AutoGenerateColumns property to true.
You can set the HeaderText of each column by accessing the HeaderCollecion of the GridView, if you want to use a different
Another solution you might be interested in, would be not to use the GridView at all, if you are only interested in displaying data (meaning no edit or delete buttons) You could render your data into a html table with columns and rows, by using Reflection, like aleafonso suggested.
I have done something like this with c# which could possibly help you.
I will have a list of destinations which will be populated to a GridView.
The destination object must be serializable and cannot have nullable values. This is my example:
[Serializable]
public class destination
{
private int idDestination;
public int IDDestination { get; set; }
private string name;
public string Name { get; set; }
private string type;
public string Type { get; set; }
private string ringingTime;
public string RingingTime { get; set; }
private int priority;
public int Priority { get; set; }
private int huntBusy;
public int HuntBusy { get; set; }
public destination() { }
}
Every time you want to populate the GridView you will need to do the following:
GridViewDestination.DataSource = ConvertArrayListToDataTable(listSelectedDestinations);
GridViewDestination.DataBind();
where ConvertArrayListToDataTable is the following:
public static DataTable ConvertArrayListToDataTable(ArrayList arrayList)
{
DataTable dt = new DataTable();
if (arrayList.Count != 0)
{
dt = ConvertObjectToDataTableSchema(arrayList[0]);
FillData(arrayList, dt);
}
return dt;
}
public static DataTable ConvertObjectToDataTableSchema(Object o)
{
DataTable dt = new DataTable();
PropertyInfo[] properties = o.GetType().GetProperties();
if (o.GetType() == typeof(destination))
{
foreach (PropertyInfo property in properties)
{
DataColumn dc = new DataColumn(property.Name);
dc.DataType = property.PropertyType; dt.Columns.Add(dc);
}
}
return dt;
}
private static void FillData(ArrayList arrayList, DataTable dt)
{
foreach (Object o in arrayList)
{
DataRow dr = dt.NewRow();
PropertyInfo[] properties = o.GetType().GetProperties();
if (o.GetType() == typeof(destination))
{
foreach (PropertyInfo property in properties)
{
dr[property.Name] = property.GetValue(o, null);
}
}
dt.Rows.Add(dr);
}
}
As far as I know, this is using reflection: using the arraylist of destinations to bind it to a gridview.
On the other hand, your GridView should be defined like this:
<asp:GridView ID="GridViewDestination" runat="server" Visible="False" Width="98%" CssClass="GridView" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="idNonAnsweredCreating" runat="server" Text='<%# bind("idDestination") %>' Visible="false"></asp:Label>
<asp:Label Visible="true" runat="server" ID="destinationLabelCreating" Text='<%# bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label Visible="true" runat="server" ID="destinationTypeLabelCreating" Text='<%# bind("type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
As you can see, you will bind as many destinations properties as needed in each column of the GridView.
Hope this helps.
精彩评论