ASP.net 自定义 GridView 控件
Posted
技术标签:
【中文标题】ASP.net 自定义 GridView 控件【英文标题】:ASP.net Custom GridView Control 【发布时间】:2012-06-07 02:11:02 【问题描述】:我正在开发自定义 GridView 控件,需要一些帮助。如果我手动输入列名,我可以让它正确地吐出我需要的东西。现在我有2个问题。第一个是让列标题动态显示。第二个是动态填充行项目。
默认.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>
默认.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】:我想我明白这里发生了什么,以及如何解决它。
对我来说最大的困惑是这个块:
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,您将遍历所有该表的行,然后对于每一行,您将遍历数据表中的所有列,并为每一列获取该行中的数据,然后显示它。
【讨论】:
以上是关于ASP.net 自定义 GridView 控件的主要内容,如果未能解决你的问题,请参考以下文章
asp.net中在Gridview中自定义一列操作,应该怎么做?
ASP.NET:如何为 GridView 创建自定义分页器?
ASP.Net GridView 和 TemplateField 可见性 - false 但 ItemTemplate 仍在执行