'How to bind dynamic DataTable to DataList?
Code behind:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Rego No", typeof(string)));
foreach (var item in list)
{
dt.Columns.Add(new DataColumn(string.Format("{0:dd/MM}",item),typeof(string)));
//enter code here
}
dlcalender.DataSource = dt;
dlcalender.DataBind();
ASPX:
DataTable
columns are dynamic like dates. I am binding the DataTable
but nothing is showing.
Can you please guide me how to show the header list in DataTable
?
Solution 1:[1]
with linq.
dlcalender.DataSource = dt.select(p=>new{
c1=p.columns[0],
c2=p.columns[1],
......
});
and use
<%# Eval("c1") %>
<%# Eval("c2") %>
......
Solution 2:[2]
The problem is that you've created columns but you haven't inserted any rows in the DataTable so that's why you don't see any results when running the site in your browser
I think you should also understand how the DataList control works in ASP.NET.
You need to create a HeaderTemplate for the information which will only appear once and an ItemTemplate for the items which are going to repeat.
You cannot bind your DataTable to the HeaderTemplate, because it doesn't know which row to use for the headings;
In the example below I've used string properties for headings and your DataTable for the repeating items:
ASPX:
<asp:DataList ID="dlcalender" runat="server">
<FooterTemplate>
</FooterTemplate>
<HeaderTemplate>
<asp:Label ID="head1" runat="server" Text='<%# Heading1 %>' />
<asp:Label ID="head2" runat="server" Text='<%# Heading2 %>' />
<asp:Label ID="head3" runat="server" Text='<%# Heading3 %>' />
<asp:Label ID="head4" runat="server" Text='<%# Heading4 %>' />
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Rego No")%>
<%# DataBinder.Eval(Container.DataItem,"Column1") %>
<%# DataBinder.Eval(Container.DataItem,"Column2")%>
<%# DataBinder.Eval(Container.DataItem,"Column3")%>
</ItemTemplate>
</asp:DataList>
Code behind:
public string Heading1 { get; set; }
public string Heading2 { get; set; }
public string Heading3 { get; set; }
public string Heading4 { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Heading1 = "Heading 1";
Heading2 = "Heading 2";
Heading3 = "Heading 3";
Heading4 = "Heading 4";
var list = new List<string> { "Column1", "Column2", "Column3" };
var table = new DataTable();
table.Columns.Add(new DataColumn("Rego No", typeof(string)));
foreach (var item in list)
table.Columns.Add(item, typeof(string));
//Now add some rows(which will be repeated in the ItemTemplate)
table.Rows.Add("0", "Row 0", "Row 0", "Row 0");
table.Rows.Add("1", "Row 1", "Row 1", "Row 1");
table.Rows.Add("2", "Row 2", "Row 2", "Row 2");
dlcalender.DataSource = table;
dlcalender.DataBind();
}
Also why don't you just use a grid view?It's much better for displaying tabular data, which I believe is what you have
Solution 3:[3]
Since you created the data columns dynamically at runtime, you have to add them to the gridview at runtime as well, ex:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Rego No", typeof(string)));
foreach (var item in list)
{
dt.Columns.Add(new DataColumn(string.Format("{0:dd/MM}",item),typeof(string)));
//enter code here
dlcalender.Columns.Add(string.Format("{0:dd/MM}",item), string.Format("{0:dd/MM}",item));
}
dlcalender.DataSource = dt;
dlcalender.DataBind();
In the above code, I have added the column to the datalist in teh same time it is added to the data table.
You can filter them if you don't want to add all of it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | vicente |
Solution 2 | Denys Wessels |
Solution 3 | Haitham Shaddad |