1. I am testing one situation: I have in gridview 3 colums: unit, quantity and value. Column unit is getting values form database, column quantity - textbox (with AutoPostBack = “true”, OnTextChanged event see below) for manually input numbers and column
value as label for display multiplication unit and quantity.
protected void tb2_TextChanged(object sender, EventArgs e)
{
TextBox thisTextBox = (TextBox)sender;
GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent;
int row = thisGridViewRow.RowIndex;
label2.Text = row.ToString();
TextBox w = (TextBox)GridView1.Rows[row].Cells[3].FindControl("tb2");
string x = GridView1.Rows[row].Cells[2].Text;
double sum = double.Parse(x) * double.Parse(w.Text);
GridView1.Rows[row].Cells[4].Text = sum.ToString();
}
Problem: when I inserted “quantity” in first row I got correct result in “value” column. But when I am inserting “quantity” in next row that “value” column is displaying correct in the same row but in first row result (“value”) disappeared although that
quantity is keeping. What should I do for keep all previous result?
2. How to display above result in gridview textboxes? I need only code in codebehind.
kszymaniak
Member
205 Points
112 Posts
disappearing values in gridview
Jan 22, 2013 12:25 PM|LINK
Hi
1. I am testing one situation: I have in gridview 3 colums: unit, quantity and value. Column unit is getting values form database, column quantity - textbox (with AutoPostBack = “true”, OnTextChanged event see below) for manually input numbers and column value as label for display multiplication unit and quantity.
protected void tb2_TextChanged(object sender, EventArgs e) { TextBox thisTextBox = (TextBox)sender; GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent; int row = thisGridViewRow.RowIndex; label2.Text = row.ToString(); TextBox w = (TextBox)GridView1.Rows[row].Cells[3].FindControl("tb2"); string x = GridView1.Rows[row].Cells[2].Text; double sum = double.Parse(x) * double.Parse(w.Text); GridView1.Rows[row].Cells[4].Text = sum.ToString(); }Problem: when I inserted “quantity” in first row I got correct result in “value” column. But when I am inserting “quantity” in next row that “value” column is displaying correct in the same row but in first row result (“value”) disappeared although that quantity is keeping. What should I do for keep all previous result?
2. How to display above result in gridview textboxes? I need only code in codebehind.
Regards
paindaasp
Star
12090 Points
2034 Posts
Re: disappearing values in gridview
Jan 22, 2013 01:49 PM|LINK
It sounds like you are binding the GridView on every PostBack, so your values are being overwritten. Try excluding the GridView DataBind on PostBacks.
If Not Page.IsPostBack then
' bind GridView
End If
sarathi125
Star
13599 Points
2691 Posts
Re: disappearing values in gridview
Jan 22, 2013 02:39 PM|LINK
Hi,
Post your gridview bind method, just give a try like the code in the link
http://forums.asp.net/t/1411910.aspx/1
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
kszymaniak
Member
205 Points
112 Posts
Re: disappearing values in gridview
Jan 22, 2013 09:29 PM|LINK
Thank you very much for Yr help. I solved problem and solution is very very easy. There was problem with line
because result wasn't display in any controls I maean: label or textbox. The code should be:
protected void textBox1_TextChanged(object sender, EventArgs e) { TextBox thisTextBox = (TextBox)sender; GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent; int row = thisGridViewRow.RowIndex; TextBox w = (TextBox)GridView1.Rows[row].Cells[3].FindControl("textBox1"); string x = GridView1.Rows[row].Cells[2].Text; double sum = double.Parse(x) * double.Parse(w.Text); ((Label)GridView1.Rows[row].Cells[4].FindControl("Label2")).Text = sum.ToString(); // GridView1.Rows[row].Cells[4].Text = sum.ToString(); // wrong code // ((TextBox)GridView1.Rows[row].Cells[4].FindControl("textBox2")).Text = sum.ToString(); // ok, when using textbox }Regards
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: disappearing values in gridview
Jan 24, 2013 01:13 AM|LINK
Hi,
Considering it that you've solved your problem successfully, I'll close your issue by marking yours as an answer.
Welcome your feedback
Reguards!