Saturday, May 28, 2011

Gridview_RowCommand Event





protected void grdData_RowCommand(object sender, GridViewCommandEventArgs e)
{     
 if (e.CommandName == "edit")
      {       
   GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
   Int32 userid = Convert.ToInt32(e.CommandArgument.ToString());
   ViewState["userid"] = userid;  
   Label lblCat = new Label();
   lblCat = (Label)grow.FindControl("lblcategory");
   ViewState["cat"] = lblCat.Text.ToString();
  }  


 if (e.CommandName == "update")
   {
    GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    DropDownList ddl = (DropDownList)grow.FindControl("ddlCat");
    ddl.DataSource = GetCategory();
    ddl.DataTextField = "categoryname";
    ddl.DataValueField = "categoryid";
    ddl.DataBind();  
    Int32 userid = Convert.ToInt32(ViewState["userid"]);    
    Int32 catid = Convert.ToInt32(ViewState["categoryid"]);
    Int32 intresult;
    SqlConnection con = new SqlConnection();           con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ToString();     SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "UpdateUser";     
    cmd.CommandType = CommandType.StoredProcedure;
   cmd.Connection = con;
   SqlParameter param = new SqlParameter(); 
   cmd.Parameters.AddWithValue("@userid", userid); 
   cmd.Parameters.AddWithValue("@categoryid", catid);  
   param = cmd.Parameters.Add("ReturnValue"SqlDbType.Int); 
   param.Direction = ParameterDirection.ReturnValue;
   con.Open(); 
   cmd.ExecuteNonQuery(); 
   intresult =(int) cmd.Parameters["ReturnValue"].Value;
   con.Close();  
      if (intresult == 1)
            {  
              lblmsg.Text="Updated Successfully!"
              grdData.EditIndex = -1;  
              BindGrid(); 
            }  
           if (intresult == -1) 
            {     
            lblmsg.Text = "Not Updated!!"
            }   
   }  


 if (e.CommandName == "delete"
  { 
   GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
  } 
}







0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More