ใส่ detail view ใน gridview
Posted พฤศจิกายน 25, 2008
on:ลองทดสอบเอา detail view มาใส่ใน gridview ทำตาม cd สอน
บางคำสั่งก็ใช้ได้บางอันก็ใช้ไม่ได้เหมือนใน cd (อาจจะเกิดจากการมั่วไปมั่วมา)
แล้วในที่สุดก็ได้… แต่พี่ที่ทำงานเค้าต้องการอีกแบบ
คือที่ gridview ปกติจะแสดง รายละเอียดออกมานิดหน่อย
แต่เมื่อเรา้คลิกที่รายละเอียดของแถวนั้น ให้แสดงรายละเอียดทั้งหมดทุกตัวอักษร
ให้แสดงออกมาในแถวนั้นๆ อืมม งงแฮะ
แต่ที่ลองทำนี้ต้อง select ตั้งสองครั้ง
แต่ก็พอใช้ได้ ถูๆ ไถๆ ตามอัถภาพ – -“
บันทึกวิธีทำ
1. ทำหน้า xxx.aspx มา
2. ลาก gridview มา
3. ให้ AutoGenerateColumns=”False”
4. กำหนด DataKeyNames=”member_id”
5. ใน <column></column> ให้ใส่คอลัมน์ของเราเข้าไป เช่น
<asp:BoundField DataField=”member_id” SortExpression=”member_id” headertext=”id” >
</asp:BoundField>
โค๊ดหน้า xxx.aspx ทั้งหมดก็มีโค๊ดดังนี้
<form id=”form1″ runat=”server”>
<asp:Label ID=”lbl1″ runat=”server”></asp:Label>
<asp:GridView ID=”GridView1″ runat=”server” BackColor=”White”
BorderColor=”#CCCCCC” BorderStyle=”None” BorderWidth=”1px” CellPadding=”3″
AutoGenerateColumns=”False” Width=”195px” DataKeyNames=”member_id”>
<RowStyle ForeColor=”#000066″ />
<FooterStyle BackColor=”White” ForeColor=”#000066″ />
<PagerStyle BackColor=”White” ForeColor=”#000066″ HorizontalAlign=”Left” />
<SelectedRowStyle BackColor=”#669999″ Font-Bold=”True” ForeColor=”White” />
<HeaderStyle BackColor=”#006699″ Font-Bold=”True” ForeColor=”White” />
<Columns>
<asp:BoundField DataField=”member_id” SortExpression=”member_id” headertext=”id” >
<HeaderStyle HorizontalAlign=”Left” />
</asp:BoundField>
<asp:BoundField DataField=”first_name” SortExpression=”first_name” headertext=”ชื่อ” >
<HeaderStyle HorizontalAlign=”Left” />
</asp:BoundField>
<asp:BoundField DataField=”last_name” SortExpression=”last_name” headertext=”รายละเอียด” >
<HeaderStyle HorizontalAlign=”Left” />
</asp:BoundField>
<asp:CommandField ShowEditButton=”True” />
<asp:CommandField ShowSelectButton=”True” />
<asp:CommandField ShowDeleteButton=”True” />
</Columns>
</asp:GridView>
<asp:DetailsView ID=”DetailsView1″ runat=”server” Height=”50px” Width=”125px”
AutoGenerateRows=”False”>
</asp:DetailsView>
</form>
6. ในหน้า xxx.aspx.vb มีโค๊ดทั้งหมดดังนี้
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Partial Class showEmp_showsub
Inherits System.Web.UI.Page
Public Enum XCol
detailcol = 2
productid
categoryid
unitprice
unitsinstock
amount
discontinued
End Enum
Private Sub ProductsData(Optional ByVal productname As String = “”)
Dim strsql As String = “select * from tblMembers”
Dim dt As New DataTable
Dim conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings(“connintranet”).ConnectionString)
Dim da As New SqlDataAdapter(strsql, conn1)
da.Fill(dt)
dt.Columns.Add(“รหัส”, GetType(Integer), “member_id”)
GridView1.DataSource = dt.DefaultView
MyGridBind()
End Sub
Private Sub MyGridBind()
Dim X1() As String = {“member_id”}
GridView1.DataKeyNames = X1
GridView1.DataBind()
End Sub
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
If Not Page.IsPostBack Then
Me.ProductsData()
Me.MyGridBind()
Else
If Session(“data”) Is Nothing Then
Me.ProductsData()
Else
GridView1.DataSource = Session(“data”)
End If
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
Response.Write(“ddddddddd”)
Me.MyGridBind()
End Sub
Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
Dim a1 As DataKey = GridView1.DataKeys(e.NewSelectedIndex)
‘lbl1.Text = CStr(a1(0))
Dim strsql2 As String = “select * from tblMembers where member_id=” & CInt(a1(0))
Dim dt2 As New DataTable
Dim conn2 As New SqlConnection(ConfigurationManager.ConnectionStrings(“connintranet”).ConnectionString)
Dim da2 As New SqlDataAdapter(strsql2, conn2)
da2.Fill(dt2)
DetailsView1.DataSource = dt2
DetailsView1.DataBind()
GridView1.Rows(e.NewSelectedIndex).Cells(XCol.detailcol).Controls.Add(DetailsView1)
End Sub
End Class
ปล.
– ในซัพโพซิเยอร์ Enum XCol นั้นยังงงอยู่ว่าใช้ทำอะไรกันแน่ ถ้าเอาไว้อ้างคอลัมน์ ทำไมต้องประกาศออกมาเยอะแยะ (เพราะไม่ได้ทำตามขั้นตอนตั้งแต่แผ่นแรก)
– ข้องใจกับการ connect database จากที่เคยเขียน php พี่ๆ เค้ามักบอกว่าถ้าเราเปิด connect ไว้โดยไม่ต้องปิด จะทำให้เว็บเร็วเพราะการที่เปิดประตูไว้ เมื่อคนจะเข้าจะออกก็ไม่ต้องเสียเวลาปิดประตู(ปิด connect) แต่เท่าที่เห็น asp.net เน้นการ ปิด connect แล้วตกลงอันใหนดีกว่ากันล่ะ ???
ช่วงนี้เครียดๆ เลยอยากเปลี่ยนทรงผม เห็นในอินเตอร์เน็ตทรงเก๋กู๊ดกันทั้งนั้น
เลยตัดหน้าตัวเองใส่หัวคนอื่นซะเลย สวยเช้ง
แต่เพื่อนทักว่า แก่…
หมดอารมณ์ที่จะรีทัชต่อ
เซ็งเป็ด (- -“)( -*-) ไปไกลๆ เลยไป๊
4 Responses to "ใส่ detail view ใน gridview"
ทั้งเก่งทั้งน่ารัก
DataKeyNames คืออะไรเหรอครับ
1 | iii
มกราคม 22, 2009 ที่ 10:43 pm
น่ารักอิกแบบ นะ คุยกันนะครับ