I'm working on a custom GridView control and need some help. I can get it to properly spit out what I need if I manually put in the column name. Right now I have 2 problems. The first one is getting the column headers to display dynamically. The second is filling in row items dynamicly.
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
I do not want to manually code for i = 0 to ...
, the upperbound, but want to do this dynamicly. Same way for the headings. The problem I run into is if I don't hard code the upperbound I get an error message stating Cannot find column 3.
. I get this when coding a second For Each
block to loop through the row contained data.