一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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();
    }

AspNet技術asp.net gridview 72般絕技第1/2頁,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 欧美日韩免费大片 | 亚洲国产精久久小蝌蚪 | 看免费5xxaaa毛片 | 国产男人和女人做性全部视频 | 亚洲图片激情 | 国产精品免费视频网站 | 丁香婷婷在线 | 男女午夜性爽快免费视频不卡 | 最新亚洲情黄在线网站 | 99精品视频在线成人精彩视频 | 亚洲免费一区二区 | 婷婷在线影院 | 亚洲一区二区三区深夜天堂 | 久久婷婷久久一区二区三区 | 午夜视频国产 | 四虎在线播放免费永久视频 | 久久亚洲国产视频 | 国产精品无内丝袜高跟鞋 | 丁香六月色婷婷 | 欧美激情伊人 | 国产成人精品久久亚洲高清不卡 | 欧美成人午夜视频 | 国产精品网红女主播久久久 | 美女免费精品视频在线观看 | 搞黄网站免费看 | 色老板在线观看永久免费视频 | 成人免费小视频 | 国产免费午夜 | 久久精品国产亚洲综合色 | 色婷婷综合在线 | 亚洲国产午夜 | 国模大胆一区二区三区 | 色视频在线免费观看 | 亚洲情趣 | 四虎综合九九色九九综合色 | 开心丁香婷婷深爱五月 | 69国产成人精品午夜福中文 | 国产精品一区二区三区高清在线 | 免费观看欧美一级特黄 | 在线永久免费观看黄网站 | 中文字幕在线一区二区三区 |