asp.net gridview 72般绝技第1/2页

GridView无代码分页排序
GridView选中,编辑,取消,删除
GridView正反双向排序
GridView和下拉菜单DropDownList结合
GridView和CheckBox结合
鼠标移到GridView某一行时改变该行的背景色方法一
鼠标移到GridView某一行时改变该行的背景色方法二
GridView实现删除时弹出确认对话框
GridView实现自动编号
GridView实现自定义时间货币等字符串格式
GridView实现用“...”代替超长字符串
GridView一般换行与强制换行
GridView显示隐藏某一列
GridView弹出新页面/弹出新窗口
GridView固定表头(不用javascript只用CSS,2行代码,很好用)
GridView合并表头多重表头无错完美版(以合并3列3行举例)
GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
GridView加入自动求和求平均值小计
GridView数据导入Excel/Excel数据读入GridView


1.GridView无代码分页排序

效果图:

1.AllowSorting设为True,aspx代码中是AllowSorting="True";
2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。
3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。

2.GridView选中,编辑,取消,删除:

效果图:

后台代码:
你可以使用sqlhelper,本文没用。代码如下:
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
{

//清清月儿http://blog.csdn.net/21aspnet 
    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" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" 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" />
                        </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" />
                    </asp:GridView>

3.GridView正反双向排序:
效果图:点姓名各2次的排序,点其他也一样可以。

后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default3 : System.Web.UI.Page
{

//清清月儿的博客http://blog.csdn.net/21aspnet 
    SqlConnection sqlcon;
    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SortOrder"] = "身份证号码";
            ViewState["OrderDire"] = "ASC";
            bind();
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sPage = e.SortExpression;
        if (ViewState["SortOrder"].ToString() == sPage)
        {
            if (ViewState["OrderDire"].ToString() == "Desc")
                ViewState["OrderDire"] = "ASC";
            else
                ViewState["OrderDire"] = "Desc";
        }
        else
        {
            ViewState["SortOrder"] = e.SortExpression;
        }
        bind();
    }

public void bind()
    {

string sqlstr = "select top 5 * from 飞狐工作室";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "飞狐工作室");
        DataView view = myds.Tables["飞狐工作室"].DefaultView;
        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        view.Sort = sort;
        GridView1.DataSource = view;
        GridView1.DataBind();
        sqlcon.Close();
    }
}

前台主要代码:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                        CellPadding="3" Font-Size="9pt" OnSorting="GridView1_Sorting" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                             <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
                            <asp:BoundField DataField="员工性别" HeaderText="性别" SortExpression="员工性别"/>
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>

</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" />
                    </asp:GridView>

4.GridView和下拉菜单DropDownList结合:

效果图:

后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default4 : System.Web.UI.Page
{
    SqlConnection sqlcon;
    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList ddl;
        if (!IsPostBack)
        {
            string sqlstr = "select top 5 * from 飞狐工作室";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "飞狐工作室");
            GridView1.DataSource = myds;
            GridView1.DataBind();
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
                if (Convert.ToString(mydrv["员工性别"]).Trim() == "True")
                {
                    ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                    ddl.SelectedIndex = 0;
                }
                if (Convert.ToString(mydrv["员工性别"]).Trim() == "False")
                {
                    ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                    ddl.SelectedIndex = 1;
                }
            }
            sqlcon.Close();
        }
    }
    public SqlDataReader ddlbind()
    {
        string sqlstr = "select distinct 员工性别 from 飞狐工作室";
        sqlcon = new SqlConnection(strCon);
        SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        return sqlcom.ExecuteReader();
    }

当前1/2页 12下一页阅读全文

(0)

相关推荐

  • asp.net GridView控件中模板列CheckBox全选、反选、取消

    复制代码 代码如下: using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebC

  • 灵活掌握Asp.net MVC中GridView的使用方法

    本文教程为大家分享了GridView控件的使用方法和具体实现代码,供大家参考,具体内容如下 Models文件下实体类: public class Customer { public int Id { get; set; } public string CompanyName { get; set; } public string ContactTitle { get; set; } public string Address { get; set; } public string City {

  • Asp.net GridView使用大全(分页实现)

    GridView自带的分页功能实现: 要实现GrdView分页的功能操作如下:1.更改GrdView控件的AllowPaging属性为true.2.更改GrdView控件的PageSize属性为 任意数值(默认为10)3.更改GrdView控件的PageSetting->Mode为Numeric等(默认为Numeric)该属性为分页样式.GridView属性设置好了,从页面上也能看到分页样式. 现在开始实现分页的功能: 1.在<<asp:GridView ID=......>后添加

  • asp.net下gridview 批量删除的实现方法第1/3页

    要实现的效果很简单,看下图片: 功能:选中CheckBox,后从数据库中删除选中项.文章侧重将如何实现批量删除,对于如何链接数据库和绑定数据不做详细解释. 1 我们先要在GridView中添加一列为CheckBox.代码如下: 复制代码 代码如下: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">  <Columns>     

  • asp.net GridView 删除时弹出确认对话框(包括内容提示)

    效果图: html代码 复制代码 代码如下: <table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%"> <tr> <th colspan="2"> GridView演示</th> <

  • 灵活掌握asp.net中gridview控件的多种使用方法(下)

    继续上篇文章的学习<灵活掌握asp.net中gridview控件的多种使用方法(上)>,在此基础上巩固gridview控件的操作使用,更上一层楼. 11.GridView实现用"..."代替超长字符串: 效果图: 解决方法:数据绑定后过滤每一行即可 for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { DataRowView mydrv; string gIntro; if (GridView1.PageIndex

  • asp.net gridview代码绑定

    拖一个gridview控件上去,然后在Page_Load里编写代码,当然我是手动用代码绑定的,这里使用了DataAdapter,它的构造函数接受了一个sql字符串和Connection对象,用它可以不必打开和关闭Connection对象,DataAdapter会自行处理,还用到了DateSet,这里新建了一个空的DateSet对象,并调用DataAdapter的Fill方法填充数据,然后通过设定GridView的数据源,调用它的DataBind方法就实现了数据绑定. 程序代码 复制代码 代码如下

  • Asp.net简单代码设置GridView自适应列宽不变形实现思路与代码

    动态绑定的GridView由于列数不固定,而列又太多(博主做的这个项目有150个左右的字段),这样设置GridView固定宽度就不能满足需求了.为此整理了两种方法来达到GridView自适应列宽不变形的效果. .aspx.cs 复制代码 代码如下: //在GridView的行数据绑定完的事件中设置 protected void gvObjectList_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType

  • ASP.NET 2.0/3.5中直接操作Gridview控件插入新记录

    一.简介 从ASP.NET 2.0起引入了一批新的功能强大的视图控件,例如Gridview.FormView和DetailsView等等.通过和数据源控件的简单结合,在许多情况下,仅需要简单的配置方式就可以开发出功能强大的应用程序.但遗憾的是,Gridview控件中并没有提供像在FormView和DetailsView控件中那样直接插入新记录操作的支持.图1给出了典型的使用控件展示数据库数据的情形. 图1 使用GridView控件展示VS2005示例数据库Address表格中的数据熟悉Gridv

  • ASP.NET数据绑定之GridView控件

    GridView 是 DataGrid的后继控件,在.net framework 2 中,虽然还存在DataGrid,但是GridView已经走上了历史的前台,取代DataGrid的趋势已是势不挡.  作用:其功能是在web页面中显示数据源中的数据.GridView和DataGrid功能相似,都是在web页面中显示数据源中的数据,将数据源中的一行数据,也就是一条记录,显示为在web页面上输出表格中的一行.     在此GirdView的详细属性和事件我不再阐述.下面我只是简单介绍一下GirdVi

  • 灵活掌握asp.net中gridview控件的多种使用方法(上)

    灵活使用asp.net中gridview控件的方法有很多种,本文内容很富,希望大家都能有所收获. 1.GridView无代码分页排序: 效果图: 小提示: 1.AllowSorting设为True,aspx代码中是AllowSorting="True": 2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12". 3.默认的是单向排序的,右击GridView弹出"属性",选择AllowSort

随机推荐