Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have implemented editable GridView. There i have two fields Quantity and Value.

It has third field called Total.I want to get total by quantity*value. For that i have made Value textbox AutoPostBack="true" added event handler(txtbx_TextChanged) for it in RowDataBound Event of GridView.

I am trying to access Quntity and Value fields inside that event handler that is txtbx_TextChanged. I tried like this,
C#
TextBox txtTotal = gv.Rows[index].cells[0].Controls[0].FindControl("TOTAL") as TextBox;

But,this shows me error for index.What should be there in place of index ? Rows[?]..?

I just want to know in textchanged event,how to get index of that Row in which i am processing that two textboxes.

I would appreciate any help. Thanks.
Posted
Updated 26-Aug-13 22:44pm
v2
Comments
Jignesh Khant 27-Aug-13 4:56am    
Try like this:
string comments = (((TextBox)row.FindControl("txtcomments")).Text).ToString();
Coder_one 27-Aug-13 5:12am    
What is 'row' here?

Hi...
See this one to find control in gridview.
GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
string txtreply = ((TextBox)gv.Rows[gvRow.RowIndex].FindControl("txtreply")).Text;

Am used like,its working fine.
thank u.
 
Share this answer
 
v2
Comments
Coder_one 27-Aug-13 6:30am    
Thanks alot. Murali. It works like a charm..! Thanks again.
[no name] 27-Aug-13 6:49am    
U are welcome.
As you asked "I just want to know in textchanged event,how to get index of that Row in which i am processing that two textboxes."

Solution 2 is sufficient for you.

But what happen if you enter data in Value first ?? You TextChanged event will be fired and start calculating quantity * value while there is no data in quantity field. PROBLEM!!!
Even after this if you enter data in quantity field, TextChanged event will not fire untill you again change data in Value field.

So its better to add a LinkButton(in same Or different column) and give it some CommandName and handle it OnRowCommand like this -

ASP.NET
<asp:gridview runat="server" id="GridView1" onrowcommand="GridView1_RowCommand" ...="" xmlns:asp="#unknown">

...
<itemtemplate>
                    <asp:linkbutton id="LinkButton1" commandname="calculateTotal" commandargument="<%# Container.DisplayIndex %>" runat="server">LinkButton</asp:linkbutton>
</itemtemplate>
...

</asp:gridview>


In code behind -
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

  if (e.CommandName == "calculateTotal")
  {
    int rowindex=intParse(e.CommandArgument.ToString());
    int quantityfield = int.Parse(((TextBox)gv.Rows[rowindex].FindControl("txtQuantity")).Text);
    int valuefield = int.Parse(((TextBox)gv.Rows[rowindex].FindControl("txtValue")).Text);    

    TextBox result = (TextBox)gv.Rows[rowindex].FindControl("TOTAL");
    result.Text = (quantityfield * valuefield).ToString() ;
  }
}


Thats All :)
 
Share this answer
 
v2
Comments
Coder_one 29-Aug-13 3:26am    
I appreciate your suggestion,but due to some requirements i can't add link button. I figured out that problem and adjusted it accordingly. Thanks for the consideration.
TextBox txtTotal =(TextBox) gv.Rows[e.RowIndex].FindControl("TOTAL");

Try this, it will work
 
Share this answer
 
Comments
Coder_one 27-Aug-13 4:43am    
It shows same error because e.RowIndex can't be accessed inside txtbx_TextChanged. That is the main problem.

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