-
09-09-2011, 12:10 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Đổ Dữ Liệu Vào DropDownList trong GridView
mình đang làm web đơn giản là có 1 gridview, được load dữ liệu từ cột
CongViec(maCongViec,TenCongViec,MaLoaiCongViec). trong GridView có 1 DropDownList chứa TenLoaiCongViec từ bảng LoaiCongViec(MaLoaiCongViec,TenLoaiCongViec). Như vậy mình sẽ phải cho dữ liệu vào dropdownlist thế nào.mong các bạn giúp
Code designMã:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="MaCongViec" HeaderText="Mã Công Việc" /> <asp:BoundField DataField="TenCongViec" HeaderText="Tên Công Việc" /> <asp:TemplateField HeaderText="Loại Công Việc"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" Width="150" > </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Mã:Public Function getList(ByVal q As String) Dim conn As SqlConnection Dim da As SqlDataAdapter conn = New SqlConnection("Server =(local);Integrated Security = True;Database = MoTaCongViec") da = New SqlDataAdapter(q, conn) Dim ds As DataSet ds = New DataSet conn.Open() Try da.Fill(ds) Catch ex As Exception End Try conn.Close() Return ds End Function Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load GridView1.DataSource = getList(" select * from Cong_Viec") GridView1.DataBind() End Sub
View more random threads:
- Làm "Giỏ Hàng" trong lập trình ASP.NET?
- Lổi khi mở chế độ Desginer - Có Liên quan đến DevExpress
- Bộ đếm Người Online 3.5 đơn giản cho anh em này !
- Thắc mắc về phương thức truyền dữ liệu giữa 2 trang bằng phương thức post
- Lỗi asp:UpdatePanel’ could not be set on property ‘ContentTemplate’ trong vs 2008
- nhúng file CSS vào trong ASP NET
- Truyền tham số cho nhiều trang
- Chuỗi kí tự sau khi loại bỏ khoảng trắng lại trở thành System.Web.UI.WebControls.TextBox?
- Ẩn 1 trang với Khách trong ASP.Net Web MVC 2 như thế nào ?
- Xóa nội dung trong textbox khi click chuột vào
-
09-09-2011, 05:31 PM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
[IMG]images/smilies/daydreaming.gif[/IMG] Cái nè bạn cần phải dùng hàm Findcontrol để lấy ra được cái dropdownlist trong gridview.
Khi bạn Bind dữ liệu cho gridview xong, thì duyệt toàn bộ các hàng, với mỗi hàng thì dùng hàm Findcontrol=>ép kiểu về dropdownlist. Sau đó, gán source cho cái dropdownlist kia là ok.
-
12-09-2011, 09:59 AM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Nếu mình đoán ko lầm thì ý của bạn là vậy phải ko
Mã:GridView1.DataSource = getList(" select * from Cong_Viec") GridView1.DataBind() Dim row As GridViewRow Dim ddl As DropDownList For Each row In GridView1.Rows ddl = CType(GridView1.Rows(row.RowIndex).Cells(2).FindControl("DropDownList1"), DropDownList) ddl.DataSource = getList("select * from Loai_Cong_Viec") ddl.DataTextField = "TenLoaiCongViec" ddl.DataValueField = "MaLoaiCongViec" ddl.DataBind() Next
-
12-09-2011, 03:50 PM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
mình làm dc rồi và post lên lun henh
Mã:<asp:TemplateField HeaderText="Loại Công Việc"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" Width="150" OnLoad="DropDownList1_load"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField>
Mã:Protected Sub DropDownList1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim ddl As DropDownList = CType(sender, DropDownList) ddl.DataSource = getList("select * from Loai_Cong_Viec") ddl.DataTextField = "TenLoaiCongViec" ddl.DataValueField = "MaLoaiCongViec" ddl.DataBind() End Sub
-
12-09-2011, 06:50 PM #5Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
[IMG]images/smilies/daydreaming.gif[/IMG]hi. Chính là do bạn viết cái load dữ liệu trong sự kiện load của dropdownlist nên ko làm được việc đó. Còn nếu muốn tùy chỉnh load gì đó, thì bạn làm theo cái cách tớ bảo ấy. Đó là:
+đổ dữ liệu cho gridview ngon lành trước đã.
+Duyệt từng hàng của gridview
+trên mỗi hàng thực hiện 2 việc:
1.lấy ra tên công việc ở cột cv of gridview. Cái này dùng FindControl("label hiển thị tên công việc").
2.Lấy ra dropdownlist trên hàng của gridview. Sau đó Fill Data cho dropdownlist theo cái tên cv lấy ở bước 1.
thế là xong. bạn thử coi[IMG]images/smilies/1.gif[/IMG]
-
08-10-2014, 12:14 PM #6Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Mã:foreach (GridViewRow row in gridview1.Rows) { DropDownList ddl = (DropDownList)row.FindControl("drodownlist1"); ddl.DataSource = datasource; ddl.DataTextField = "text"; ddl.DataValueField = "value"; ddl.DataBind(); }
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ