Search This Blog

Sunday, March 25, 2012

Iterating through a GridView to find a row's binded text


foreach (GridViewRow Row in GridView1.Rows)
    {
        Label MyLabel = Row.FindControl("Label1") as Label;

        if (MyLabel != null)
        {
            Response.Write(MyLabel.Text);
        }
    }


private void findUserIdInGridview()
02{
03    if (gdvUser != null && gdvUser.Rows.Count > 0)
04    {
05        foreach (GridViewRow row in gdvUser.Rows)
06        {
07            //get userId
08 
09            //first way: sIndex is the UserId.
10            //row.RowIndex is the value from: DataKeyNames="UserId"
11            string sIndex =
12               (string)gdvUser.DataKeys[row.RowIndex].Value.ToString();
13 
14            //second way: find Label control: lblUserId in this row (if exists).
15            Label lUserId = (Label)row.FindControl("lblUserId");
16            string sUserIdYouWant = lUserId.Text;
17            aFunctionCallforThisUser(sUserIdYouWant);
18        }
19    }
20}
try this out:
DataSet objDS = new DataSet(); 
                DataTable objDT = new DataTable("theTable");

                objDS.Tables.Add(objDT); 
                DataColumn objDC; 
                objDC = new DataColumn("Order_Item_Id", Type.GetType("System.String"));

                objDT.Columns.Add(objDC); 
                objDC = new DataColumn("Drug_Id", Type.GetType("System.String"));

                objDT.Columns.Add(objDC); 
                objDC = new DataColumn("DrugDesc", Type.GetType("System.String"));

                objDT.Columns.Add(objDC); 
                objDC = new DataColumn("Qty_Ordered", Type.GetType("System.String"));

                objDT.Columns.Add(objDC); 
                objDC = new DataColumn("Qty_Fulfilled", Type.GetType("System.String"));

                objDT.Columns.Add(objDC); 
                objDC = new DataColumn("Notes", Type.GetType("System.String"));

                objDT.Columns.Add(objDC); 
                DataRow objDR; 
                foreach (GridViewRow objtestDR 
in gvOpenOrder.Rows) 
                    { 
                    //Create new row in objDT table with another drug item

                    objDR = objDT.NewRow(); 
                        objDR["Order_Item_Id"] = ((TextBox)objtestDR.FindControl("order_item_id")).Text;

                        objDR["Drug_Id"] = ((TextBox)objtestDR.FindControl("drug_id")).Text;

                        objDR["DrugDesc"] = objtestDR.Cells[3].Text; 
                        objDR["Qty_Ordered"] = ((TextBox)objtestDR.FindControl("qtyOrdered")).Text;

                        objDR["Qty_Fulfilled"] = ((TextBox)objtestDR.FindControl("txtQtyFulfilled")).Text;

                        objDR["Notes"] = ((TextBox)objtestDR.FindControl("txtNotes")).Text;

                    objDT.Rows.Add(objDR); 
                    }

No comments:

Post a Comment