i have a button that will extract 2 columns from a selected row (the
button is not a buttonfield btw, its a standalone asp button).
the first column was easy as the datatypes were string. the second one
is a little tricky (for me that is) as im trying to extract the "unit
price" from the selectedrow (gridview1.selectedrow.cells[5]) in my
case. im using a label that will display the cumulative unit price from
all the selected rows
heres a code that i was trying but as obvious, it has problems with datatypes.
int invoicetotal;
int unitprice;
unitprice = GridView1.SelectedRow.Cells[5];
invoicetotal = invoicetotal + unitprice;
Label3.Text = invoicetotal;
what i want is that every time i click on the button, the label will display the total amount of the prices that were selected.
thanks.
Loading
Mahesh ChandPosted Dec 2, 2009, 8:03 AM
Kirtan PatelPosted Dec 2, 2009, 12:39 PM
I solved it Without using that label value ..
Problem was PostBack of Button.
put the GridView1.DataBind() like below
if(!IsPostBack)
{
GridView1.DataBind();
}
and please check "Do you like this answer" check box :)
Mark UyPosted Dec 2, 2009, 12:25 PM
instead of declaring a variable = 0;
i used a default label value instead.
decimal unitprice;
decimal invoicetotal = Convert.ToDecimal(Label4.Text);
unitprice = Convert.ToDecimal(Label3.Text);
invoicetotal = unitprice + invoicetotal;
Label4.Text = invoicetotal.ToString();
its working now.
thank god for labels.
Mark UyPosted Dec 2, 2009, 12:06 PM
i have 2 labels to check the unitserial column and the unit price column.
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
Label1.Text = GridView1.SelectedRow.Cells[1].Text;
Label3.Text = GridView1.SelectedRow.Cells[5].Text;
}
and they are pointing to the correct values upon select on the gridview.
and also i think the problem lies here:
int invoicetotal = 0;
as the invoicetotal reverts back to 0 everytime i click on the button.
im back to square one :( . . .
Kirtan PatelPosted Dec 2, 2009, 10:37 AM
your Problem Solved .. :)
Mark UyPosted Dec 2, 2009, 10:21 AM
Mark UyPosted Dec 2, 2009, 9:50 AM
Kirtan PatelPosted Dec 2, 2009, 9:38 AM
mark , can you send me your project Zip in mail ( [email protected] )
. i can resolve it better ..at mine machine .and will be quick also :)
Mark UyPosted Dec 2, 2009, 9:30 AM
Mark UyPosted Dec 2, 2009, 8:44 AM
i tried what kirtan said and and thanks for reminding me to declare a variable as 0 if it will be use in a math algorithm. now i forgot to mention that the datatype for the unit price was "money" so it was displaying some decimal places in there.
i tried convertodecimal (as well as convert.toint) but got an error = "Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index". after clicking the button once.
so i tried switching to double where i was able to click once without an error, but got the same error message when i tried to click on the button the 2nd time.
what should i do to fix the error??
Kirtan PatelPosted Dec 2, 2009, 8:12 AM
you should Write like below
protected void Button1_Click(object sender, EventArgs e)
{
int unitprice;
int invoicetotal=0;
unitprice = Convert.ToInt32(GridView1.SelectedRow.Cells[5].Text);
invoicetotal = invoicetotal + unitprice;
Label1.Text = invoicetotal.ToString();
}
if still need help then let me know :)
Mark UyPosted Dec 2, 2009, 7:36 AM
any body else??? please help!!
AdamPosted Dec 2, 2009, 7:11 AM
Label3.Text = invoicetotal.ToString();
Hope this helps