我有一个变量定义如下:什么是一个简单的方法来绑定一个GridView与动态列?
Dim iRows As List(Of String())
我也一直在转变,要列出清单只是为了使其更易于使用。
Dim iRows As List(Of IList(Of String))
我想使用嵌套数组/列表的内容将该列表绑定到GridView,以动态定义列。我不知道会有多少列,但我知道他们在整个列表中都是一样的。
我只是不确定如何去做。思考?
我有一个变量定义如下:什么是一个简单的方法来绑定一个GridView与动态列?
Dim iRows As List(Of String())
我也一直在转变,要列出清单只是为了使其更易于使用。
Dim iRows As List(Of IList(Of String))
我想使用嵌套数组/列表的内容将该列表绑定到GridView,以动态定义列。我不知道会有多少列,但我知道他们在整个列表中都是一样的。
我只是不确定如何去做。思考?
对我来说,似乎不知道你的数据源看起来像/有多少列,没有办法将每列放到不同的gridview列中。
这意味着,固定标记是不可能的=>您需要将AutoGenerateColumns属性设置为true。
您可以通过访问在GridView的HeaderCollecion设定每一列的的HeaderText,如果你想使用一个不同的
您可能会感兴趣的另一种解决方案,将不会在所有使用GridView控件,如果你只对显示数据感兴趣(意思是没有编辑或删除按钮)你可以使用Reflection来将你的数据渲染成一个有列和行的html表格,就像aleafonso建议的那样。
有趣......我想这是一个很好的观点。毕竟,这就是它的存在。我正在寻找一种不太“自动化”的方式,因为我正在定义像按钮等列的其他属性。这就是说,你是正确的,这将做基础,也许还有另一种方式来处理其余的。我不认为我说得好。 –
我已经做了这样的事情与c#可能可以帮助你。
我将有一个目的地列表这将被填充到一个GridView。
目标对象必须是可序列化的,并且不能有可为空的值。这是我的例子:
[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() { }
}
要填充GridView的每一次你需要做到以下几点:
GridViewDestination.DataSource = ConvertArrayListToDataTable(listSelectedDestinations);
GridViewDestination.DataBind();
其中ConvertArrayListToDataTable如下:
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);
}
}
据据我所知,这是使用反射:使用目标的arraylist绑定到一个gridview。
在另一方面,你的GridView应该这样定义的:
<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>
正如你所看到的,你会绑定多达目的地需要在GridView的每一列性能。
希望这会有所帮助。
请考虑改为使用DataTable。 – SLaks
我想动态构建一个可绑定的数据表并不是那么糟糕。我只是觉得可能有更简单的方法。 –
还没有呃? –