自行加入自定義的GridView的表頭後,原來GridView裡定義的AllowSorting="True"SortExpression功能便消失,CustomerIDCompanyNameAddressCity欄位標題變為純文字而無法切換正反向排序。

在網路找了許久,有的加入HyperLinksort變數或加入session控制排序方向,或有的加寫GridView1_Sorting。終於找到某連結網址,網址已不可考了,我連結NorthWind資料表Customers當測試,試到目前,以下我的Sample方式,我覺得最符合我的需求,只需控制到GridView1_RowCreated即可,原來定義的AllowSorting="True"SortExpression依舊有效。

  

 

 

VB

 

<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="system.web.httputility" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

 

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

 

End Sub

 

Protected Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.Header Then

Dim tcc As TableCellCollection = e.Row.Cells

Dim intcount As Integer = tcc.Count   '前端網頁碼定義Columns裡的欄位數量,此例為4 = CustomerID、CompanyName、Address、City

 

tcc.Add(New TableHeaderCell())

tcc(4).Attributes.Add("colspan", "4")   '由前端網頁碼定義Columns裡的欄位數量後開始編號,此例為0,1,2,3後,使用tcc(4)

tcc(4).Attributes.Add("bgcolor", "#00ff00")

tcc(4).Text = "第一列跨行測試</th></tr><tr>"   '換列要加</tr><tr>

tcc.Add(New TableHeaderCell())

tcc(5).Attributes.Add("colspan", "2")

tcc(5).Attributes.Add("rowspan", "2")

tcc(5).Attributes.Add("bgcolor", "#ff00ff")

tcc(5).Text = "第二列跨行跨列測試一</th>"

tcc.Add(New TableHeaderCell())

tcc(6).Attributes.Add("colspan", "2")

tcc(6).Attributes.Add("bgcolor", "#00ffff")

tcc(6).Text = "第二列跨行測試二</th></tr><tr>"   '換列要加</tr><tr>

tcc.Add(New TableHeaderCell())

tcc(7).Attributes.Add("bgcolor", "#444444")

tcc(7).Text = "第三列跨行測試一</th>"

tcc.Add(New TableHeaderCell())

tcc(8).Attributes.Add("bgcolor", "#777777")

tcc(8).Text = "第三列跨行測試二</th></tr><tr>"   '換列要加</tr><tr>

 

For i As Integer = 0 To intcount -1

tcc.Add(tcc(0))   '開始加入CustomerID、CompanyName、Address、City

Next

 

'以上layout完成後,tcc(編號)為由左至右由上至下開始重新排列;tcc(0)為第一列跨行測試、tcc(1)為第二列跨行跨列測試一、tcc(2)為第二列跨行測試二、tcc(3)為第三列跨行測試一、tcc(4)為第三列跨行測試二、tcc(5)為CustomerID、tcc(6)為CompanyName、tcc(7)為Address、tcc(8)為City

 

tcc(5).Attributes.Add("bgcolor", "#0000ff")

tcc(6).Attributes.Add("bgcolor", "#0000bb")

tcc(7).Attributes.Add("bgcolor", "#000077")

tcc(8).Attributes.Add("bgcolor", "#000033")

End If

End Sub

 

 

</script>

 

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

</head>

 

<body style="FONT: 10pt 細明體">

    <form runat="server">

<center>

<asp:Label id="message" runat="server" font-bold="True" font-size="18"/>

</center>

<asp:GridView ID="GridView1" runat="server" DataKeyNames="CustomerID" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="SqlDataSource1" Font-Size="10pt" BorderColor="#999999" BorderStyle="Solid" AllowSorting="True" AllowPaging="True" PageSize="10" OnRowCreated="GridView1_RowCreated" >

<FooterStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />

<Columns>

<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" readonly="true" SortExpression="CustomerID" ItemStyle-HorizontalAlign="Right" />

<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" readonly="true" SortExpression="CompanyName" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="200px" />

<asp:BoundField DataField="Address" HeaderText="Address" readonly="true" SortExpression="Address" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="200px" />

<asp:BoundField DataField="City" HeaderText="City" readonly="true" SortExpression="City" ItemStyle-HorizontalAlign="Right" />

</Columns>

<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

<HeaderStyle BackColor="#999999" Font-Bold="False" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ connectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Customerid, CompanyName, Address, City from Customers" >

</asp:SqlDataSource>

    </form>

</body>

</html>

 

 

C#

 

<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System" %>

<%@ Import Namespace="System.Web" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.Sql" %>

<%@ Import Namespace="System.Data.SqlClient" %>

 

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

 

}

}

 

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.Header) {

TableCellCollection tcc = e.Row.Cells;

int intcount = tcc.Count;   //前端網頁碼定義Columns裡的欄位數量,此例為4 = CustomerID、CompanyName、Address、City

 

tcc.Add(new TableHeaderCell());

tcc[4].Attributes.Add("colspan", "4");   //由前端網頁碼定義Columns裡的欄位數量後開始編號,此例為0,1,2,3後,使用tcc(4]

tcc[4].Attributes.Add("bgcolor", "#00ff00");

tcc[4].Text = "第一列跨行測試</th></tr><tr>";   //換列要加</tr><tr>

tcc.Add(new TableHeaderCell());

tcc[5].Attributes.Add("colspan", "2");

tcc[5].Attributes.Add("rowspan", "2");

tcc[5].Attributes.Add("bgcolor", "#ff00ff");

tcc[5].Text = "第二列跨行跨列測試一</th>";

tcc.Add(new TableHeaderCell());

tcc[6].Attributes.Add("colspan", "2");

tcc[6].Attributes.Add("bgcolor", "#00ffff");

tcc[6].Text = "第二列跨行測試二</th></tr><tr>";   //換列要加</tr><tr>

tcc.Add(new TableHeaderCell());

tcc[7].Attributes.Add("bgcolor", "#444444");

tcc[7].Text = "第三列跨行測試一</th>";

tcc.Add(new TableHeaderCell());

tcc[8].Attributes.Add("bgcolor", "#777777");

tcc[8].Text = "第三列跨行測試二</th></tr><tr>";   //換列要加</tr><tr>

 

for (int i = 0; i <= intcount - 1; i++) {

tcc.Add(tcc[0]);   //開始加入CustomerID、CompanyName、Address、City

}

 

//以上layout完成後,tcc[編號]為由左至右由上至下開始重新排列;tcc[0]為第一列跨行測試、tcc[1]為第二列跨行跨列測試一、tcc[2]為第二列跨行測試二、tcc[3]為第三列跨行測試一、tcc[4]為第三列跨行測試二、tcc[5]為CustomerID、tcc[6]為CompanyName、tcc[7]為Address、tcc[8]為City

 

tcc[5].Attributes.Add("bgcolor", "#0000ff");

tcc[6].Attributes.Add("bgcolor", "#0000bb");

tcc[7].Attributes.Add("bgcolor", "#000077");

tcc[8].Attributes.Add("bgcolor", "#000033");

}

}

 

 

</script>

<html>

<head id="Head1" runat="server">

<title></title>

</head>

<body>

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

<center>

<asp:Label id="message" runat="server" font-bold="True" font-size="18"/>

</center>

<asp:GridView ID="GridView1" runat="server" DataKeyNames="CustomerID" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="SqlDataSource1" Font-Size="10pt" BorderColor="#999999" BorderStyle="Solid" AllowSorting="True" AllowPaging="True" PageSize="10" OnRowCreated="GridView1_RowCreated" >

<FooterStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />

<Columns>

<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" readonly="true" SortExpression="CustomerID" ItemStyle-HorizontalAlign="Right" />

<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" readonly="true" SortExpression="CompanyName" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="200px" />

<asp:BoundField DataField="Address" HeaderText="Address" readonly="true" SortExpression="Address" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="200px" />

<asp:BoundField DataField="City" HeaderText="City" readonly="true" SortExpression="City" ItemStyle-HorizontalAlign="Right" />

</Columns>

<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

<HeaderStyle BackColor="#999999" Font-Bold="False" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ connectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Customerid, CompanyName, Address, City from Customers" >

</asp:SqlDataSource>

</form>

</body>

</html>

arrow
arrow
    全站熱搜

    adamschen9921 發表在 痞客邦 留言(0) 人氣()