challengeAccepted
challengeAccepted

Reputation: 7600

Creating tables and displaying textboxes in codebehind

I have a Dataset from the database which i want to display. My aim is to display a month's data on an aspx page using VS 2005 with 5 days per each row. I have written the code like this but i am confused with the i and j. This code displays nothing.

if (ds != null && ds.Tables[0].Rows.Count > 0)
{
    Table table = new Table();
    table.ID = "Table1";

    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    TextBox tb1 = new TextBox();
    TextBox tb2 = new TextBox();

    // I am not sure what i and j should be here to display 5 per each row..
    for (int i = 0; i < 5; i++)
    {
        if (int j == 0; j < ds.Tables[0].Rows.Count; j ++)
        {

             tb1.ID = "txtDateRow" + x + "Col" + j;
             tb1.Text = ds.Tables[0].Rows[x]["Date"].ToString();
             tb2.ID = "txtDetails" + x + "Col" + j;
             tb2.Text = ds.Tables[0].Rows[x]["AmountSold"].ToString();
             cell.Controls.Add(tb1);
             cell.Controls.Add(tb2);
             table.Rows.Add(row);
        }
    }
    Panel1.Controls.Add(table);
}

If someone could help me solve this, i really appreciate it. Thanks a lot.

Upvotes: 0

Views: 3739

Answers (3)

Kai G
Kai G

Reputation: 3502

I'm not sure what 'x' is supposed to be in your code. I think the general structure should be something like this but the code below will add the same thing for each column:

if (ds != null && ds.Tables[0].Rows.Count > 0)
{
    Table table = new Table();
    table.ID = "Table1";

    // j is the row index
    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
    {
        TableRow row = new TableRow();

        // i is the column index
        for (int i = 0; i < 5; i++)
        {
             TableCell cell = new TableCell();
             TextBox tb1 = new TextBox();
             TextBox tb2 = new TextBox();

             tb1.ID = "txtDateRow" + j + "Col" + i;
             tb1.Text = ds.Tables[0].Rows[j]["Date"].ToString();
             tb2.ID = "txtDetails" + j + "Col" + i;
             tb2.Text = ds.Tables[0].Rows[j]["AmountSold"].ToString();
             cell.Controls.Add(tb1);
             cell.Controls.Add(tb2);

             row.Cells.Add(cell);
        }

        table.Rows.Add(row);
    }
    Panel1.Controls.Add(table);
}

Upvotes: 0

Rakesh
Rakesh

Reputation: 863

row.Controls.Add(cell) is missing. Bcoz of this textbox controls are not added to the table and you are not able to see anything. Add this line and it will help you see. Later you can think about i & j.

Upvotes: 1

JonH
JonH

Reputation: 33153

You have a loop with a counter, namely, i but you test for j==0 what is j? Is j initalized somewhere? Is j even declared? If not, are you sure you don't want to test for i == 0 ?

That is the reason you are not getting any results in the for loop. The code:

 tb1.ID = "txtDateRow" + x + "Col" + j;
             tb1.Text = ds.Tables[0].Rows[x]["Date"].ToString();
             tb2.ID = "txtDetails" + x + "Col" + j;
             tb2.Text = ds.Tables[0].Rows[x]["AmountSold"].ToString();
             cell.Controls.Add(tb1);
             cell.Controls.Add(tb2);
             table.Rows.Add(row);

Is inside a condition:

if (j == 0)

The question is does this condition ever happen? If so you need to post more code. Debug your code and set breakpoints and watch how it runs. Per your edit you posted:

if (int j == 0; j < ds.Tables[0].Rows.Count; j ++)

This is not even valid, I think you need to pick up a book on C# syntax before you tackle this problem.

Upvotes: 0

Related Questions