C# 怎样删除GridView中的数据
4个回答
展开全部
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4" ForeColor="#333333"
GridLines="None" PageSize="15" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField HeaderText="LeaveWordId" DataField="LeaveWordId"
SortExpression="LeaveWordId" />
<asp:BoundField HeaderText="PersonId" DataField="PersonId"
SortExpression="PersonId" />
<asp:BoundField HeaderText="LeaveBy" DataField="LeaveBy"
SortExpression="LeaveBy" />
<asp:BoundField HeaderText="IpSource" DataField="IpSource"
SortExpression="IpSource" />
<asp:BoundField DataField="Content" HeaderText="Content"
SortExpression="Content" />
<asp:BoundField DataField="SubmitDatetime" HeaderText="SubmitDatetime"
SortExpression="SubmitDatetime" />
<asp:CommandField HeaderText="Operation" ShowDeleteButton="True"
ShowHeader="True" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="SelectByPersonId" TypeName="BL.Pt_LeaveWords_BLL">
<SelectParameters>
<asp:SessionParameter DefaultValue="98" Name="PersonId"
SessionField="Common.SessionName.PERSON_ID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
GridLines="None" PageSize="15" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField HeaderText="LeaveWordId" DataField="LeaveWordId"
SortExpression="LeaveWordId" />
<asp:BoundField HeaderText="PersonId" DataField="PersonId"
SortExpression="PersonId" />
<asp:BoundField HeaderText="LeaveBy" DataField="LeaveBy"
SortExpression="LeaveBy" />
<asp:BoundField HeaderText="IpSource" DataField="IpSource"
SortExpression="IpSource" />
<asp:BoundField DataField="Content" HeaderText="Content"
SortExpression="Content" />
<asp:BoundField DataField="SubmitDatetime" HeaderText="SubmitDatetime"
SortExpression="SubmitDatetime" />
<asp:CommandField HeaderText="Operation" ShowDeleteButton="True"
ShowHeader="True" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="SelectByPersonId" TypeName="BL.Pt_LeaveWords_BLL">
<SelectParameters>
<asp:SessionParameter DefaultValue="98" Name="PersonId"
SessionField="Common.SessionName.PERSON_ID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Image3.Attributes.Add("onclick", "javascript:return confirm('确定要删除所选的项吗!');"); // Image3是ID号
if (e.CommandName == "Del") //此处要把删除按钮的CommandName设置为“Del”
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string id = e.CommandArgument.ToString();
string strsql = "delete from 表名 where ID=" + id;
SqlCommand comm = new SqlCommand(strsql, conn);
comm.ExecuteNonQuery();
//调用重置函数
reset();
}
}
//重置GridView1中的数据
public void reset()
{
GridView1.DataSourceID = "SqlDataSource1";
//GridView1.DataBind();
}
{
Image3.Attributes.Add("onclick", "javascript:return confirm('确定要删除所选的项吗!');"); // Image3是ID号
if (e.CommandName == "Del") //此处要把删除按钮的CommandName设置为“Del”
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string id = e.CommandArgument.ToString();
string strsql = "delete from 表名 where ID=" + id;
SqlCommand comm = new SqlCommand(strsql, conn);
comm.ExecuteNonQuery();
//调用重置函数
reset();
}
}
//重置GridView1中的数据
public void reset()
{
GridView1.DataSourceID = "SqlDataSource1";
//GridView1.DataBind();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是删除这个item 还是清空其中的数据?
如果是连数据带item都删除的话,
DataTable dt ;
foreach(datarow dr in dt.rows)
{
dr.delete();
//只删除数据,即 使得该行的所有数据为空串,但保留该行
//foreach(datacloumn dc in dt.columns)
// dr[dc] = "";
}
如果是连数据带item都删除的话,
DataTable dt ;
foreach(datarow dr in dt.rows)
{
dr.delete();
//只删除数据,即 使得该行的所有数据为空串,但保留该行
//foreach(datacloumn dc in dt.columns)
// dr[dc] = "";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不是有个OnRowDeleting属性嘛,只要获得你要删除的ID就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询