ASP.net自定义GridView控件

时间:2012-06-01 20:33:50

标签: asp.net datagrid user-controls asp.net-2.0

我正在开发一个自定义GridView控件,需要一些帮助。如果手动输入列名,我可以正确地吐出我需要的东西。现在我有2个问题。第一个是动态显示列标题。第二个是动态填充行项目。

Default.aspx的

<asp:View id="vCreateNew" runat="server">
            Content Here (View 2)...
            <trac:DataGridView id="gvMain" runat="server"  GridLines="Horizontal"  AutoGenerateColumns="False" > </trac:DataGridView>
        </asp:View>

Default.aspx.vb

Partial Class processes_ProgramTrack_Default
    Inherits Citi.CSPaper.Controls.BasePage
    Protected Sub ProgramTrackNav_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles ProgramTrackNav.MenuItemClick
        Dim strSelection As String = ProgramTrackNav.SelectedValue
        Dim sqlDataSource As New SqlDataSource()
        sqlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("CSPaperWEBConnectionString").ConnectionString
        Select Case strSelection
            ......                
            Case "CreateNew"
                mlvNav.SetActiveView(vCreateNew)
                sqlDataSource.SelectCommand = "SELECT [ProgramNumber], [ProgramName], [ProgramStatus] FROM tblPrgTrackPrograms"
                Dim args As New DataSourceSelectArguments
                gvMain.DataSource = sqlDataSource.Select(args)
                gvMain.DataBind()
            ........
            End Select
    End Sub
End Class

GridViewControl.ascx

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="GridViewControl.ascx.vb"  Inherits="GridViewControl" %>
<asp:GridView ID="GridViewControl" runat="server" >
</asp:GridView>

GridViewControl.ascx.vb

Public Class GridViewControl
    Inherits System.Web.UI.UserControl
    Private _html As New StringBuilder()
    Private _dataSource As IEnumerable
    Private _CssClass As String
    Private _CellSpacing As String
    Public Property DataSource() As IEnumerable
        Get
            Return _dataSource
        End Get
        Set(ByVal value As IEnumerable)
            _dataSource = value
        End Set
    End Property
    Public Overridable Property CssClass() As String
        Get
            Return _CssClass
        End Get
        Set(ByVal value As String)
            _CssClass = value
        End Set
    End Property
    Public Overridable Property CellSpacing() As String
        Get
            Return _CellSpacing
        End Get
        Set(ByVal value As String)
            _CellSpacing = value
        End Set
    End Property
    Private Sub CreateBulletedList()
        Dim dataSource As IEnumerable = Nothing
        Dim i As Integer = 0
        Dim strArr() As String = {"ID", "Name", "Fee"}
        Try
            dataSource = Me._dataSource
        Catch
        End Try

        If Not (dataSource Is Nothing) Then
            _html.Append("<table id=""" & ClientID & """ class=""" & _CssClass & """ CellSpacing=""" & _CellSpacing & """>")
            _html.Append("<thead>" & vbCrLf & "<tr>" & vbCrLf)
            For Each Item As String In strArr
                _html.Append("<th>" & Item & "</th>" & vbCrLf)
            Next
            _html.Append("</tr>" & vbCrLf & "</thead><tbody>" & vbCrLf)
            For Each dataObject As Object In dataSource
                _html.Append("<tr>" & vbCrLf)
                For i = 0 To 2
                    _html.Append("<td>" & vbCrLf)
                    _html.Append(dataObject.Row(i))
                    _html.Append("</td>" & vbCrLf)
                Next
                _html.Append("</tr>" & vbCrLf)
            Next dataObject
            _html.Append("</tbody></table>" & vbCrLf)
        End If
    End Sub
    Public Overrides Sub DataBind()
        MyBase.OnDataBinding(EventArgs.Empty)

        CreateBulletedList()
    End Sub
    Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
        output.Write(_html)
    End Sub
End Class

------------------------------------
| Table Data                       |
------------------------------------
ID  AppName            Environment
2   TestApp            UAT
3   ServerFileMaint    UAT
4   ProgramTrack       Development
5   RegZ_Stmnt_Adj     Active
6   SecInv             Decommission

我不想手动编写上限的for i = 0 to ...,而是想动态地执行此操作。标题的方式相同。我遇到的问题是,如果我不对上限进行硬编码,则会收到一条错误消息,指出Cannot find column 3.。我在编写第二个For Each块时循环遍历包含行的数据时得到了这个。

1 个答案:

答案 0 :(得分:0)

我想我知道这里发生了什么,以及如何解决它。

对我来说,最大的困惑之处就是这个障碍:

            For Each dataObject As Object In dataSource
                _html.Append("<tr>" & vbCrLf)
                For i = 0 To 2
                    _html.Append("<td>" & vbCrLf)
                    _html.Append(dataObject.Row(i))
                    _html.Append("</td>" & vbCrLf)
                Next
                _html.Append("</tr>" & vbCrLf)
            Next dataObject

如果dataSource是一个DataSet,那么这种方法很有意义......但不是真的,因为相反,你会想要引用Table(0)。如果dataSource是一个DataTable,那么这种情况是有道理的,但不是真的,因为那样你会想要遍历For Each dataObject as DataRow in dataSource.Rows。但这似乎并非如此,因为您引用了dataObject.Row(i)。

你想要做的是(粗略/未经测试的代码)更像是这样:

            For Each dataObject As DataRow In dataSource.Rows
                _html.Append("<tr>" & vbCrLf)
                For Each col as DataColumn In dataSource.Columns
                    _html.Append("<td>" & vbCrLf)
                    _html.Append(dataObject(col).ToString())
                    _html.Append("</td>" & vbCrLf)
                Next
                _html.Append("</tr>" & vbCrLf)
            Next

这假设dataSource是一个DataTable,你将循环遍历所有表行,然后对于每一行,你将循环遍历数据表中的所有列,并为每一列获取该行中的数据显示它。