Click here to Skip to main content
15,921,062 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am adding few textbox dynamic in OnRowDataBound and when i try getting the value then i am getting null.

Here is my code

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int columnValue = Convert.ToInt32(ViewState["CountColumnValue"].ToString());
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 1; i <= columnValue; i++)
            {
                TextBox myTextBox = new TextBox();
                myTextBox.ID = "myTextBox" + (i).ToString();
                e.Row.Cells[i].Controls.Add(myTextBox);
            }
        }
    }


protected void btnEnter_Click(object sender, EventArgs e)
    {
        ViewState["CountColumnValue"] = txtNumber.Text;
        SetInitialRow();
    }

private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));

        int columnValue = Convert.ToInt32(ViewState["CountColumnValue"].ToString());

        for (int i = 0; i < columnValue; i++)
        {
            dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
        }
        dr = dt.NewRow();
        dr["RowNumber"] = 1;

        for (int j = 0; j < columnValue; j++)
        {
            dr["Column" + j] = string.Empty;
        }
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

protected void btnResult_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Gridview1.Rows[0].Cells.Count; i++)
        {
            GridViewRow grd = Gridview1.Rows[0];
            string str = (grd.Cells[1].FindControl("myTextBox1") as TextBox).Text;
        } 
    }
Posted
Updated 18-Jan-12 19:14pm
v2
Comments
anushripatil 19-Jan-12 1:16am    
in btnResult_Click,i=1
string str = (grd.Cells[1].FindControl("myTextBox1") as TextBox).Text;
the id of your textbox should be like "myTextBox"+i

chang eyour fix index in cells to your country

old
(grd.Cells[1].FindControl("myTextBox1") as TextBox).Text;


new
(grd.Cells[i].FindControl("myTextBox" + i) as TextBox).Text;


changes
* Cells[1] to Cells[i]
* "myTextBox1" to "myTextBox" + i
 
Share this answer
 
Plz give me the answer as soon as.
 
Share this answer
 
When your populate your grid you should add this condition in your rowdatabound event because 1st time rowdatabound runs with -ve index value.
SQL
if (e.Row.RowIndex != -1)
{
}

and then try doing this for getting the values from the textboxes:

C#
Foreach(Datarow dr in Gridview1.Rows)
{
    TextBox txtDummy = new TextBox;
    txtDummy = (TextBox)e.Row.FindControl("textBoxID goes here"));
    string dummy = txtDummy.text;
}


For ID see the source of the page and see whats the id its generating for your dynamic textboxes, it should be something like "Gridview1_ctl02_textboxname_textbox"
 
Share this answer
 
v3
if (e.Row.RowType == DataControlRowType.DataRow)
{

TextBox  txtFCAlias = (TextBox )Gridview1.Rows[i].FindControl("myTextBox1");
OR
 string str = ((TextBox )Gridview1.Rows[i].FindControl("myTextBox1")).Text.trim();

OR
string str = ((TextBox )e.Rows.FindControl("myTextBox1")).Text.trim();
}
 
Share this answer
 
v2
hi,

One question:
Does the grid already have a column or the columns are created dynamically?

If columns are created dynamically..
You are binding the data table to the grid.Fine.On the rowbound you are adding a new textbox control to the grid.But where are you assigning the textbox.text ?

Am i missing something here?
 
Share this answer
 
v2
try below code to show dynamic grid or static grid generated from staticId I have taken SrNo instead of any ID so don't confuse

#region values help to show gridview
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("SrNo", typeof(int)));
dt.Columns.Add(new DataColumn("Date", typeof(DateTime)));
dt.Columns.Add(new DataColumn("StartTime", typeof(string)));
dt.Columns.Add(new DataColumn("EndTime", typeof(string)));
dt.Columns.Add(new DataColumn("TotalTime", typeof(string)));
dt.Columns.Add(new DataColumn("ProjectName", typeof(string)));
dt.Columns.Add(new DataColumn("ModuleName", typeof(string)));
dt.Columns.Add(new DataColumn("EntityName", typeof(string)));
dt.Columns.Add(new DataColumn("WorkType", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
dt.Columns.Add(new DataColumn("Remarks", typeof(string)));
dr = dt.NewRow();
dr["SrNo"] = 1;
dr["Date"] = DateTime.Now;
dr["StartTime"] = string.Empty;
dr["EndTime"] = string.Empty;
dr["TotalTime"] = string.Empty;
dr["ProjectName"] = string.Empty;
dr["ModuleName"] = string.Empty;
dr["EntityName"] = string.Empty;
dr["WorkType"] = string.Empty;
dr["Status"] = string.Empty;
dr["Remarks"] = string.Empty;
dt.Rows.Add(dr);

ViewState["CurrentTable"] = dt;

grdDaily.DataSource = dt;
grdDaily.DataBind();
}
#endregion

#region virtualTable
private void virtualtable()
{
dtvt.Columns.Add(new DataColumn("SrNo", typeof(int)));
dtvt.Columns.Add(new DataColumn("Date", typeof(DateTime)));
dtvt.Columns.Add(new DataColumn("StartTime", typeof(string)));
dtvt.Columns.Add(new DataColumn("EndTime", typeof(string)));
dtvt.Columns.Add(new DataColumn("TotalTime", typeof(string)));
dtvt.Columns.Add(new DataColumn("ProjectName", typeof(string)));
dtvt.Columns.Add(new DataColumn("ModuleName", typeof(string)));
dtvt.Columns.Add(new DataColumn("EntityName", typeof(string)));
dtvt.Columns.Add(new DataColumn("WorkType", typeof(string)));
dtvt.Columns.Add(new DataColumn("Status", typeof(string)));
dtvt.Columns.Add(new DataColumn("Remarks", typeof(string)));
ViewState["dtFinal"] = dtvt;
ViewState["dtFinal123"] = dtvt;
}
#endregion
 
Share this answer
 

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