web页 <asp:GridView ID="GridView1" runat="server"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>cs页 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sConnectionString; // Modify the following string to correctly connect to your SQL Server. sConnectionString = "Password=sa;User ID=sa;" + "Initial Catalog=wzdb;" + "Data Source=(local)"; SqlConnection conn = new SqlConnection(sConnectionString); conn.Open(); DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from gc_table"; cmd.ExecuteNonQuery(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(ds); conn.Close(); conn.Dispose(); GridView1.DataSource = ds; GridView1.DataBind();
} } protected void LinkButton1_Click(object sender, EventArgs e) { Control col = sender as Control; int index = ((GridViewRow)col.Parent.Parent).RowIndex; GridView1.Rows[index].Visible = false; }
然后如下
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick='<%# "if (!confirm(\"你确定要删除" + Eval("字段名称").ToString() + "吗?\")) return false;"%>'></asp:LinkButton>
</ItemTemplate>
如果仅仅是在gridview中删除,用gridview1.rows.Remove
==在Gridview 添加一列
<asp:TemplateColumn HeaderText="操作">
<itemstyle horizontalalign="Center" />
<itemtemplate>
<cc1:LinkButton id="lbActivate" runat="server" CommandName="delete" onclientclick="return window.confirm('如果删除该人员将无法恢复,是否要删除该人员?');"></cc1:LinkButton>
</itemtemplate>
<headerstyle horizontalalign="Center" width="15%" />
</asp:TemplateColumn>然后 通过,Gridview 的ItemCommand事件 protected void DataGridUserList_ItemCommand(object source, DataGridCommandEventArgs e)
{
string id = this.DataGridUserList.DataKeys[e.Item.ItemIndex].ToString();
if (e.CommandName == "delete")
{ if ()//写自己调用 底层的方法,或 sql 语句
{
Popwin.ShowMessage("删除成功!", this.Page); }
else
{
Popwin.ShowMessage("删除失败!", this.Page);
}
} //删除完后在把数据绑定一便
//this.DataGridUserListDataBind(); } }
==在Gridview 添加一列
<asp:TemplateColumn HeaderText="操作">
<itemstyle horizontalalign="Center" />
<itemtemplate>
<cc1:LinkButton id="lbActivate" runat="server" CommandName="delete" onclientclick="return window.confirm('如果删除该人员将无法恢复,是否要删除该人员?');"></cc1:LinkButton>
</itemtemplate>
<headerstyle horizontalalign="Center" width="15%" />
</asp:TemplateColumn>//然后 通过,Gridview 的ItemCommand事件 protected void DataGridUserList_ItemCommand(object source, DataGridCommandEventArgs e)
{
string id = this.DataGridUserList.DataKeys[e.Item.ItemIndex].ToString();
if (e.CommandName == "delete")
{ if ()//写自己调用 底层的方法,或 sql 语句
{
Popwin.ShowMessage("删除成功!", this.Page); }
else
{
Popwin.ShowMessage("删除失败!", this.Page);
}
} //删除完后在把数据绑定一便
//this.DataGridUserListDataBind(); } }
{
string id = this.DataGridUserList.DataKeys[e.Item.ItemIndex].ToString();
if (e.CommandName == "delete")
{ if ()//写自己调用 底层的方法,或 sql 语句
{
Popwin.ShowMessage("删除成功!", this.Page); }
else
{
Popwin.ShowMessage("删除失败!", this.Page);
}
} //删除完后在把数据绑定一便
//this.DataGridUserListDataBind(); } }
DataTable dtwhere=new DataTable()
CheckBox chk;
//循环遍历GridView行数据
foreach (GridViewRow row in this.grv_OutInter.Rows)
{
//判断是否为数据行
if (row.RowType == DataControlRowType.DataRow)
{
DataRow dr;
dr = dtwhere.NewRow(); chk = (CheckBox)row.FindControl("你页面的CheckBoxID");//获取控件ID
dr[“字段名称”] = 值;//用户ID
.....
dr[“字段名称”] = Convert.ToString(chk.Checked);//是否选中
dtwhere.Rows.Add(dr);//添加到dtwhere
}
}
链接数据源
DbConnection conn = null;//数据库链接
.....
后面的不用我写了吧!·
列如(UpdateTempBySelect(dtwhere);//删除方法)
List<CourseInfo> list = ViewState[""+hfClassId.Value+""] as List<CourseInfo>;
CourseInfo course = list.Find(ee=>ee.Code==courseid);
if(course!=null)
list.Remove(course);
gvClassCourse.DataSource = list;
gvClassCourse.DataBind();
下载这个文件 里面有一个 用checkbox 和gridview控件实现删除的例子...
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>cs页
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sConnectionString; // Modify the following string to correctly connect to your SQL Server.
sConnectionString = "Password=sa;User ID=sa;"
+ "Initial Catalog=wzdb;"
+ "Data Source=(local)"; SqlConnection conn = new SqlConnection(sConnectionString);
conn.Open(); DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from gc_table";
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
conn.Close();
conn.Dispose(); GridView1.DataSource = ds;
GridView1.DataBind();
}
} protected void LinkButton1_Click(object sender, EventArgs e)
{
Control col = sender as Control;
int index = ((GridViewRow)col.Parent.Parent).RowIndex;
GridView1.Rows[index].Visible = false;
}
要是要从数据库中删除,上面有答案。