0

行を選択し、グリッドビューから右クリックして削除する方法があるかどうかを知りたいと思いました。

削除ステートメントがありますが、右クリックで選択する方法がありません。

mousebuttons.rightを使用するという提案をいくつか見ましたが、それは私には機能せず、エラーが発生します(マウスボタンは現在のコンテキストには存在しません)

これが私の現在のフィルステートメントです

    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();
            }

私のselect行ステートメント

    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);            
        }
    }

と私の削除ステートメント

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();
4

1 に答える 1

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);
}
于 2012-08-15T21:21:23.463 に答える