後台制定Gridview欄位
個人隨手筆記
谷哥內也有許多資料
剛好這次專題用VB寫
整理出大概三種寫個人比較喜歡一跟三
頁前拉了三個GridView 、二個SqlDataSource 、一個Button
1: <form id="form1" runat="server">
2: <div>
3: <br />
4: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
5: EnableModelValidation="True">
6: <Columns>
7: <asp:BoundField DataField="Day" DataFormatString="{0:yyyy-MM-dd}"
8: HeaderText="日期" SortExpression="Day" />
9: <asp:BoundField DataField="Product_name" HeaderText="品名"
10: SortExpression="Product_name" />
11: <asp:BoundField DataField="Lunch" HeaderText="午餐" SortExpression="Lunch" />
12: <asp:BoundField DataField="Dinner" HeaderText="晚餐"
13: SortExpression="Dinner" />
14: <asp:BoundField DataField="Manufacturers" HeaderText="廠商"
15: SortExpression="Manufacturers" />
16:
17: </Columns>
18: </asp:GridView>
19: 1<br />
20: <asp:SqlDataSource ID="SqlDataSource1" runat="server"
21: ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
22: SelectCommand="">
23: </asp:SqlDataSource>
24: <asp:SqlDataSource ID="SqlDataSource2" runat="server"
25: ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
26: SelectCommand="">
27: </asp:SqlDataSource>
28: <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
29: EnableModelValidation="True">
30: </asp:GridView>
31: 2<br />
32: <asp:GridView ID="GridView3" runat="server">
33: </asp:GridView>
34: 3<br />
35: <asp:Button ID="Button1" runat="server" Text="Button" />
36: </div>
37: </form>
全寫在Button1_CLick事件內
第一種的寫法可以前後台自行做調整配合的是GridView1顯示 如時間格式 yyyy-MM-dd
1: SqlDataSource1.SelectCommand = "SELECT [Day], [Product_name], [Lunch], [Dinner], [Manufacturers] FROM [ordering]"
2: GridView1.DataSource = SqlDataSource1
3: GridView1.DataBind()
第二種寫法是屬於把第一種的資料欄位和繫結在後台上
1: SqlDataSource2.SelectCommand = "SELECT [Day], [Product_name], [Lunch], [Dinner], [Manufacturers] FROM [ordering]"
2: GridView2.DataSource = SqlDataSource2
3: Dim myDateTimeField As New BoundField()
4: myDateTimeField.DataField = "Day"
5: myDateTimeField.HeaderText = "日期"
6: myDateTimeField.DataFormatString = "{0:yyyy-MM-dd}"
7: myDateTimeField.HtmlEncode = "false"
8: GridView2.Columns.Insert(0, myDateTimeField)
9:
10: Dim myDateTimeField1 As New BoundField()
11: myDateTimeField1.DataField = "Product_name"
12: myDateTimeField1.HeaderText = "品名"
13: myDateTimeField1.HtmlEncode = "false"
14: GridView2.Columns.Insert(1, myDateTimeField1)
15:
16: Dim myDateTimeField13 As New BoundField()
17: myDateTimeField13.DataField = "Lunch"
18: myDateTimeField13.HeaderText = "午餐"
19: myDateTimeField13.HtmlEncode = "false"
20: GridView2.Columns.Insert(2, myDateTimeField13)
21:
22: Dim myDateTimeField4 As New BoundField()
23: myDateTimeField4.DataField = "Dinner"
24: myDateTimeField4.HeaderText = "晚餐"
25: myDateTimeField4.HtmlEncode = "false"
26: GridView2.Columns.Insert(3, myDateTimeField4)
27:
28: Dim myDateTimeField5 As New BoundField()
29: myDateTimeField5.DataField = "Manufacturers"
30: myDateTimeField5.HeaderText = "廠商"
31: myDateTimeField5.HtmlEncode = "false"
32: GridView2.Columns.Insert(4, myDateTimeField5)
33: GridView2.DataBind()
第三種方法也算是個人覺得滿喜歡用的-方法運用是先把資料Fill 自己設定預存表單內,再透過自己新增欄位格式表單之後
在按照此表格方式讀取一一的塞入表單內,即可完成
首先在上面需宣告 Imports System.Data
因個人寫法所以在上面需加上
Imports System.Data.SqlClient
Imports System.Web.Configuration
1: Dim myDataset As New DataSet()
2: Dim Conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString.ToString())
3: Conn.Open()
4: Dim sqlstr As String = "SELECT [Day], [Product_name], [Lunch], [Dinner], [Manufacturers] FROM [ordering]"
5: Dim myAdapter As New SqlDataAdapter(sqlstr, Conn)
6:
7: myAdapter.Fill(myDataset, "out_ordering_viw")
8: Dim mytable As DataTable = myDataset.Tables("out_ordering_viw")
9:
10: '建立資料表
11: Dim vtables As New DataTable()
12: Dim Workrow As DataRow '資料列
13: vtables.Columns.Add(New DataColumn("日期")) '標題欄
14: vtables.Columns.Add(New DataColumn("品名")) '01
15: vtables.Columns.Add(New DataColumn("午餐")) '02
16: vtables.Columns.Add(New DataColumn("晚餐")) '03
17: vtables.Columns.Add(New DataColumn("廠商")) '04
18:
19:
20: For i As Integer = 0 To myDataset.Tables("out_ordering_viw").Rows.Count - 1
21:
22: Workrow = vtables.NewRow()
23: Workrow(0) = DateTime.Parse(myDataset.Tables("out_ordering_viw").Rows(i)(0).ToString()).ToString("yyyy-MM-dd")
24: Workrow(1) = myDataset.Tables("out_ordering_viw").Rows(i)(1).ToString()
25: Workrow(2) = myDataset.Tables("out_ordering_viw").Rows(i)(2).ToString()
26: Workrow(3) = myDataset.Tables("out_ordering_viw").Rows(i)(3).ToString()
27: Workrow(4) = myDataset.Tables("out_ordering_viw").Rows(i)(4).ToString()
28: vtables.Rows.Add(Workrow)
29: Next
30: GridView3.DataSource = New DataView(vtables)
31: GridView3.DataBind()
32: Conn.Close()
以上大概整理許多網頁自己整理出的心得
不過看過有人男女格式自訂DataFormatString="{0:男;;女}
個人去測試用不太出來,不過也是可用別的方法解決...
不過總是好奇為什麼不能用!!
大家一起加入blogads 賺零用錢!!