C# 在“栅格视图”上单击鼠标右键
我想知道是否有一种方法可以在网格视图中右键单击选择一行并删除 我有delete语句,但我不知道右键单击时如何选择 我看到了一些建议,说要使用鼠标按钮。对,但这对我不起作用,并且有错误(鼠标按钮在当前上下文中不存在) 这是我目前的填充声明C# 在“栅格视图”上单击鼠标右键,c#,asp.net,select,gridview,right-click,C#,Asp.net,Select,Gridview,Right Click,我想知道是否有一种方法可以在网格视图中右键单击选择一行并删除 我有delete语句,但我不知道右键单击时如何选择 我看到了一些建议,说要使用鼠标按钮。对,但这对我不起作用,并且有错误(鼠标按钮在当前上下文中不存在) 这是我目前的填充声明 protected void getGLDepts() { mpSearch.Focus(); string[] mpvalue = mpSearch.Text.Split('(',')');
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();
我的select行语句
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();
还有我的删除语句
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();
对我来说很好:
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);
}
void GridView1\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
e、 添加(“onContextMenu”,ClientScript.GetPostBackEventReference(GridView1,“选择$”+e.Row.RowIndex.ToString())+“返回false;”;
}
}
受保护的覆盖无效渲染(HtmlTextWriter编写器)
{
对于(int index=0;index
您可以在gridview中添加选择和删除按钮。不想使用这些按钮。您是否也将其绑定到数据库?如果是,我将在初始回答下粘贴第二个示例。请记住,旧的delete语句在哪里?您正在尝试捕获鼠标右键单击事件并捕获您选择的行只需添加一个删除按钮,点击它的透视图Datagrid行,然后将代码添加到该按钮事件中..Yuriy可以在我的答案中编辑/更新第一个示例。。我正努力抓紧时间帮助OP走出困境。。谢谢你什么也没做。有了这句话,它如何判断它是右键单击?将Select'更改为Delete`并处理rowdeting
eventyury的示例肯定对您有用我还测试了他的代码confirmation@yuriy,当我右键单击时,windows菜单仍会弹出。此外,如果上下文菜单不清楚,我会尽量避免使用上下文菜单。我只是试图捕获右键单击事件并使其启动我的代码。