Gridview删除一行(100分)
用鼠标选中一行之后点击删除就删除一行,不要Gridview自带的!详细一点的代码正确后80分利马补上一个Gridview我前面用的是RadioButton如果选中了Rad...
用鼠标选中一行之后点击删除就删除一行,不要Gridview自带的!
详细一点的代码
正确后80分利马补上
一个Gridview 我前面用的是RadioButton
如果选中了RadioButton,然后点外面的删除按扭就可以删除那一行,就是这样的 展开
详细一点的代码
正确后80分利马补上
一个Gridview 我前面用的是RadioButton
如果选中了RadioButton,然后点外面的删除按扭就可以删除那一行,就是这样的 展开
8个回答
展开全部
你的表肯定有个ID的主键值吧,你的gridview要设定一下DataKeyNames="ID"这个属性值,接下的事件就好多了,写个OnRowDeleting="PublicGridRowDeleting"事件就可以了。
其实你不需要radiobutton的,一个按钮就可以取到ID值来了,我把所有代码贴出你看下!
<asp:GridView ID="GridLog" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
BorderColor="#333" BorderStyle="solid" BorderWidth="1" OnRowDeleting="PublicGridRowDeleting"
GridLines="None" Width="98%" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false" ReadOnly="True"
SortExpression="ID" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您确认删除该规则');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
日志库暂时为空!
</EmptyDataTemplate>
</asp:GridView>
CS代码
protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();
string strSQL;
strSQL = "Delete from systemRole " +
" WHERE id = " + strID;
myBase.NoreturnSql(strSQL);
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('指定规则删除成功!');</script>");
GridBind();
}
关键是设定好DataKeyNames后,可以靠 string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();获得选择列的ID值
其实你不需要radiobutton的,一个按钮就可以取到ID值来了,我把所有代码贴出你看下!
<asp:GridView ID="GridLog" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
BorderColor="#333" BorderStyle="solid" BorderWidth="1" OnRowDeleting="PublicGridRowDeleting"
GridLines="None" Width="98%" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false" ReadOnly="True"
SortExpression="ID" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您确认删除该规则');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
日志库暂时为空!
</EmptyDataTemplate>
</asp:GridView>
CS代码
protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();
string strSQL;
strSQL = "Delete from systemRole " +
" WHERE id = " + strID;
myBase.NoreturnSql(strSQL);
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('指定规则删除成功!');</script>");
GridBind();
}
关键是设定好DataKeyNames后,可以靠 string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();获得选择列的ID值
展开全部
那你应该需要记录一下这一行的序列或者是记录下这行数据的ID,然后在后台根据这个序列(ID)来编写删除代码 !
哦 这样 下面 我的代码希望对你有用
if (r.RowType == DataControlRowType.DataRow)
{
chk = (CheckBox)(r.FindControl("ckslt"));
if (chk.Checked)
{
//获得被选中的行编号
key = Convert.ToInt32(this.GridView1.DataKeys[r.RowIndex].Value.ToString());
int id;
id = Convert.ToInt32(((Label)(r.Cells[0].FindControl("lb_id"))).Text);
string sql = "删除语句 ";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["连接字符串"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.DataBind();
}
}
哦 这样 下面 我的代码希望对你有用
if (r.RowType == DataControlRowType.DataRow)
{
chk = (CheckBox)(r.FindControl("ckslt"));
if (chk.Checked)
{
//获得被选中的行编号
key = Convert.ToInt32(this.GridView1.DataKeys[r.RowIndex].Value.ToString());
int id;
id = Convert.ToInt32(((Label)(r.Cells[0].FindControl("lb_id"))).Text);
string sql = "删除语句 ";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["连接字符串"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.DataBind();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
其实不需要radiobutton的,一个按钮就可以取到ID值来了,所有代码贴出!
<asp:GridView ID="GridLog" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
BorderColor="#333" BorderStyle="solid" BorderWidth="1" OnRowDeleting="PublicGridRowDeleting"
GridLines="None" Width="98%" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false" ReadOnly="True"
SortExpression="ID" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您确认删除该规则');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
日志库暂时为空!
</EmptyDataTemplate>
</asp:GridView>
CS代码
protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();
string strSQL;
strSQL = "Delete from systemRole " +
" WHERE id = " + strID;
myBase.NoreturnSql(strSQL);
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('指定规则删除成功!');</script>");
GridBind();
}
<asp:GridView ID="GridLog" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
BorderColor="#333" BorderStyle="solid" BorderWidth="1" OnRowDeleting="PublicGridRowDeleting"
GridLines="None" Width="98%" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false" ReadOnly="True"
SortExpression="ID" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您确认删除该规则');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
日志库暂时为空!
</EmptyDataTemplate>
</asp:GridView>
CS代码
protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();
string strSQL;
strSQL = "Delete from systemRole " +
" WHERE id = " + strID;
myBase.NoreturnSql(strSQL);
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('指定规则删除成功!');</script>");
GridBind();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先在页面的GridView标记里,加一事件 OnRowCommand="GridviewCmd"
然后在后台cs文件里添加一个函数,
当前在GridView里还有一个删除的按钮,你要给这个按钮,加两个数据,一个是
CommandName="delete" 还有一个是参数:CommandArgument=""这个一般是用来绑定表中的主键,以便于删除的时候判断
protected void GridviewCmd( object sender, GridViewCommandEventArgs e )
{
if ( e.CommandName == "delete" )
{
string Sql = "DELETE FROM 绑定这个GridView的数据的表名 WHERE ID=" + e.CommandArgument.ToString();
然后执行这个Sql
}
这里从新绑定GridView的数据源
}
然后在后台cs文件里添加一个函数,
当前在GridView里还有一个删除的按钮,你要给这个按钮,加两个数据,一个是
CommandName="delete" 还有一个是参数:CommandArgument=""这个一般是用来绑定表中的主键,以便于删除的时候判断
protected void GridviewCmd( object sender, GridViewCommandEventArgs e )
{
if ( e.CommandName == "delete" )
{
string Sql = "DELETE FROM 绑定这个GridView的数据的表名 WHERE ID=" + e.CommandArgument.ToString();
然后执行这个Sql
}
这里从新绑定GridView的数据源
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
protected void btnDelete_Click(object sender, System.EventArgs e)
{
SqlConnection con = DB.Getcon();
con.Open();
SqlCommand cmd = new SqlCommand("Delete 表名 where 主键='" + Gridview1.Items[Gridview1.SelectedIndex].Cells[i].Text + "'", con);
cmd.ExecuteNonQuery();
con.close();
bind();
}
是想要这样的吧
{
SqlConnection con = DB.Getcon();
con.Open();
SqlCommand cmd = new SqlCommand("Delete 表名 where 主键='" + Gridview1.Items[Gridview1.SelectedIndex].Cells[i].Text + "'", con);
cmd.ExecuteNonQuery();
con.close();
bind();
}
是想要这样的吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询