2012-08-15 83 views
0

我想知道是否有方法來選擇一行並從網格視圖中右鍵單擊刪除。右鍵單擊網格視圖

我有刪除語句,但我沒有如何選擇右鍵單擊。

我見過一對夫婦的建議,說使用mousebuttons.right但是,這並不爲我工作和錯誤與(mousebuttons在目前情況下不存在)

這裏是我當前的填充聲明

protected void getGLDepts() 
    { 
      mpSearch.Focus(); 
      string[] mpvalue = mpSearch.Text.Split('(',')'); 
      string coa = ""; 
      string map = ""; 
      SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Rollup2ConnectionString"].ConnectionString); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.Connection = myconn; 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = "USP_GET_GL_BY_DEPT"; 
      cmd.Parameters.Add("@DEPTCODE", SqlDbType.Int).Value = mpvalue[1].ToString(); 
      foreach (ListItem item in mpcbl.Items) 
      { 
       if (item.Selected) 
       { 
        coa += "','" + item.Value; 
       } 
      } 
      if (coa != "") coa = coa.Substring(2, coa.Length - 2) + "'"; 
      else coa = "''"; 
      cmd.Parameters.Add("@COA", SqlDbType.VarChar).Value = coa; 
      foreach (ListItem item in exceptdefault.Items) 
      { 
       if (item.Selected) 
       { 
        map += "','" + item.Value; 
       } 
      } 
      if (map != "") map = map.Substring(2, map.Length - 2) + "'"; 
      else coa = "''"; 
      cmd.Parameters.Add("@MAP", SqlDbType.VarChar).Value = map; 

      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 

      try 
      { 
       da.Fill(ds); 
       if (ds.Tables[0].Rows.Count > 0) 
       { 
        gvMapping.DataSource = ds; 
        gvMapping.DataBind(); 

        lblGLDeptData.ForeColor = System.Drawing.Color.Black; 
        lblGLDeptData.Text = " " + ds.Tables[0].Rows.Count + " Cost Center/Funds are Mapped to this Department."; 
       } 
       else 
       { 
        gvMapping.DataSource = ds; 
        gvMapping.DataBind(); 

        lblGLDeptData.ForeColor = System.Drawing.Color.Black; 
        lblGLDeptData.Text = " No Currently Mapped Cost Centers."; 
       } 
      } 
      catch (Exception ex) 
      { 
       lblGLDeptData.ForeColor = System.Drawing.Color.Red; 
       lblGLDeptData.Text = ex.Message; 
      } 
      finally 
      { 
       myconn.Close(); 
       myconn.Dispose(); 
      } 

我選擇行語句

protected void gvSelect(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //e.Row.Cells[0].Style["display"] = "none";   
      e.Row.ToolTip = "Click to select row"; 
      e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gvMapping, "Select$" + e.Row.RowIndex);    
     } 
    } 

和我的delete語句

protected void delSysGLDepts(object sender, EventArgs e) 
    { 
     if (cbsys.Checked && !cbgl.Checked) 
     { 
      GridViewRow row = gvMapping.SelectedRow; 
      SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Rollup2ConnectionString"].ConnectionString); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.Connection = myconn; 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = "USP_DELETE_SYS_ROW"; 
      cmd.Parameters.Add("@SYSID", SqlDbType.Int).Value = row.Cells[1].Text; 

       myconn.Open(); 
       object count = cmd.ExecuteNonQuery(); 

       myconn.Close(); 
       myconn.Dispose(); 

       getSysDepts(); 
+0

你可以選擇添加和刪除按鈕,你的GridView。 – 2012-08-15 20:43:20

+0

不想使用這些按鈕。 – user1512593 2012-08-15 20:46:06

+1

你有這個綁定到數據庫以及如果是這樣我會粘貼第二個例子,以及最初的答案 – MethodMan 2012-08-15 20:46:11

回答

1

很適合我:

void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes.Add("onContextMenu ", ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()) + "; return false;"); 
    } 
} 

protected override void Render(HtmlTextWriter writer) 
{ 
    for (int index = 0; index < GridView1.Rows.Count; index++) 
    { 
     ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select$" + index.ToString()); 
    } 

    base.Render(writer); 
} 
+0

這沒有做任何事情。有了這個聲明,它是如何告訴它是正確的點擊? – user1512593 2012-08-15 21:30:49

+0

更改'選擇'刪除'並處理'RowDeleting'事件 – 2012-08-15 21:36:59

+0

尤里的例子應該肯定適合你我也測試了他的代碼確認 – MethodMan 2012-08-15 21:41:26