【问题标题】:Right click Grid view右键单击网格视图
【发布时间】:2012-08-12 04:44:04
【问题描述】:

我想知道是否有办法从网格视图中选择一行并在右键单击时删除。

我有删除语句,但我只是不知道如何右键单击。

我看到了一些建议使用 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();
            }

我的选择行语句

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

【问题讨论】:

  • 您可以在gridview中添加选择和删除按钮。
  • 不想使用那些按钮。
  • 你是否也将它绑定到数据库,如果是这样,我将在初始答案下粘贴第二个示例
  • 记住旧的删除语句在哪里,您正在尝试捕获鼠标右键单击事件并捕获选定的行,您可以添加一个删除按钮单击它以获得它的透视 Datagrid Row 和将您的代码添加到该按钮事件..
  • Yuriy 如有必要,可以在我的回答中随意编辑/更新第一个示例。我正在努力帮助 OP 解决问题。谢谢

标签: c# asp.net select gridview right-click


【解决方案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);
}

【讨论】:

  • 那没有做任何事情。有了这个语句,它如何判断它是右键单击?
  • 更改Select' to Delete`并处理RowDeleting事件
  • Yuriy 的例子绝对适合你,我还测试了他的代码以进行确认
  • @yuriy,当我右键单击 Windows 菜单时仍然弹出。啊哈
  • 另外,如果不清楚,我会尽量避免使用上下文菜单。我只是想捕获右键单击事件并启动我的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2012-01-08
  • 2016-08-31
  • 2012-08-16
  • 1970-01-01
  • 2015-10-11
  • 2015-03-21
相关资源
最近更新 更多