Asp.net中GridView使用詳解(轉載)

来源:https://www.cnblogs.com/cxy-sj/archive/2018/03/30/8678162.html
-Advertisement-
Play Games

l GridView無代碼分頁排序 l GridView選中,編輯,取消,刪除 l GridView正反雙向排序 l GridView和下拉菜單DropDownList結合 l GridView和CheckBox結合 l 滑鼠移到GridView某一行時改變該行的背景色方法一 l 滑鼠移到GridV ...


l         GridView無代碼分頁排序

l         GridView選中,編輯,取消,刪除

l         GridView正反雙向排序

l         GridView和下拉菜單DropDownList結合

l         GridViewCheckBox結合

l         滑鼠移到GridView某一行時改變該行的背景色方法一

l         滑鼠移到GridView某一行時改變該行的背景色方法二

l         GridView實現刪除時彈出確認對話框

l         GridView實現自動編號

l         GridView實現自定義時間貨幣等字元串格式

l         GridView實現用“...”代替超長字元串

l         GridView一般換行與強制換行

l         GridView顯示隱藏某一列

l         GridView彈出新頁面/彈出新視窗

l         GridView固定表頭(不用JavaScript只用CSS2行代碼,很好用)

l         GridView合併表頭多重表頭無錯完美版(以合併33行舉例)

l         GridView突出顯示某一單元格(例如金額低於多少,分數不及格等)

l         GridView加入自動求和求平均值小計

l         GridView數據導入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" 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正反雙向排序:

後臺代碼:

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

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

    }

前臺主要代碼:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

                        CellPadding="3" Font-Size="9pt"  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:TemplateField HeaderText="員工性別">

                                <ItemTemplate>

                                    <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# ddlbind()%>' DataValueField="員工性別" DataTextField="員工性別">

                                    </asp:DropDownList>

                                </ItemTemplate>

                            </asp:TemplateField>

                            <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>

5.GridViewCheckBox結合:

後臺代碼:

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 Default5 : System.Web.UI.Page

{

    SqlConnection sqlcon;

    string strCon = "Data Source=(local);Database=北風貿易;Uid=sa;Pwd=sa";

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            bind();

        }

    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)

    {

        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (CheckBox2.Checked == true)

            {

                cbox.Checked = true;

            }

            else

            {

                cbox.Checked = false;

            }

        }

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        sqlcon = new SqlConnection(strCon);

        SqlCommand sqlcom;

        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)

            {

                string sqlstr = "delete from 飛狐工作室 where 身份證號碼='" + GridView1.DataKeys[i].Value + "'";

                sqlcom = new SqlCommand(sqlstr, sqlcon);

                sqlcon.Open();

                sqlcom.ExecuteNonQuery();

                sqlcon.Close();

            }

        }

        bind();

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        CheckBox2.Checked = false;

        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            cbox.Checked = false;

        }

    }

    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, "tb_Member");

        GridView1.DataSource = myds;

        GridView1.DataKeyNames = new string[] { "身份證號碼" };

        GridView1.DataBind();

        sqlcon.Close();

    }

}

前臺主要代碼:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

                        CellPadding="3" Font-Size="9pt"  BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">

                        <FooterStyle BackColor="White" ForeColor="#000066" />

                        <Columns>

                             <asp:TemplateField>

                                <ItemTemplate>

                                    <asp:CheckBox ID="CheckBox1" runat="server" />

                                </ItemTemplate>

                            </asp:TemplateField>

                             <asp:BoundField DataField="身份證號碼" HeaderText="用戶ID" 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>

                     <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged"

                        Text="全選" />

                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />

                    <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="刪除" OnClick="Button2_Click" />

6.滑鼠移到GridView某一行時改變該行的背景色方法一:

做法:

雙擊GridView的OnRowDataBound事件;

在後臺的GridView1_RowDataBound()方法添加代碼,最後代碼如下所示:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    //首先判斷是否是數據行

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                //當滑鼠停留時更改背景色

                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");

                //當滑鼠移開時還原背景色

                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

            }

    }

前臺代碼:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>實現滑鼠划過改變GridView的行背景色清清月兒http://blog.csdn.net/21aspnet </title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="身份證號碼"

            DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">

            <Columns>

                <asp:BoundField DataField="身份證號碼" HeaderText="身份證號碼" ReadOnly="True" SortExpression="身份證號碼" />

                <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />

                <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址" />

                <asp:BoundField DataField="郵政編碼" HeaderText="郵政編碼" SortExpression="郵政編碼" />

            </Columns>

            <FooterStyle BackColor="White" ForeColor="#000066" />

            <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>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:北風貿易ConnectionString1 %>"

            SelectCommand="SELECT top 5 [身份證號碼], [姓名], [員工性別], [家庭住址], [郵政編碼] FROM [飛狐工作室]" DataSourceMode="DataReader"></asp:SqlDataSource>

   

    </div>

    </form>

</body>

</html>

 

7.滑鼠移到GridView某一行時改變該行的背景色方法二:

做法:和上面的一樣就是代碼不同

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        //如果是綁定數據行

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            //滑鼠經過時,行背景色變

            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

            //滑鼠移出時,行背景色變

            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

        }

    }

8.GridView實現刪除時彈出確認對話框:

實現方法:

雙擊GridView的OnRowDataBound事件;

在後臺的GridView1_RowDataBound()方法添加代碼,最後代碼如下所示:

    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[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你確認要刪除:/"" + e.Row.Cells[1].Text + "/"嗎?')");

            }

        }

    }

9.GridView實現自動編號:

實現方法:

雙擊GridView的OnRowDataBound事件;

在後臺的GridView1_RowDataBound()方法添加代碼,最後代碼如下所示:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowIndex != -1)

        {

            int id = e.Row.RowIndex + 1;

            e.Row.Cells[0].Text = id.ToString();

        }

    }

 

註意這時最好把前臺的第一列的表頭該為“編號”,因為以前的第一列被“吃掉”了。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"

                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">

                        <FooterStyle BackColor="White" ForeColor="#000066" />

                        <Columns>

                            <asp:BoundField DataField="身份證號碼" HeaderText="編號" 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>

10.GridView實現自定義時間貨幣等字元串格式:

解決方法:

在asp.NET 2.0中,如果要在綁定列中顯示比如日期格式等,如果用下麵的方法是顯示不了的

<asp :BoundField DataField="CreationDate"

DataFormatString="{0:M-dd-yyyy}"

HeaderText="CreationDate" />

主要是由於htmlencode屬性預設設置為true,已防止XSS攻擊,安全起見而用的,所以,可以有以下兩種方法解決

1、

<asp :GridView ID="GridView1" runat="server">

<columns>

<asp :BoundField DataField="CreationDate"

DataFormatString="{0:M-dd-yyyy}"

HtmlEncode="false"

HeaderText="CreationDate" />

</columns>

</asp>

將htmlencode設置為false即可

另外的解決方法為,使用模版列

<asp :GridView ID="GridView3" runat="server" >

<columns>

<asp :TemplateField HeaderText="CreationDate" >

<edititemtemplate>

<asp :Label ID="Label1" runat="server"

Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>'>

</asp>

</edititemtemplate>

<itemtemplate>

<asp :Label ID="Label1" runat="server"

Text=’<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>

</asp>

</itemtemplate>

</asp>

</columns>

</asp>

前臺代碼:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="身份證號碼"

            DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">

            <Columns>

                <asp:BoundField DataField="身份證號碼" HeaderText="身份證號碼" ReadOnly="True" SortExpression="身份證號碼" />

                <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />

                <asp:BoundField DataField="郵政編碼" HeaderText="郵政編碼" SortExpression="郵政編碼" />

                <asp:BoundField DataField="出生日期" HeaderText="出生日期" SortExpression="出生日期" />

                <asp:BoundField DataField="起薪" HeaderText="起薪" SortExpression="起薪" />

            </Columns>

            <FooterStyle BackColor="White" ForeColor="#000066" />

            <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>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:北風貿易ConnectionString1 %>"

            SelectCommand="SELECT top 5 [出生日期], [起薪], [身份證號碼], [姓名], [家庭住址], [郵政編碼] FROM [飛狐工作室]" DataSourceMode="DataReader"></asp:SqlDataSource>

附錄-常用格式化公式:

{0:C}  貨幣;

{0:D4}由0填充的4個字元寬的欄位中顯示整數;

{0:000.0}四捨五入小數點保留第幾位有效數字;

{0:N2}小數點保留2位有效數字;{0:N2}%   小數點保留2位有效數字加百分號;

{0:D}長日期;{0:d}短日期;{0:yy-MM-dd}   例如07-3-25;;{0:yyyy-MM-dd}  例如2007-3-25

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • mvc:V視圖 Controller: <?php header("Content-Type: text/html; charset=UTF-8"); require'productModel.php'; $porduct = new Porduct(); $a =isset($_GET['a']) ...
  • 迭代器 一、什麼是迭代器 二、為何要有迭代器,什麼是可迭代對象,什麼是迭代器對象 三、迭代器對象的使用 四、for迴圈 五、迭代器的優缺點 優點:1.提供一種統一的,不依賴於索引的迭代方式 2.懶性計算,每次只有一條數據,節省記憶體 缺點:1.無法獲取長度(只有在迭代完畢才能知道有多少值) 2.一次性 ...
  • 本文非原創~~ 指定一個點(源點)到其餘各個頂點的最短路徑,也叫做“單源最短路徑”。例如求下圖中的1號頂點到2、3、4、5、6號頂點的最短路徑。 與Floyd-Warshall演算法一樣這裡仍然使用二維數組e來存儲頂點之間邊的關係,初始值如下。 我們還需要用一個一維數組dis來存儲1號頂點到其餘各個頂 ...
  • import ide; ide.setConfig("editor_font_name","fixedsys"); ...
  • 《C#面向服務WebService從入門到精通》包含以下兩個部分: 一、《C#遠程調用技術WebService修煉手冊【基礎篇】》本次分享課您將學習到以下乾貨知識點:1)、WebService技術調用原理圖。2)、C# WebService常用的幾種調用方式。3)、C# WebService調試小技 ...
  • 記錄日誌時, 經常需要描述對象的狀態發生了怎樣的變化, 以前處理的非常簡單粗暴: a. 重寫class的ToString()方法, 將重要的屬性都輸出來 b. 記錄日誌時: 誰誰誰 由 變更前實例.ToString() 變成 變更後實例.ToString() 但輸出的日誌總是太長了, 翻看日誌時想找 ...
  • RabbitMQ是一種重要的消息隊列中間件,在生產環境中,穩定是第一考慮。RabbitMQ廠家也深知開發者的聲音,穩定、可靠是第一考慮,為了消息傳輸的可靠性傳輸,RabbitMQ提供了多種途徑的消息持久化保證:Exchange持久化、Queue持久化及Message的持久化等。以保證RabbitMQ ...
  • c yield關鍵字的用法 1.yield實現的功能 yield return: 先看下麵的代碼,通過yield return實現了類似用foreach遍曆數組的功能,說明yield return也是用來實現迭代器的功能的。 <! more yield break: 再看下麵的代碼,只輸出了1,2, ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...