Deba
Deba

Reputation: 25

How to read dynamical added text box value inside a gridview using C#?

I want to add row and column dynamically with a TextBox in every cell of a GridView. I have done this successfully. But the problem is that I can not read the TextBox's value when I click a button.

<asp:GridView runat="server" ID="gv" OnRowDataBound="gv_OnRowDataBound"></asp:GridView>

Dynamically add row and column in the grid:

protected void btnGenerate_OnClick(object sender, EventArgs e)
{
    int rowsCount = Convert.ToInt32(tbxRow.Text);
    int colsCount = Convert.ToInt32(tbxCol.Text);
    DataTable dt=new DataTable();
    for(int col=0;col<colsCount;col++)
    {
        dt.Columns.Add("D-" + col, typeof (int));
    }
    for (int i = 0; i < rowsCount; i++)
    {
        DataRow dr = dt.NewRow();
        dt.Rows.Add(dr);
    }
    gv.DataSource = dt;
    gv.DataBind();
}

Here is my code to add TextBoxes to the GridView:

protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TextBox txt = new TextBox();
            txt.ID = "tbx" + i;
            e.Row.Cells[i].Controls.Add(txt);
        }
    }
}

I have tried this to get the value of the TextBox, but it always shows null:

protected void btnSave_OnClick(object sender, EventArgs e)
{
    foreach (GridViewRow row in gv.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < row.Cells.Count; i++)
            {
                TextBox tb = (TextBox) row.Cells[i].FindControl("tbx" + i);
            }

        }
    }
}

Upvotes: 2

Views: 136

Answers (2)

Md Sarowar Hossen
Md Sarowar Hossen

Reputation: 16

Updated

protected void GenerateGridColumn()
{
    int colsCount = Convert.ToInt32(tbxCol.Text);
    TemplateField tfield;
    BoundField bfield = new BoundField();
    bfield.HeaderText = "";
    for (int col = 0; col <= colsCount; col++)
    {
        tfield = new TemplateField();
        tfield.HeaderText = "D-" + col;
        gv.Columns.Add(tfield);
    }
    tfield = new TemplateField();
    tfield.HeaderText = "Supply";
    gv.Columns.Add(tfield);

}
protected void btnGenerate_OnClick(object sender, EventArgs e)
{
    GenerateGridColumn();
    int rowsCount = Convert.ToInt32(tbxRow.Text);
    DataTable dt = new DataTable();
    for (int i = 0; i <= rowsCount; i++)
    {
        DataRow dr = dt.NewRow();
        dt.Rows.Add(dr);
    }
    gv.DataSource = dt;
    gv.DataBind();
    btnSave.Visible = true;
}


protected void gv_OnRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            if (i > 0)
            {
                TextBox txt = new TextBox();
                txt.ID = "tbx" + i;
                e.Row.Cells[i].Controls.Add(txt);
            }
        }
    }
}

 protected void btnSave_OnClick(object sender, EventArgs e)
{
foreach (GridViewRow row in gv.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < row.Cells.Count; i++)
        {
            TextBox tb = (TextBox) row.Cells[i].FindControl("tbx" + i);
        }
    }
}
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460158

You have to add them in OnRowCreated which is triggered every postback not only when you databind the grid:

protected void gv_OnRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TextBox txt = new TextBox();
            txt.ID = "tbx" + i;
            e.Row.Cells[i].Controls.Add(txt);
        }
    }
}

So you have to use initialize and add them in RowCreated and use RowDataBound if you want to assign a text.

But why don't you use a TemplateField and add the textbox there. That makes your life much easier.

Side-Note: you don't need the DataControlRowType.DataRow-check if you enumerate the Rows-property of the grid because only DataRow-items are returned:

protected void btnSave_OnClick(object sender, EventArgs e)
{
    foreach (GridViewRow row in gv.Rows)
    {
        for (int i = 0; i < row.Cells.Count; i++)
        {
            TextBox tb = (TextBox) row.Cells[i].FindControl("tbx" + i);
        }
    }
}

Upvotes: 1

Related Questions