This is a migrated thread and some comments may be shown as answers.

Looping through GridDataItem's in button OnClick outside of Grid

2 Answers 490 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ronnie
Top achievements
Rank 1
Ronnie asked on 30 Mar 2010, 11:41 PM
Hello,

I need to use a for loop instead of a foreach loop to update all the TextBox values(txtQuantity), but I don't know how to reference/cast the GridDataItem to the RadGrid while not being inside of a RadGrid event.
This OnClick works on the most part.  But if evaluates to false it just continues.  I need it to stop.

      protected void btnUpdateCart_Click(object sender, EventArgs e) 
      { 
         // TODO: Use for loop to stop when an expression evaluates to false. 
         //for (int i = 0; i < grdCart.Items.Count; i++) 
         foreach (GridDataItem dataItem in grdCart.MasterTableView.Items) 
         { 
            // TODO: Cast (GridDataItem)dataItem here and remove foreach 
            string strItemId = dataItem.GetDataKeyValue("ID").ToString(); 
            RadTextBox txtQuantity = (RadTextBox)dataItem.FindControl("txtQuantity"); 
            int itemId, quantity; 
            bool success = false
 
            if (Int32.TryParse(strItemId, out itemId) && Int32.TryParse(txtQuantity.Text, out quantity)) 
            { 
               success = _cartItem.Update(itemId, quantity); 
            } 
 
            lblFeedback.Text = success ? "Your cart has been successfully updated!" : "There was an error updating your cart.<br />Please verify that the quantity value is a number and try again."
         } 
 
         grdCart.Rebind(); 
      } 

Thank you in advance,
Ronnie

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 31 Mar 2010, 06:19 AM
Hi Ronnie,

The following code shows the way you can refer GridDataItem in a for loop in an external button OnClick event. You can exit from the for loop by using the break statement.

C#:

protected
 void btnUpdateCart_Click(object sender, EventArgs e)
    for (int i = 0; i < grdCart.MasterTableView.Items.Count; i++) 
    { 
        GridDataItem dataItem = (GridDataItem)grdCart.MasterTableView.Items[i]; 
        string strItemId = dataItem.GetDataKeyValue("ID").ToString(); 
        TextBox txtQuantity = (TextBox)dataItem.FindControl("txtQuantity"); 
        int itemId, quantity; 
        bool success = false
        if (Int32.TryParse(strItemId, out itemId) && Int32.TryParse(txtQuantity.Text, out quantity)) 
        { 
            success = _cartItem.Update(itemId, quantity); 
            lblFeedback.Text = "Your cart has been successfully updated!"
        } 
        else 
        { 
            lblFeedback.Text = "There was an error updating your cart.<br />Please verify that the quantity value is a number and try again.";  
            break
        } 
    } 
 
    RadGrid1.Rebind(); 
}

Hope this helps,
Princy.
0
Ronnie
Top achievements
Rank 1
answered on 31 Mar 2010, 11:01 AM
Thank you Princy,

I was under the idea that a for loop automatically breaks at false, my mistake.
Thank you for including the break statement.  With that I can still use the foreach loop.  It's down to preference now.

Either way, now I know how to get a specific GridDataItem by index.  That will come in quite handy!

Thanks again,
Ronnie


Tags
Grid
Asked by
Ronnie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ronnie
Top achievements
Rank 1
Share this question
or