Monday, April 16, 2012

How to find control and datakey value in Gridview RowCommand event


protected void grdInvoices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("SaveSourceDes"))
        {



            Control ctrl = e.CommandSource as Control;

            if (ctrl != null)
            {

                GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;

                //Now you can find control on the row where event is raised by using FindControl method.

                DropDownList findme =(DropDownList) _currenrtrow.FindControl("ddlSourceDesc");
                ObjSys.pSourceDiscription=findme.SelectedValue.ToString();
                if (ObjSys.pSourceDiscription == "SELECT")
                {

                }
                else
                {
                    // Get the datakey value
                 
                    GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                   int InvoId = Convert.ToInt32(grdInvoices.DataKeys[row.RowIndex].Value);
                   ObjSys.pInvoiceID = InvoId;
                   ObjSys.UpdateInvoiceSourceDesc();
                }

            }

            //int index = Convert.ToInt32(e.CommandSource.ToString());

            //GridViewRow row = grdInvoices.Rows[index];

            //DropDownList lstState = (DropDownList)row.FindControl("ddlSourceDesc");

         

        }
    }

No comments:

Post a Comment