Click here to Skip to main content
14,826,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

i am trying to bind data into html table from datatable, in my datatable it has 50 entries of district name with their preference order.
i want to bind this district list with thier preference order (in vertical manner not horizontal) in HTML Table ( i.e in tabular format which will have 10 rows and 5 columns only.( 10*5=50 entries).

so, pls help if any one has it's solution... i have tried a lot but data is being displayed in horizontal which i don't need..

Thanks
Posted

i have tried like this.....

Datatable1 contains 50 row of district with their preference order like this..

Pref_no District
1 Ujjain
2 Ratlam
3 Bhopal
.... ......
in this way..

code which i tried is given below:
C#
       int rows = Datatable1.Rows.Count / 10;
        int ck = Datatable1.Rows.Count % 10;
    string str = "<table border=" + 1 + ">"

       int cnt = Datatable1.Rows.Count-1;
        int cnt1 = Datatable1.Rows.Count-1;
       for (int i = 0; i < 10; i++)
      {
            if (ck == 0)
            {
                str += "<tr>";
                for (int j = 0; j < rows; j++)
               {

           if (Datatable1.Rows[cnt1 - (cnt)][0].ToString() != "")
                 {

                      str += "<td>" + Datatable1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
                }
                 else if (Datatable1.Rows[cnt1 - (cnt - 1)][1].ToString() != "")
               {

                    str += "<td>" + dt1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
                 }
                ;
            cnt--;
               }
                str += "</tr>";
         }
          else
          {
          str += "<tr>";
             for (int j = 0; j < rows+1; j++)
         {

              if (Datatable1.Rows[cnt1 - (cnt)][0].ToString() != "")
              {

                 str += "<td>" + Datatable1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
                 }
                 else if (Datatable1.Rows[cnt1 - (cnt - 1)][1].ToString() != "")
                {

                      str += "<td>" + dt1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
         }
                  ;
              cnt--;
          }
         str += "</tr>";
           }
}
    str += "</table>";
    Label1.Text = str;
   
v3
Finally i got the solution & completed the requirement , i have taken Table control from ToolBox:

XML
<legend><strong>District Prefrence List</strong> </legend>
 <asp:Table ID="tblData" runat="server" GridLines="Both" Width="100%">
   </asp:Table>

on default.aspx.cs code behind page i wrote:
C#
ArrayList PreferenceItem = new ArrayList();
ArrayList PreferenceValue = new ArrayList();
for (int i = 0; i < 50; i++)
{
    if (i < Datatable1.Rows.Count)
    {
        PreferenceItem.Add(Datatable1.Rows[i][0].ToString());
        PreferenceValue.Add(Datatable1.Rows[i][1].ToString());
    }
    else
    {
        PreferenceItem.Add(" ");
        PreferenceValue.Add(" ");
    }
}

for (int r = 0; r < 10; r++)
{
    TableRow row = new TableRow();
    for (int c = 0; c < 5; c++)
    {
        TableCell cell = new TableCell();
        cell.Width = Unit.Percentage(5);
        cell.Controls.Add(new LiteralControl(PreferenceItem[((c*10)
           + (r % 10))].ToString()));
        row.Cells.Add(cell);
        cell = new TableCell();
        cell.Width = Unit.Percentage(15);
       cell.Controls.Add(new LiteralControl(PreferenceValue[((c*10)
            + (r % 10))].ToString()));
        row.Cells.Add(cell);
    }
    tblData.Rows.Add(row);
}
   
v5
Hi,
Iterate through datatable and create the html table dynamically according to the data in datatable. Add the rows and columns to it with your values..
Try like this:
C#
Table tbl = new Table();
tbl.Width = Unit.Percentage(100);
TableRow tblRow = new TableRow();
int cellInd = 0;
for (int i = 0; i < DataTable1.Rows.Count; i++)
{
    //Since you need 5 columns
    if (cellInd == 5)
    {
        // Break the row if the column count of that row becomes 5
        tblRow = new TableRow();
        cellInd = 1;
        //TableRow tblRow = new TableRow();
        TableCell tblCell = new TableCell();
        tblCell.Width = Unit.Percentage(20);
        tblCell.Text = dtRole.Rows[i]["MyColumn"] == DBNull.Value? "" :dtRole.Rows[i]["MyColumn"].ToString();
        tblRow.Controls.Add(tblCell);
        tbl.Rows.Add(tblRow);
    }
    else
    {
        //TableRow tblRow = new TableRow();
        TableCell tblCell = new TableCell();
        tblCell.Width = Unit.Percentage(20);
        tblCell.Text = dtRole.Rows[i]["MyColumn"] == DBNull.Value? "" :dtRole.Rows[i]["MyColumn"].ToString();
        tblRow.Controls.Add(tblCell);
        tbl.Rows.Add(tblRow);
        cellInd += 1;
    }
}
//Now add the dynamic table in any controls such like panels or placeholders.
Panel1.Controls.Add(tbl);



--Amit
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900