3

I have a few questions concerning populating a table from a datatable in asp.net

I have pulled data correctly from my database but now I have a table with data in it. How do I go about populating my asp.net Table control? I understand how to access the rows and columns data but don't know how to "bind" or "populate" the Table control.

I looked into using a gridview but some people said they load slow so I am avoiding that.

Any help would be much appreciated

HTML

<asp:Table runat="server" ID="table" CssClass="table">
    <asp:TableHeaderRow>
        <asp:TableCell>Cell 1 Header</asp:TableCell>
        <asp:TableCell>Cell 2 Header</asp:TableCell>
    </asp:TableHeaderRow>
    <asp:TableRow>
        <asp:TableCell ID="cell1"></asp:TableCell>
        <asp:TableCell ID="cell2"></asp:TableCell>
    </asp:TableRow>
</asp:Table>

C#

        String sql = "SELECT * FROM TABLE";
        SqlCommand command = new SqlCommand(sql, dbConnection);
        SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);
        DataTable table = dataSet.Tables[0];

        // Loop through rows
        foreach (DataRow row in table.Rows)
        {
            // Loop through columns
            foreach(DataColumn column in table.Columns){

            }
        }
3
  • So what is wrong with a gridview? I wouldn't avoid that if it were me. I have never once used a table control but I don't think they can be bound which rather cripples it as a viable tool for this kind of thing. Commented Jan 6, 2016 at 22:57
  • You can still use a Table if you want, you just need something to create the data, such as a Repeater. Commented Jan 6, 2016 at 23:21
  • How do I use a repeater Commented Jan 6, 2016 at 23:28

2 Answers 2

2

I'm giving you code, how to use Repeater:

<asp:Repeater ID="rptTable" runat="server">
    <HeaderTemplate>
        <table class="table">
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td width="50%"><%# Eval("identifier") %></td>
            <td width="*"><%# Eval("value") %></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

And your code behind:

SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select identifier, value from table_name", con);

try{
    con.Open();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    rptTable.DataSource = ds;
    rptTable.DataBind();
}
catch(Exception ex){
    //...
}
finally{
    con.Close();
}

Of course, you have to change table's design and sql query as you need. I just showed basic structure

Sign up to request clarification or add additional context in comments.

1 Comment

rptTable is giving error how it should be declared in the C# code ?
0

You can try this:

private void GenerateTable()
{
    DataTable dt = this.GetData(); //GetData returns your datatable from sql
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        TableRow row = new TableRow();
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            TableCell cell = new TableCell();
            cell.Text = dt.Rows[i][j].ToString();
            row.Cells.Add(cell);
        }
        table.Rows.Add(row); 
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.