Asp.net中GridView使用详解

发布时间:2017-09-03 11:19:07
Asp.net中GridView使用详解

lGridView正反双向排序

lGridView和下拉菜单DropDownList结合

lGridView和CheckBox结合

l鼠标移到GridView某一行时改变该行的背景色方法一

l鼠标移到GridView某一行时改变该行的背景色方法二

lGridView实现删除时弹出确认对话框

lGridView实现自动编号

lGridView实现自定义时间货币等字符串格式

lGridView实现用“...”代替超长字符串

lGridView一般换行与强制换行

lGridView显示隐藏某一列

lGridView弹出新页面/弹出新窗口

lGridView固定表头(不用只用CSS,2行代码,很好用)

lGridView合并表头多重表头无错完美版(以合并3列3行举例)

lGridView突出显示某一单元格(例如金额低于多少,分数不及格等)

lGridView加入自动求和求平均值小计

lGridView数据导入Excel/Excel数据读入GridView

1.GridView简单代码分页排序:

\

1.AllowSorting设为True,aspx代码中是AllowSorting="True";

2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。

3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。

4.添加代码:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; Bind(); }

2.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; SqlCommand sqlcom; string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } //删除 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlcon = new SqlConnection(strCon); sqlcom = new SqlCommand(sqlstr,sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); bind(); } //更新 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { sqlcon = new SqlConnection(strCon); string sqlstr = "update 表 set 字段1='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlcom=new SqlCommand(sqlstr,sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.EditIndex = -1; bind(); } //取消 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } //绑定 public void bind() { string sqlstr = "select * from 表"; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, "表"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "id" };//主键 GridView1.DataBind(); sqlcon.Close(); } }
前台主要代码: <asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" cellpadding="4" forecolor="#333333" gridlines="None" data-cke-pa-onrowdeleting="GridView1_RowDeleting" data-cke-pa-onrowediting="GridView1_RowEditing" data-cke-pa-onrowupdating="GridView1_RowUpdating" data-cke-pa-onrowcancelingedit="GridView1_RowCancelingEdit"> <footerstyle backcolor="#990000" font-bold="True" forecolor="White"> <columns> <asp:boundfield datafield="身份证号码" headertext="用户ID" readonly="True"> <asp:boundfield datafield="姓名" headertext="用户姓名"> <asp:boundfield datafield="员工性别" headertext="性别"> <asp:boundfield datafield="家庭住址" headertext="家庭住址"> <asp:commandfield headertext="选择" showselectbutton="True"> <asp:commandfield headertext="编辑" showeditbutton="True"> <asp:commandfield headertext="删除" showdeletebutton="True"> </asp:commandfield></asp:commandfield></asp:commandfield></asp:boundfield></asp:boundfield></asp:boundfield></asp:boundfield></columns> <rowstyle forecolor="#000066"> <selectedrowstyle backcolor="#669999" font-bold="True" forecolor="White"> <pagerstyle backcolor="White" forecolor="#000066" horizontalalign="Left"> <headerstyle backcolor="#006699" font-bold="True" forecolor="White"> </headerstyle></pagerstyle></selectedrowstyle></rowstyle></footerstyle></asp:gridview>

3.GridView正反双向排序:

\

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网络营销 http://www.feimao666.com