我有一個變量定義如下:什麼是一個簡單的方法來綁定一個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
我想動態構建一個可綁定的數據表並不是那麼糟糕。我只是覺得可能有更簡單的方法。 –
還沒有呃? –