VB.net - 以编程方式更改数据绑定Datagridview的列类型

时间:2017-10-01 05:15:16

标签: vb.net winforms datagridview

有没有办法更改数据库绑定的datagridview的列类型?

到目前为止我有这个代码:

Dim query As String = "SELECT a.class_code as 'Class Code', a.sub_code as 'Subject Title', Gr_Lvl as 'Grade Level', sub_session as 'Session', TIME_FORMAT(start_time, '%h:%i%p') as 'Start Time', TIME_FORMAT(end_time, '%h:%i%p') as 'End Time', sub_day as 'Day', class_room as 'Room', sub_units as 'Units', teacher_name as 'Teacher', sub_remarks as 'Remarks', subloading_no " & _
    "FROM tbl_subloading a INNER JOIN tbl_class b on a.class_code=b.class_code INNER JOIN tbl_subject c on a.sub_code=c.sub_code  WHERE  a.class_code='" & txtClassCode.Text & "' AND sub_semester = '" & UserDataRead1("SELECT semester_desc From tbl_semester where status= 'active'") & "' ORDER BY start_time ASC"
    FillDBGrid(query, dgvClassDist) 

目前的输出:

enter image description here

我只希望主题标题列是组合框而不仅仅是文本字段。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:-1)

如果您正在使用数据网格并让它为您自动生成列,则可以更改数据网格中.aspx / .ascx页面中的列定义。不使用asp:BoundColumn,而是使用asp:TemplateColumn。例如:

            <asp:DataGrid ID="dgMyData" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundColumn DataField="class_code" HeaderText="Class Code" />
                <asp:BoundColumn DataField="sub_code" HeaderText="Subject Title" />
                <asp:TemplateColumn>
                    <ItemTemplate>
                        <%#Eval("sub_code")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlSubjects" runat="server" />
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:BoundColumn DataField="Gr_Lvl" HeaderText="Grade Level" />
                <asp:BoundColumn DataField="sub_session" HeaderText="Session" />
                <asp:BoundColumn DataField="start_time" HeaderText="Start Time" DataFormatString="{0:hh:mm tt}" />
                <asp:BoundColumn DataField="end_time" HeaderText="End Time" DataFormatString="{0:hh:mm tt}" />
                <asp:BoundColumn DataField="sub_day" HeaderText="Day" />
                <asp:BoundColumn DataField="class_room" HeaderText="Room" />
                <asp:BoundColumn DataField="sub_units" HeaderText="Units" />
                <asp:BoundColumn DataField="teacher_name" HeaderText="Teacher" />
                <asp:BoundColumn DataField="sub_remarks" HeaderText="Remarks" />
                <asp:BoundColumn DataField="subloading_no" HeaderText="???" />
            </Columns>
        </asp:DataGrid>

然后你可以把代码放在后面,在适当的时候填写下拉列表。

Imports System.Collections.Generic

Public Class DataGridTest
Inherits System.Web.UI.Page

Private Class Subject
    Public Property Code As String
    Public Property Name As String
End Class

Private SubjectList As List(Of Subject)

Private Function GetSubjects() As List(Of Subject)
    'TODO - write some code to get the subjects - below is just an example
    Return New List(Of Subject) From {New Subject With {.Code = "HIST 100", .Name = "US History"},
                                      New Subject With {.Code = "MATH 212", .Name = "Algerbra II"},
                                      New Subject With {.Code = "SCI B200", .Name = "Adv. Biology"}}
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' CACHE THE SUBJECTS
    SubjectList = GetSubjects()
    ' Get the class data but don't bother making the names pretty - let the datagrid do that
    Dim qry = "Select a.class_code, a.sub_code, Gr_Lvl, ....."
    ' execute the query and bind the data to the datagrid
    Dim dgvClassDist As DataView
    dgMyData.DataSource = dgvClassDist
    dgMyData.DataBind()
End Sub

Private Sub dgMyData_ItemDataBound(sender As Object, e As DataGridItemEventArgs) Handles dgMyData.ItemDataBound
    If e.Item.ItemType = ListItemType.EditItem Then
        Dim ddl As DropDownList = e.Item.FindControl("ddlSubjects")
        ddl.Items.Clear()
        ddl.DataSource = SubjectList
        ddl.DataValueField = "Code"
        ddl.DataTextField = "Name"
        ddl.DataBind()
        Dim currentClassCode = e.Item.DataItem("class_code").ToString
        Dim item As ListItem = ddl.Items.FindByValue(currentClassCode)
        If item IsNot Nothing Then
            item.Selected = True
        End If
    End If
End Sub

结束班

请注意以下几点:

  1. 不要在查询中重命名列 - 保留HeaderText datagrid的属性为你做这件事。
  2. 不要像日期一样格式化查询字段 - 让datagrid也使用DataFormatString属性为您执行此操作。
  3. 我并不是100%确定您希望拥有这些下拉列表以及您希望用户如何更改它们以便我将它们放入编辑模板中 - 您需要有一个编辑按钮在datagrid中,让网格切换到编辑模式并编写更多代码来处理编辑。如果您总是想要一个下拉列表,那么您可以将DDL放在ItemTemplate中,并在ItemType为Item或Alternating项时将其填入ItemDataBound事件中。