ASP.NET - How to convert datalist to datatable

Asked By msakt on 14-Feb-12 03:11 AM
 How to convert datalist to datatable  without use foreach,forloop
Web Star replied to msakt on 14-Feb-12 03:16 AM
If you don't want to use loop than just try to get Binding datasource of DataList into Datatable as follows

DataTable dt = (DataTable ) dlName.DataSource;

hope this helps your
kalpana aparnathi replied to msakt on 14-Feb-12 03:17 AM
hi,

here gives you one link for solution:http://geekswithblogs.net/dotNETvinz/archive/2009/08/13/bind-datatable-to-datalist-control.aspx

Regards,
dipa ahuja replied to msakt on 14-Feb-12 03:28 AM
Way 1

void ToDatatable()
{
  //creating datatable
  DataTable dt = new DataTable();
 
  //adding column
  dt.Columns.Add("id"typeof(int));
  dt.Columns.Add("name"typeof(string));
  foreach (DataListItem item in DataList1.Items)
  {
    int id = int.Parse(((Label)item.FindControl("lblid")).Text);
    string name = ((Label)item.FindControl("lblName")).Text;
    //adding rows
    dt.Rows.Add(id, name);
  }
}

Way 2

//creating datatable
DataTable dt = new DataTable();
dt = (DataTable)DataList1.DataSource;

Somesh Yadav replied to msakt on 14-Feb-12 03:43 AM
Hi,
 

This example shows how to bind a DataTable in a DataList control. Just for the simplicity of this demo I’m going to create a dummy data in the DataTable. See the code blocks below:

 

private DataTable GetData()

{

      DataTable dt = new DataTable();

      DataRow dr;

      dt.Columns.Add(new System.Data.DataColumn("Column1", typeof(String)));

      dt.Columns.Add(new System.Data.DataColumn("Column2", typeof(String)));

        dt.Columns.Add(new System.Data.DataColumn("Column3", typeof(String)));

 

      dr = dt.NewRow();

      dr[0] = "Column1 Item A";

      dr[1] = "Column2 Item B";

      dr[2] = "Column3 Item C";

      dt.Rows.Add(dr);

 

      dr = dt.NewRow();

      dr[0] = "Column1 Item AA";

      dr[1] = "Column2 Item BB";

      dr[2] = "Column3 Item CC";

      dt.Rows.Add(dr);

 

      dr = dt.NewRow();

      dr[0] = "Column1 Item AAA";

      dr[1] = "Column2 Item BBB";

      dr[2] = "Column3 Item CCC";

      dt.Rows.Add(dr);

 

      dr = dt.NewRow();

      dr[0] = "Column1 Item AAAA";

      dr[1] = "Column2 Item BBBB";

      dr[2] = "Column3 Item CCCC";

      dt.Rows.Add(dr);

 

      return dt;

}

 

Now let’s set up the DataList in the ASPX source like below.

 

<asp:DataList ID="DataList1" runat="server">

      <HeaderTemplate>

         <table>

            <tr>

            <th style="width:150px" align="left">HEADER 1</th>

            <th style="width:150px" align="left">HEADER 2</th>

            <th style="width:150px" align="left">HEADER 3</th>

            </tr>

          </table>

      </HeaderTemplate>

      <ItemTemplate>

          <table>

            <tr>

           <td style="width:150px" align="left"> <asp:Label ID="LabelTest1" Text='<%# DataBinder.Eval(Container.DataItem, "Column1") %>' runat="server" /></td>

           <td style="width:150px" align="left"> <asp:Label ID="LabelTest2" Text='<%# DataBinder.Eval(Container.DataItem, "Column2") %>' runat="server" /></td>

           <td style="width:150px" align="left"> <asp:Label ID="LabelTest3" Text='<%# DataBinder.Eval(Container.DataItem, "Column3") %>' runat="server" /></td>

            </tr>

          </table>

      </ItemTemplate>

</asp:DataList>

 

As you can see, the set up above was very simple. Now let’s go switch back to the code behind part of the page and bind the DataList with the data from the DataTable. Here’s the code block below:

 

    protected void Page_Load(object sender, EventArgs e)

    {

      if (!IsPostBack)

      {

        DataTable dt = GetData();

        DataList1.DataSource = dt;

        DataList1.DataBind();

      }

    }

Hope it helps you.
Chintan Vaghela replied to msakt on 14-Feb-12 03:59 AM

Hello,

Convert a Generic List to a DataTable Using Reflection

Check following article for detail

 

http://beecy.net/post/2009/05/18/convert-generic-list-to-datatable-using-reflection.aspx

 

 

Hope this is helpful !

Thanks