VS2010的GridView,不使用自带的删除按钮,自定义一个删除按钮,删除所在行的信息,怎么弄啊
发布网友
发布时间:2022-04-27 13:02
我来回答
共3个回答
热心网友
时间:2022-04-27 14:31
前台
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5" AllowSorting="True"
AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="3"
Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
Height="221px" Width="100%" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="CID" HeaderText="用户ID" ReadOnly="true">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="用户姓名" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Sex" HeaderText="性别" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="家庭住址" >
<ItemStyle Width="140px" />
</asp:BoundField>
<asp:BoundField DataField="Post" HeaderText="邮政编码" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="true">
<ItemStyle Width="70px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
</asp:GridView>
后台
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=wxd;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定GridView
public void bind()
{
string sqlstr = "select * from Admin";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "Admin");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "CID"};
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int CID = (int)GridView1.DataKeys[e.RowIndex].Value;
string Sql = "delete Admin where CID=" + CID + "";
sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom = new SqlCommand( Sql,sqlcon);
try
{
sqlcom.Connection.Open();
sqlcom.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw ex;
}
sqlcom.Connection.Close();
bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
}
}
}
}
热心网友
时间:2022-04-27 15:49
在编辑列里,添加一个TemplateField字段,然后后台添加删除代码
热心网友
时间:2022-04-27 17:24
用模板列里面放linkbutton imagebutton 等其他按钮,后台写相对应的事件 或者GridView的Commoned 事件都行。