Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code is just in a Normal WebForm

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Subject" HeaderText="Subject" />
<asp:TemplateField HeaderText="Marks">
<ItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

</Columns>
    </asp:GridView>





This code is in inherited aspx.cs page
C#
protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = setSubject();
        GridView1.DataBind();
    }

private DataTable setSubject()
    {
        DataTable dt = new DataTable();
        dt.Clear();
        dt.Columns.Add("Subject");

        DataRow dr = dt.NewRow();
        dr["Subject"] = "English";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["Subject"] = "Physics";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["Subject"] = "Chemistry";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["Subject"] = "Mathematics";
        dt.Rows.Add(dr);
        return dt;
    }


protected void Button1_Click(object sender, EventArgs e)
    {
Double result;
 foreach (GridViewRow grdrow in grd.Rows)
        {
            if (grdrow.RowType == DataControlRowType.DataRow)
            {
                TextBox textBoxValue = (TextBox)grdrow.FindControl("TextBox1");
                result = result + Convert.ToDouble(textBoxValue.Text);
            }
        }
}






I can not retrieve the value of after edited value of textbox using this.

But the code is effective for a different website(project).
Posted
Updated 9-Jul-12 7:10am
v6
Comments
DamithSL 9-Jul-12 11:17am    
are you bind data on page load? can show the page load code and full code of find control?
Kaushik Saha from Kolkata,India 9-Jul-12 12:38pm    
yes.Now I clearly describe the total functions.
DamithSL 9-Jul-12 12:40pm    
Ok, check my answer.
S@53K^S 9-Jul-12 11:20am    
Did you try and fire the DataGrid RowEdit event and after that try and recapture the data for that if so then you will be able to get the data

firstly you are using grdrow instead of GridView1.
apart from this you can try
(TextBox)GridView1.Rows[0].FindControl("TextBox1");

and you want to select row from the grid then try this

(TextBox)GridView1.SelectedRow.Cells[0].FindControl("TextBox1");

hope it'll help.
 
Share this answer
 
You can use FindControl method.

Try something like:
C#
// For every row
foreach (GridViewRow item in myGrid.Rows)
{   
    TextBox myTextBox = (TextBox)item.FindControl("myTextBox");
    string text = myTextBox.Text;       
} 
 
Share this answer
 
Comments
Kaushik Saha from Kolkata,India 9-Jul-12 12:36pm    
Before i tried this but not solve.
So,I update the related code clearly.
Thanks try to help me.
try adding IsPostBack when you bind data.

C#
private void Page_Load()
{
    if (!IsPostBack)
    {
        GridView1.DataSource = setSubject();
        GridView1.DataBind();
    }
}
 
Share this answer
 
Comments
Kaushik Saha from Kolkata,India 9-Jul-12 13:52pm    
Thanks

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