Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   Dynamically create datagrid columns (http://www.velocityreviews.com/forums/t762324-dynamically-create-datagrid-columns.html)

Terry Holland 09-26-2005 01:53 PM

Dynamically create datagrid columns
 
I am trying to write a user control that is essentially a DataGrid with some
custom functionality. My grid is to be bound to a custom collection.
I have created an interface called IListData. Part of this interface is a
collection of DataGridColumn objects. I have code in my control to add a
bound column for each column I have in my collection of columns shown below.
What I would like to do is decide which type of column to use for each of my
bound columns - ie I might want to have a checkbox in one of my columns - or
I might need a dropdown list in another columns. Im having difficulty with
showing bound columns as anything other than text ie checkbox, dropdown box
etc


'################ 'Add columns to grid #########################
For Each objDataGridColumn As clsDataGridColumn In
ListData.DataGridColumns
objBoundColumn = New BoundColumn
'intWidth = objDataGridColumn.Width
With objBoundColumn
.DataField = objDataGridColumn.DataField
.HeaderText = objDataGridColumn.Caption
.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
.ItemStyle.HorizontalAlign = objDataGridColumn.Align
If objDataGridColumn.Width = 0 Then
.Visible = False
Else
.HeaderStyle.Width =
Unit.Percentage(objDataGridColumn.Width)
End If
End With
DataGrid1.Columns.Add(objBoundColumn)
Next

'################ End #########################

'################ clsDataGridColumn #########################
Public Enum enuColumnType
Label
Textbox
Dropdown
Checkbox
End Enum
Public Enum enuAlign
Left
Right
Centre
End Enum

<Serializable()> _
Public Class clsDataGridColumn
Private m_strDataField As String
Private m_strCaption As String
Private m_ColumnType As enuColumnType
Private m_Align As HorizontalAlign
Private m_intWidth As Integer

Private Sub New()

End Sub

Private Sub New(ByVal strDataField As String, ByVal strCaption As
String, ByVal ColumnType As enuColumnType, ByVal Align As enuAlign, ByVal
intWidth As Integer)
m_strDataField = strDataField
m_strCaption = strCaption
m_ColumnType = ColumnType
m_Align = GetHorizontalAlign(Align)
m_intWidth = intWidth
End Sub
Public Shared Function NewObject(ByVal strDataField As String, ByVal
strCaption As String, ByVal ColumnType As enuColumnType, ByVal Align As
enuAlign, ByVal intWidth As Integer) As clsDataGridColumn
Return New clsDataGridColumn(strDataField, strCaption, ColumnType,
Align, intWidth)
End Function
Private Function GetHorizontalAlign(ByVal Align As enuAlign) As
HorizontalAlign
Select Case Align
Case enuAlign.Left
Return HorizontalAlign.Left
Case enuAlign.Centre
Return HorizontalAlign.Center
Case enuAlign.Right
Return HorizontalAlign.Right
Case Else
Return HorizontalAlign.NotSet
End Select

End Function
Public ReadOnly Property DataField() As String
Get
Return m_strDataField
End Get
End Property
Public ReadOnly Property ColumnType() As enuColumnType
Get
Return m_ColumnType
End Get
End Property
Public ReadOnly Property Width() As Integer
Get
Return m_intWidth
End Get
End Property
Public ReadOnly Property Caption() As String
Get
Return m_strCaption
End Get
End Property
Public ReadOnly Property Align() As HorizontalAlign
Get
Return m_Align
End Get
End Property
'################ End #########################



Elton Wang 09-26-2005 04:44 PM

Re: Dynamically create datagrid columns
 
Hi Terry,

Bound column is not for containing some other controls, such as checkbox,
dropdownlist, and so on. You should use TemplateColumn for your purpose.


HTH


"Terry Holland" <terryeholland@newsgroup.nospam> wrote in message
news:u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl...
>I am trying to write a user control that is essentially a DataGrid with
>some
> custom functionality. My grid is to be bound to a custom collection.
> I have created an interface called IListData. Part of this interface is a
> collection of DataGridColumn objects. I have code in my control to add a
> bound column for each column I have in my collection of columns shown
> below.
> What I would like to do is decide which type of column to use for each of
> my
> bound columns - ie I might want to have a checkbox in one of my columns -
> or
> I might need a dropdown list in another columns. Im having difficulty
> with
> showing bound columns as anything other than text ie checkbox, dropdown
> box
> etc
>
>
> '################ 'Add columns to grid #########################
> For Each objDataGridColumn As clsDataGridColumn In
> ListData.DataGridColumns
> objBoundColumn = New BoundColumn
> 'intWidth = objDataGridColumn.Width
> With objBoundColumn
> .DataField = objDataGridColumn.DataField
> .HeaderText = objDataGridColumn.Caption
> .HeaderStyle.HorizontalAlign = HorizontalAlign.Center
> .ItemStyle.HorizontalAlign = objDataGridColumn.Align
> If objDataGridColumn.Width = 0 Then
> .Visible = False
> Else
> .HeaderStyle.Width =
> Unit.Percentage(objDataGridColumn.Width)
> End If
> End With
> DataGrid1.Columns.Add(objBoundColumn)
> Next
>
> '################ End
> #########################
>
> '################ clsDataGridColumn #########################
> Public Enum enuColumnType
> Label
> Textbox
> Dropdown
> Checkbox
> End Enum
> Public Enum enuAlign
> Left
> Right
> Centre
> End Enum
>
> <Serializable()> _
> Public Class clsDataGridColumn
> Private m_strDataField As String
> Private m_strCaption As String
> Private m_ColumnType As enuColumnType
> Private m_Align As HorizontalAlign
> Private m_intWidth As Integer
>
> Private Sub New()
>
> End Sub
>
> Private Sub New(ByVal strDataField As String, ByVal strCaption As
> String, ByVal ColumnType As enuColumnType, ByVal Align As enuAlign, ByVal
> intWidth As Integer)
> m_strDataField = strDataField
> m_strCaption = strCaption
> m_ColumnType = ColumnType
> m_Align = GetHorizontalAlign(Align)
> m_intWidth = intWidth
> End Sub
> Public Shared Function NewObject(ByVal strDataField As String, ByVal
> strCaption As String, ByVal ColumnType As enuColumnType, ByVal Align As
> enuAlign, ByVal intWidth As Integer) As clsDataGridColumn
> Return New clsDataGridColumn(strDataField, strCaption, ColumnType,
> Align, intWidth)
> End Function
> Private Function GetHorizontalAlign(ByVal Align As enuAlign) As
> HorizontalAlign
> Select Case Align
> Case enuAlign.Left
> Return HorizontalAlign.Left
> Case enuAlign.Centre
> Return HorizontalAlign.Center
> Case enuAlign.Right
> Return HorizontalAlign.Right
> Case Else
> Return HorizontalAlign.NotSet
> End Select
>
> End Function
> Public ReadOnly Property DataField() As String
> Get
> Return m_strDataField
> End Get
> End Property
> Public ReadOnly Property ColumnType() As enuColumnType
> Get
> Return m_ColumnType
> End Get
> End Property
> Public ReadOnly Property Width() As Integer
> Get
> Return m_intWidth
> End Get
> End Property
> Public ReadOnly Property Caption() As String
> Get
> Return m_strCaption
> End Get
> End Property
> Public ReadOnly Property Align() As HorizontalAlign
> Get
> Return m_Align
> End Get
> End Property
> '################ End
> #########################
>
>




Steven Cheng[MSFT] 09-27-2005 01:45 AM

Re: Dynamically create datagrid columns
 
Hi Terry,

As Elton has mentioned, the current ASP.NET DataGrid Control only contains
limited buildin DataGridColumns. And as for the BoundColumn, it only
support displaying data with Label and edit through TextBox. If we want to
provide other controls for the column, currently the most common means is
to use TemplateColumn which can be customzed with our own html template.
However, for your scenario, you're adding the Columns dynamically, so I
think Templatecolumn is not a possible approach, I think you may need to
create your own custom DAtaGridColumn types for your particular datas. Here
is a good msdn tech article discussing on creating Custom
columns for asp.net datagrid:

#Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/de...us/dnaspp/html
/creatingcustomcolumns.asp

BTW, when we creating datagrid columns dynamically, be sure to create those
columns in each request of the page(not only the first request ) since
columns collection info are not stored in viewstate. Also, we need to
create them earlier in the page's severside processing, the Page's Init and
Load event is the proper place.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)








--------------------
| From: "Elton Wang" <elton_wang@hotmail.com>
| References: <u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl>
| Subject: Re: Dynamically create datagrid columns
| Date: Mon, 26 Sep 2005 12:44:49 -0400
| Lines: 137
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#$sGgmrwFHA.2312@TK2MSFTNGP14.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| NNTP-Posting-Host: 204.101.136.100
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5639
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
|
| Hi Terry,
|
| Bound column is not for containing some other controls, such as checkbox,
| dropdownlist, and so on. You should use TemplateColumn for your purpose.
|
|
| HTH
|
|
| "Terry Holland" <terryeholland@newsgroup.nospam> wrote in message
| news:u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl...
| >I am trying to write a user control that is essentially a DataGrid with
| >some
| > custom functionality. My grid is to be bound to a custom collection.
| > I have created an interface called IListData. Part of this interface is
a
| > collection of DataGridColumn objects. I have code in my control to
add a
| > bound column for each column I have in my collection of columns shown
| > below.
| > What I would like to do is decide which type of column to use for each
of
| > my
| > bound columns - ie I might want to have a checkbox in one of my columns
-
| > or
| > I might need a dropdown list in another columns. Im having difficulty
| > with
| > showing bound columns as anything other than text ie checkbox, dropdown
| > box
| > etc
| >
| >
| > '################ 'Add columns to grid #########################
| > For Each objDataGridColumn As clsDataGridColumn In
| > ListData.DataGridColumns
| > objBoundColumn = New BoundColumn
| > 'intWidth = objDataGridColumn.Width
| > With objBoundColumn
| > .DataField = objDataGridColumn.DataField
| > .HeaderText = objDataGridColumn.Caption
| > .HeaderStyle.HorizontalAlign = HorizontalAlign.Center
| > .ItemStyle.HorizontalAlign = objDataGridColumn.Align
| > If objDataGridColumn.Width = 0 Then
| > .Visible = False
| > Else
| > .HeaderStyle.Width =
| > Unit.Percentage(objDataGridColumn.Width)
| > End If
| > End With
| > DataGrid1.Columns.Add(objBoundColumn)
| > Next
| >
| > '################ End
| > #########################
| >
| > '################ clsDataGridColumn #########################
| > Public Enum enuColumnType
| > Label
| > Textbox
| > Dropdown
| > Checkbox
| > End Enum
| > Public Enum enuAlign
| > Left
| > Right
| > Centre
| > End Enum
| >
| > <Serializable()> _
| > Public Class clsDataGridColumn
| > Private m_strDataField As String
| > Private m_strCaption As String
| > Private m_ColumnType As enuColumnType
| > Private m_Align As HorizontalAlign
| > Private m_intWidth As Integer
| >
| > Private Sub New()
| >
| > End Sub
| >
| > Private Sub New(ByVal strDataField As String, ByVal strCaption As
| > String, ByVal ColumnType As enuColumnType, ByVal Align As enuAlign,
ByVal
| > intWidth As Integer)
| > m_strDataField = strDataField
| > m_strCaption = strCaption
| > m_ColumnType = ColumnType
| > m_Align = GetHorizontalAlign(Align)
| > m_intWidth = intWidth
| > End Sub
| > Public Shared Function NewObject(ByVal strDataField As String, ByVal
| > strCaption As String, ByVal ColumnType As enuColumnType, ByVal Align As
| > enuAlign, ByVal intWidth As Integer) As clsDataGridColumn
| > Return New clsDataGridColumn(strDataField, strCaption,
ColumnType,
| > Align, intWidth)
| > End Function
| > Private Function GetHorizontalAlign(ByVal Align As enuAlign) As
| > HorizontalAlign
| > Select Case Align
| > Case enuAlign.Left
| > Return HorizontalAlign.Left
| > Case enuAlign.Centre
| > Return HorizontalAlign.Center
| > Case enuAlign.Right
| > Return HorizontalAlign.Right
| > Case Else
| > Return HorizontalAlign.NotSet
| > End Select
| >
| > End Function
| > Public ReadOnly Property DataField() As String
| > Get
| > Return m_strDataField
| > End Get
| > End Property
| > Public ReadOnly Property ColumnType() As enuColumnType
| > Get
| > Return m_ColumnType
| > End Get
| > End Property
| > Public ReadOnly Property Width() As Integer
| > Get
| > Return m_intWidth
| > End Get
| > End Property
| > Public ReadOnly Property Caption() As String
| > Get
| > Return m_strCaption
| > End Get
| > End Property
| > Public ReadOnly Property Align() As HorizontalAlign
| > Get
| > Return m_Align
| > End Get
| > End Property
| > '################ End
| > #########################
| >
| >
|
|
|


Terry Holland 09-29-2005 03:55 PM

Re: Dynamically create datagrid columns
 
Steven

Thanks for your reply. I think you helped me on this before. I am still
not totally at ease with what Im doing here.
I like the concept of creating custom columns and I agree that this is in
fact what I need but Im having some difficulty in implementing exactly what
I want.
The end result of what I want is to have my datagrid display the contents of
one of my custom collections with certain cells for every row editable. ie
I dont want to the user to have to edit line by line, I want them to have
for example column 0 as a non editable label, column 1 as an editable
textbox, column 2 as a checkbox. The user should be able to tick a
selection of rows and have the values that they have entered into the text
box (column 1) for each of the selected rows saved back to a database.
Could you give me some code sample specific to this type of scenario please
(vb if poss)

tia

Terry



Steven Cheng[MSFT] 09-30-2005 05:56 AM

Re: Dynamically create datagrid columns
 
Thanks for your followup Terry,

So I think I've got your idea. I're wantting to create a DataGrid, it 'll
display a certain database columns's data in a certain datagrid column, and
also diplaying in textbox (for edit/update) in another column so that you
can batch update them without edit/update single row each time, yes?

IMO, for this scenario, using template column would be the most convenient
means. Just define the TextBox in a certain template column's ItemTemplate(
also for the checkbox field), and in runtime, we loop through the
datagrid's each DataGridItem and check the checkbox and textbox column's
control's value to determine whether to do the update operation or not.
Also, of course we can do this through defining custom datagrid column, but
If you don't have many page which will reuse the it, I don't think it's
necessary.

Anway, if you feel the template column approach is ok, I can build a simple
demo page for your reference. Please feel free to let me know if you have
any further concerns.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "Terry Holland" <terryeholland@newsgroup.nospam>
| References: <u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl>
<#$sGgmrwFHA.2312@TK2MSFTNGP14.phx.gbl>
<2o1r7UwwFHA.2080@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: Dynamically create datagrid columns
| Date: Thu, 29 Sep 2005 16:55:00 +0100
| Lines: 22
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <e640n4QxFHA.3236@TK2MSFTNGP14.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| NNTP-Posting-Host: host240.multiserv.com 194.200.135.240
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5659
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
|
| Steven
|
| Thanks for your reply. I think you helped me on this before. I am still
| not totally at ease with what Im doing here.
| I like the concept of creating custom columns and I agree that this is in
| fact what I need but Im having some difficulty in implementing exactly
what
| I want.
| The end result of what I want is to have my datagrid display the contents
of
| one of my custom collections with certain cells for every row editable.
ie
| I dont want to the user to have to edit line by line, I want them to have
| for example column 0 as a non editable label, column 1 as an editable
| textbox, column 2 as a checkbox. The user should be able to tick a
| selection of rows and have the values that they have entered into the text
| box (column 1) for each of the selected rows saved back to a database.
| Could you give me some code sample specific to this type of scenario
please
| (vb if poss)
|
| tia
|
| Terry
|
|
|


Terry Holland 09-30-2005 10:11 AM

Re: Dynamically create datagrid columns
 
> So I think I've got your idea. I're wantting to create a DataGrid, it 'll
> display a certain database columns's data in a certain datagrid column,

and
> also diplaying in textbox (for edit/update) in another column so that you
> can batch update them without edit/update single row each time, yes?


Yes, this is exactly what I want to do

> IMO, for this scenario, using template column would be the most convenient
> means. Just define the TextBox in a certain template column's

ItemTemplate(
> also for the checkbox field), and in runtime, we loop through the
> datagrid's each DataGridItem and check the checkbox and textbox column's
> control's value to determine whether to do the update operation or not.
> Also, of course we can do this through defining custom datagrid column,

but
> If you don't have many page which will reuse the it, I don't think it's
> necessary.


I have many pages that will need this functionality. I have started
creating a User Control that will contain my a datagrid. This control has a
Datasource property into which I pass my custom collection. I want my
collection to contain information about what properties to display in the
grid and which type of column (ie textbox, label, checkbox, dropdown list
etc). Bearing this in mind I think it would be better to creat custom
columns. I am in the process of doing this now. You say that I will need
to loop through the datagrids collection of DataGridItem and update my
collection according to the values contained in each DataGridItem. I was
under the impression that I would not need to do this because my grid is
bound to my collection. Could you please clarify this point.

> Anway, if you feel the template column approach is ok, I can build a

simple
> demo page for your reference. Please feel free to let me know if you have
> any further concerns.


I prefer the custom column method as I will have many uses for this
functionality, and I have mad a start on this.


Thanks for time



Terry Holland 09-30-2005 04:11 PM

Re: Dynamically create datagrid columns
 
Steven

Im trying to use the custom column method explained in
http://msdn.microsoft.com/library/de...tomcolumns.asp

I have the following setup:

I have created a user control called ctlMaint_List.ascx. I have added a
datagrid to this control. I have a method called InitialiseControl which I
call from the Page_Load of the page that the control is placed on. I pass
an object that implements my IListData interface to the InitialiseControl
method. Part of the IListData interface is a DataGridColumns property which
is a collection of clsDataGridColumn objects. The clsDataGridColumn is a
class that is used to describe the properties of a field in my custom
collection that I want to display in the grid.
I have created a number of custom columns based on the code found in the
article that you suggested that I look at. Ive included code for one of the
columns (label).
When I run my page I get an error on the line
ctl.Text = DGI.DataItem(DataField)

of method
Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As
EventArgs)

in my class clsDGCLabel

The error that is displayed on my page is

Server Error in '/CSAWeb' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 40: Throw New Exception("Specified DataField was not
found.")
Line 41: Catch OtherEx As Exception
Line 42: Throw New Exception(OtherEx.InnerException.ToString)
Line 43: End Try
Line 44: End Sub


but if I put a break on the line and execute the line and print the
OtherEx.Message in the immediate window I get this error

"No default member found for type 'clsStockCycleDisplay_INFO'."

My class 'clsStockCycleDisplay_INFO' is what my collection consists of.

I dont know why this is happening. If I use my custom column classes as the
column classes in the test project at the link that you sent me, it works
ok.
I hope this is not too confusing. Ive pasted what I believe to be relevant
code below






==================================
Page_Load code
==================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put Stock code to initialize the page here

Dim Status As modEnums.enuStatus

Status = Session("Status")

m_objStockCycleDisplay_ROC =
clsStockCycleDisplay_ROC.GetStockCycleDisplay_ROC( Status)

ctlMaint_List1.InitialiseControl(m_objStockCycleDi splay_ROC)
'modEnums.enuStatus.NewStockFromOracle_AwaitingPMA pproval))

End Sub


==================================
InitialiseControl method of my user control
==================================
Public Sub InitialiseControl(ByVal ListData As IListData)

m_objROC = ListData.DataSource

'Add columns

For Each o As clsDataGridColumn In ListData.DataGridColumns

Select Case o.ColumnType

Case enuColumnType.Checkbox

'DataGrid1.Columns.Add(New clsDGCCheckBoxColumn(o))

Case enuColumnType.Dropdown

'DataGrid1.Columns.Add(New clsDGCDropDownColumn(o))

Case enuColumnType.Textbox

'DataGrid1.Columns.Add(New clsDGCTextBox(o))

Case enuColumnType.Label

AddColumn_Label(o)

'DataGrid1.Columns.Add(New clsDGCLabel(o))

Case Else

AddColumn_Label(o)

'DataGrid1.Columns.Add(New clsDGCLabel(o))

End Select

Next

'Bind data

With DataGrid1

..HeaderStyle.CssClass = "GridHeader"

..DataSource = m_objROC

..DataBind()

End With

'Add grid buttons

With ListData

If .ShowColumns("New") Then AddNewButton()

If .ShowColumns("Edit") Then AddEditButton()

If .ShowColumns("Delete") Then AddDeleteButton()

End With

End Sub

==================================
IListData Implemetation in my Custom Collection
==================================
#Region " IListData Implementation"

Public ReadOnly Property DataSource() As CollectionBase Implements
IListData.DataSource

Get

Return Me

End Get

End Property

Public ReadOnly Property ShowColumns() As clsFlag_COL Implements
IListData.ShowColumns

Get

Return m_objShowColumns

End Get

End Property

Public ReadOnly Property DataGridColumns() As
DGCCustomColumns.clsDataGridColumn_COL Implements IListData.DataGridColumns

Get

Return m_objColumns

End Get

End Property

#End Region

==================================
clsDataGridColumn
==================================
Option Strict On

Public Enum enuColumnType

Label

Textbox

Dropdown

Checkbox

End Enum

Public Enum enuAlign

Left

Right

Centre

End Enum

<Serializable()> _

Public Class clsDataGridColumn

Public DataField As String

Public Caption As String

Public ColumnType As enuColumnType

Public Align As HorizontalAlign

Public Width As Integer

Private Sub New()

End Sub

Private Sub New(ByVal strDataField As String, ByVal strCaption As String,
ByVal ColumnType As enuColumnType, ByVal Alignment As enuAlign, ByVal
intWidth As Integer)

DataField = strDataField

Caption = strCaption

ColumnType = ColumnType

Align = GetHorizontalAlign(Alignment)

intWidth = intWidth

End Sub

Public Shared Function NewObject(ByVal strDataField As String, _

ByVal strCaption As String, _

ByVal ColumnType As enuColumnType, _

ByVal Align As enuAlign, _

ByVal intWidth As Integer) As clsDataGridColumn

Return New clsDataGridColumn(strDataField, strCaption, ColumnType, Align,
intWidth)

End Function

Private Function GetHorizontalAlign(ByVal Align As enuAlign) As
HorizontalAlign

Select Case Align

Case enuAlign.Left

Return HorizontalAlign.Left

Case enuAlign.Centre

Return HorizontalAlign.Center

Case enuAlign.Right

Return HorizontalAlign.Right

Case Else

Return HorizontalAlign.NotSet

End Select

End Function



End Class

==================================
clsDGCLabel
==================================
Imports System.Web.UI.WebControls

Imports System.Web.UI

Public Class clsDGCLabel

Inherits DataGridColumn

Private m_objDGC As clsDataGridColumn

Public DataField As String

Public Sub New(ByVal objDGC As clsDataGridColumn)

m_objDGC = objDGC

With m_objDGC

HeaderText = .Caption

DataField = .DataField

Me.HeaderStyle.HorizontalAlign = .Align

End With

End Sub

Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)

MyBase.InitializeCell(cell, columnIndex, itemType)

Select Case itemType

Case ListItemType.Header

cell.Text = HeaderText

Case ListItemType.Item, ListItemType.AlternatingItem, _

ListItemType.EditItem

AddHandler cell.DataBinding, AddressOf ItemDataBinding

Dim ctl As New Label

cell.Controls.Add(ctl)

End Select

End Sub

Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As EventArgs)

Dim cell As TableCell = CType(sender, TableCell)

Dim ctl As Label = CType(cell.Controls(0), Label)

Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem)

Try

ctl.Text = DGI.DataItem(DataField)

Catch RangeEx As IndexOutOfRangeException

Throw New Exception("Specified DataField was not found.")

Catch OtherEx As Exception

Throw New Exception(OtherEx.InnerException.ToString)

End Try

End Sub

Private Sub EditItemDataBinding(ByVal sender As Object, ByVal e As
EventArgs)

Dim cell As TableCell = CType(sender, TableCell)

Dim ctl As Label = CType(cell.Controls(0), Label)

Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem)

Try

ctl.Text = DGI.DataItem(DataField)

Catch RangeEx As IndexOutOfRangeException

Throw New Exception("Specified DataField was not found.")

Catch OtherEx As Exception

Throw New Exception(OtherEx.InnerException.ToString)

End Try

End Sub





End Class



Tina 10-02-2005 11:43 PM

Re: Dynamically create datagrid columns
 
Steven,
I have a problem similar to terrys that maybe you can help me with?

I'm creating custom template columns at run time and then adding them to a
datagrid. (code example below)

The grid displays beautifully and contains all of the proper textboxes. I
need to process the changes that users have
entered in a Update_button click event. BUT, my objects are all gone in
that event!!!!!

I have dozens of working programs that process textboxes in the
itemtemplates and then process updates upon a
button hit but this is the first time I have had to create the columns at
run time.

Some people have suggested that I rebind my grid in postbacks but that would
destroy the user updates. I saw you
comment that dynamically created objects are not part of ViewState. Does
this mean that my objective is impossible?

Thanks for any help you can provide.
T

'Create the textbox in the ItemTemplate...
Dim myCol As New TemplateColumn
myCol.HeaderText = row("COLUMN_NAME")
Dim myTBT As New TextBoxTemplate(row("COLUMN_NAME"))
myCol.ItemTemplate = myTBT
dg.Columns.Add(myCol)
-----------------------------------------------------------------------------

Public Class TextBoxTemplate
Implements ITemplate
Dim ColumnName As String

Public Sub New(ByVal cname As String)
ColumnName = cname
End Sub

Public Sub InstantiateIn(ByVal container As Control) Implements
System.Web.UI.ITemplate.InstantiateIn
Dim txtTemp As TextBox = New TextBox
AddHandler txtTemp.DataBinding, AddressOf Me.BindTextBox
container.Controls.Add(txtTemp)
End Sub
Private Sub BindTextBox(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim strContents As String = ""
Dim txtTemp As TextBox = CType(sender, TextBox)
Dim container As DataGridItem = CType(txtTemp.NamingContainer,
DataGridItem)
strContents = (CType(container.DataItem,
DataRowView)).Row(ColumnName).ToString()
txtTemp.Text = strContents
txtTemp.ID = ColumnName

End Sub
----------------------------------------------------------------------------------------------


"Steven Cheng[MSFT]" <stcheng@online.microsoft.com> wrote in message
news:2o1r7UwwFHA.2080@TK2MSFTNGXA01.phx.gbl...
> Hi Terry,
>
> As Elton has mentioned, the current ASP.NET DataGrid Control only contains
> limited buildin DataGridColumns. And as for the BoundColumn, it only
> support displaying data with Label and edit through TextBox. If we want to
> provide other controls for the column, currently the most common means is
> to use TemplateColumn which can be customzed with our own html template.
> However, for your scenario, you're adding the Columns dynamically, so I
> think Templatecolumn is not a possible approach, I think you may need to
> create your own custom DAtaGridColumn types for your particular datas.
> Here
> is a good msdn tech article discussing on creating Custom
> columns for asp.net datagrid:
>
> #Creating Custom Columns for the ASP.NET Datagrid
> http://msdn.microsoft.com/library/de...us/dnaspp/html
> /creatingcustomcolumns.asp
>
> BTW, when we creating datagrid columns dynamically, be sure to create
> those
> columns in each request of the page(not only the first request ) since
> columns collection info are not stored in viewstate. Also, we need to
> create them earlier in the page's severside processing, the Page's Init
> and
> Load event is the proper place.
>
> Hope helps. Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>
>
>
>
>
>
> --------------------
> | From: "Elton Wang" <elton_wang@hotmail.com>
> | References: <u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl>
> | Subject: Re: Dynamically create datagrid columns
> | Date: Mon, 26 Sep 2005 12:44:49 -0400
> | Lines: 137
> | X-Priority: 3
> | X-MSMail-Priority: Normal
> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
> | X-RFC2646: Format=Flowed; Original
> | Message-ID: <#$sGgmrwFHA.2312@TK2MSFTNGP14.phx.gbl>
> | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
> | NNTP-Posting-Host: 204.101.136.100
> | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
> | Xref: TK2MSFTNGXA01.phx.gbl
> microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5639
> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
> |
> | Hi Terry,
> |
> | Bound column is not for containing some other controls, such as
> checkbox,
> | dropdownlist, and so on. You should use TemplateColumn for your purpose.
> |
> |
> | HTH
> |
> |
> | "Terry Holland" <terryeholland@newsgroup.nospam> wrote in message
> | news:u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl...
> | >I am trying to write a user control that is essentially a DataGrid with
> | >some
> | > custom functionality. My grid is to be bound to a custom collection.
> | > I have created an interface called IListData. Part of this interface
> is
> a
> | > collection of DataGridColumn objects. I have code in my control to
> add a
> | > bound column for each column I have in my collection of columns shown
> | > below.
> | > What I would like to do is decide which type of column to use for each
> of
> | > my
> | > bound columns - ie I might want to have a checkbox in one of my
> columns
> -
> | > or
> | > I might need a dropdown list in another columns. Im having difficulty
> | > with
> | > showing bound columns as anything other than text ie checkbox,
> dropdown
> | > box
> | > etc
> | >
> | >
> | > '################ 'Add columns to grid #########################
> | > For Each objDataGridColumn As clsDataGridColumn In
> | > ListData.DataGridColumns
> | > objBoundColumn = New BoundColumn
> | > 'intWidth = objDataGridColumn.Width
> | > With objBoundColumn
> | > .DataField = objDataGridColumn.DataField
> | > .HeaderText = objDataGridColumn.Caption
> | > .HeaderStyle.HorizontalAlign = HorizontalAlign.Center
> | > .ItemStyle.HorizontalAlign = objDataGridColumn.Align
> | > If objDataGridColumn.Width = 0 Then
> | > .Visible = False
> | > Else
> | > .HeaderStyle.Width =
> | > Unit.Percentage(objDataGridColumn.Width)
> | > End If
> | > End With
> | > DataGrid1.Columns.Add(objBoundColumn)
> | > Next
> | >
> | > '################ End
> | > #########################
> | >
> | > '################ clsDataGridColumn #########################
> | > Public Enum enuColumnType
> | > Label
> | > Textbox
> | > Dropdown
> | > Checkbox
> | > End Enum
> | > Public Enum enuAlign
> | > Left
> | > Right
> | > Centre
> | > End Enum
> | >
> | > <Serializable()> _
> | > Public Class clsDataGridColumn
> | > Private m_strDataField As String
> | > Private m_strCaption As String
> | > Private m_ColumnType As enuColumnType
> | > Private m_Align As HorizontalAlign
> | > Private m_intWidth As Integer
> | >
> | > Private Sub New()
> | >
> | > End Sub
> | >
> | > Private Sub New(ByVal strDataField As String, ByVal strCaption As
> | > String, ByVal ColumnType As enuColumnType, ByVal Align As enuAlign,
> ByVal
> | > intWidth As Integer)
> | > m_strDataField = strDataField
> | > m_strCaption = strCaption
> | > m_ColumnType = ColumnType
> | > m_Align = GetHorizontalAlign(Align)
> | > m_intWidth = intWidth
> | > End Sub
> | > Public Shared Function NewObject(ByVal strDataField As String,
> ByVal
> | > strCaption As String, ByVal ColumnType As enuColumnType, ByVal Align
> As
> | > enuAlign, ByVal intWidth As Integer) As clsDataGridColumn
> | > Return New clsDataGridColumn(strDataField, strCaption,
> ColumnType,
> | > Align, intWidth)
> | > End Function
> | > Private Function GetHorizontalAlign(ByVal Align As enuAlign) As
> | > HorizontalAlign
> | > Select Case Align
> | > Case enuAlign.Left
> | > Return HorizontalAlign.Left
> | > Case enuAlign.Centre
> | > Return HorizontalAlign.Center
> | > Case enuAlign.Right
> | > Return HorizontalAlign.Right
> | > Case Else
> | > Return HorizontalAlign.NotSet
> | > End Select
> | >
> | > End Function
> | > Public ReadOnly Property DataField() As String
> | > Get
> | > Return m_strDataField
> | > End Get
> | > End Property
> | > Public ReadOnly Property ColumnType() As enuColumnType
> | > Get
> | > Return m_ColumnType
> | > End Get
> | > End Property
> | > Public ReadOnly Property Width() As Integer
> | > Get
> | > Return m_intWidth
> | > End Get
> | > End Property
> | > Public ReadOnly Property Caption() As String
> | > Get
> | > Return m_strCaption
> | > End Get
> | > End Property
> | > Public ReadOnly Property Align() As HorizontalAlign
> | > Get
> | > Return m_Align
> | > End Get
> | > End Property
> | > '################ End
> | > #########################
> | >
> | >
> |
> |
> |
>




Steven Cheng[MSFT] 10-03-2005 01:06 AM

Re: Dynamically create datagrid columns
 
Hi Tina,

I'm curious about the code that how you created the dynamic columns for the
datagrid? Of course, creating dynamic columns is possible. Have you
created them in each page's request (not only !IsPostBack) ?, that's very
important. Anyway, if you still have problems, you can monitor this thread
later since I'll try posting followup with a simple demo page.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "Tina" <tinamseaburn@nospammeexcite.com>
| References: <u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl>
<#$sGgmrwFHA.2312@TK2MSFTNGP14.phx.gbl>
<2o1r7UwwFHA.2080@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: Dynamically create datagrid columns
| Date: Sun, 2 Oct 2005 16:43:42 -0700
| Lines: 277
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <e0imgs6xFHA.3720@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| NNTP-Posting-Host: user-vcauojn.dsl.mindspring.com 216.175.98.119
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5676
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
|
| Steven,
| I have a problem similar to terrys that maybe you can help me with?
|
| I'm creating custom template columns at run time and then adding them to
a
| datagrid. (code example below)
|
| The grid displays beautifully and contains all of the proper textboxes.
I
| need to process the changes that users have
| entered in a Update_button click event. BUT, my objects are all gone in
| that event!!!!!
|
| I have dozens of working programs that process textboxes in the
| itemtemplates and then process updates upon a
| button hit but this is the first time I have had to create the columns at
| run time.
|
| Some people have suggested that I rebind my grid in postbacks but that
would
| destroy the user updates. I saw you
| comment that dynamically created objects are not part of ViewState. Does
| this mean that my objective is impossible?
|
| Thanks for any help you can provide.
| T
|
| 'Create the textbox in the ItemTemplate...
| Dim myCol As New TemplateColumn
| myCol.HeaderText = row("COLUMN_NAME")
| Dim myTBT As New TextBoxTemplate(row("COLUMN_NAME"))
| myCol.ItemTemplate = myTBT
| dg.Columns.Add(myCol)
|
----------------------------------------------------------------------------
-
|
| Public Class TextBoxTemplate
| Implements ITemplate
| Dim ColumnName As String
|
| Public Sub New(ByVal cname As String)
| ColumnName = cname
| End Sub
|
| Public Sub InstantiateIn(ByVal container As Control) Implements
| System.Web.UI.ITemplate.InstantiateIn
| Dim txtTemp As TextBox = New TextBox
| AddHandler txtTemp.DataBinding, AddressOf Me.BindTextBox
| container.Controls.Add(txtTemp)
| End Sub
| Private Sub BindTextBox(ByVal sender As Object, ByVal e As
| System.EventArgs)
| Dim strContents As String = ""
| Dim txtTemp As TextBox = CType(sender, TextBox)
| Dim container As DataGridItem = CType(txtTemp.NamingContainer,
| DataGridItem)
| strContents = (CType(container.DataItem,
| DataRowView)).Row(ColumnName).ToString()
| txtTemp.Text = strContents
| txtTemp.ID = ColumnName
|
| End Sub
|
----------------------------------------------------------------------------
------------------
|
|
| "Steven Cheng[MSFT]" <stcheng@online.microsoft.com> wrote in message
| news:2o1r7UwwFHA.2080@TK2MSFTNGXA01.phx.gbl...
| > Hi Terry,
| >
| > As Elton has mentioned, the current ASP.NET DataGrid Control only
contains
| > limited buildin DataGridColumns. And as for the BoundColumn, it only
| > support displaying data with Label and edit through TextBox. If we want
to
| > provide other controls for the column, currently the most common means
is
| > to use TemplateColumn which can be customzed with our own html template.
| > However, for your scenario, you're adding the Columns dynamically, so I
| > think Templatecolumn is not a possible approach, I think you may need to
| > create your own custom DAtaGridColumn types for your particular datas.
| > Here
| > is a good msdn tech article discussing on creating Custom
| > columns for asp.net datagrid:
| >
| > #Creating Custom Columns for the ASP.NET Datagrid
| >
http://msdn.microsoft.com/library/de...us/dnaspp/html
| > /creatingcustomcolumns.asp
| >
| > BTW, when we creating datagrid columns dynamically, be sure to create
| > those
| > columns in each request of the page(not only the first request ) since
| > columns collection info are not stored in viewstate. Also, we need to
| > create them earlier in the page's severside processing, the Page's Init
| > and
| > Load event is the proper place.
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| >
| >
| >
| > --------------------
| > | From: "Elton Wang" <elton_wang@hotmail.com>
| > | References: <u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl>
| > | Subject: Re: Dynamically create datagrid columns
| > | Date: Mon, 26 Sep 2005 12:44:49 -0400
| > | Lines: 137
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <#$sGgmrwFHA.2312@TK2MSFTNGP14.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| > | NNTP-Posting-Host: 204.101.136.100
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5639
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| > |
| > | Hi Terry,
| > |
| > | Bound column is not for containing some other controls, such as
| > checkbox,
| > | dropdownlist, and so on. You should use TemplateColumn for your
purpose.
| > |
| > |
| > | HTH
| > |
| > |
| > | "Terry Holland" <terryeholland@newsgroup.nospam> wrote in message
| > | news:u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl...
| > | >I am trying to write a user control that is essentially a DataGrid
with
| > | >some
| > | > custom functionality. My grid is to be bound to a custom
collection.
| > | > I have created an interface called IListData. Part of this
interface
| > is
| > a
| > | > collection of DataGridColumn objects. I have code in my control to
| > add a
| > | > bound column for each column I have in my collection of columns
shown
| > | > below.
| > | > What I would like to do is decide which type of column to use for
each
| > of
| > | > my
| > | > bound columns - ie I might want to have a checkbox in one of my
| > columns
| > -
| > | > or
| > | > I might need a dropdown list in another columns. Im having
difficulty
| > | > with
| > | > showing bound columns as anything other than text ie checkbox,
| > dropdown
| > | > box
| > | > etc
| > | >
| > | >
| > | > '################ 'Add columns to grid #########################
| > | > For Each objDataGridColumn As clsDataGridColumn In
| > | > ListData.DataGridColumns
| > | > objBoundColumn = New BoundColumn
| > | > 'intWidth = objDataGridColumn.Width
| > | > With objBoundColumn
| > | > .DataField = objDataGridColumn.DataField
| > | > .HeaderText = objDataGridColumn.Caption
| > | > .HeaderStyle.HorizontalAlign = HorizontalAlign.Center
| > | > .ItemStyle.HorizontalAlign = objDataGridColumn.Align
| > | > If objDataGridColumn.Width = 0 Then
| > | > .Visible = False
| > | > Else
| > | > .HeaderStyle.Width =
| > | > Unit.Percentage(objDataGridColumn.Width)
| > | > End If
| > | > End With
| > | > DataGrid1.Columns.Add(objBoundColumn)
| > | > Next
| > | >
| > | > '################ End
| > | > #########################
| > | >
| > | > '################ clsDataGridColumn #########################
| > | > Public Enum enuColumnType
| > | > Label
| > | > Textbox
| > | > Dropdown
| > | > Checkbox
| > | > End Enum
| > | > Public Enum enuAlign
| > | > Left
| > | > Right
| > | > Centre
| > | > End Enum
| > | >
| > | > <Serializable()> _
| > | > Public Class clsDataGridColumn
| > | > Private m_strDataField As String
| > | > Private m_strCaption As String
| > | > Private m_ColumnType As enuColumnType
| > | > Private m_Align As HorizontalAlign
| > | > Private m_intWidth As Integer
| > | >
| > | > Private Sub New()
| > | >
| > | > End Sub
| > | >
| > | > Private Sub New(ByVal strDataField As String, ByVal strCaption As
| > | > String, ByVal ColumnType As enuColumnType, ByVal Align As enuAlign,
| > ByVal
| > | > intWidth As Integer)
| > | > m_strDataField = strDataField
| > | > m_strCaption = strCaption
| > | > m_ColumnType = ColumnType
| > | > m_Align = GetHorizontalAlign(Align)
| > | > m_intWidth = intWidth
| > | > End Sub
| > | > Public Shared Function NewObject(ByVal strDataField As String,
| > ByVal
| > | > strCaption As String, ByVal ColumnType As enuColumnType, ByVal
Align
| > As
| > | > enuAlign, ByVal intWidth As Integer) As clsDataGridColumn
| > | > Return New clsDataGridColumn(strDataField, strCaption,
| > ColumnType,
| > | > Align, intWidth)
| > | > End Function
| > | > Private Function GetHorizontalAlign(ByVal Align As enuAlign) As
| > | > HorizontalAlign
| > | > Select Case Align
| > | > Case enuAlign.Left
| > | > Return HorizontalAlign.Left
| > | > Case enuAlign.Centre
| > | > Return HorizontalAlign.Center
| > | > Case enuAlign.Right
| > | > Return HorizontalAlign.Right
| > | > Case Else
| > | > Return HorizontalAlign.NotSet
| > | > End Select
| > | >
| > | > End Function
| > | > Public ReadOnly Property DataField() As String
| > | > Get
| > | > Return m_strDataField
| > | > End Get
| > | > End Property
| > | > Public ReadOnly Property ColumnType() As enuColumnType
| > | > Get
| > | > Return m_ColumnType
| > | > End Get
| > | > End Property
| > | > Public ReadOnly Property Width() As Integer
| > | > Get
| > | > Return m_intWidth
| > | > End Get
| > | > End Property
| > | > Public ReadOnly Property Caption() As String
| > | > Get
| > | > Return m_strCaption
| > | > End Get
| > | > End Property
| > | > Public ReadOnly Property Align() As HorizontalAlign
| > | > Get
| > | > Return m_Align
| > | > End Get
| > | > End Property
| > | > '################ End
| > | > #########################
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|


Steven Cheng[MSFT] 10-03-2005 01:08 AM

Re: Dynamically create datagrid columns
 
Hi Terry,

Don't worry, I'll try creating a simple demo page with some simple custom
datagrid columns, I'll update you soon.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Terry Holland" <terryeholland@newsgroup.nospam>
| References: <u5WweGqwFHA.720@TK2MSFTNGP15.phx.gbl>
<#$sGgmrwFHA.2312@TK2MSFTNGP14.phx.gbl>
<2o1r7UwwFHA.2080@TK2MSFTNGXA01.phx.gbl>
<e640n4QxFHA.3236@TK2MSFTNGP14.phx.gbl>
<9wuitOYxFHA.628@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: Dynamically create datagrid columns
| Date: Fri, 30 Sep 2005 17:11:14 +0100
| Lines: 413
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
| Message-ID: <#480ZmdxFHA.2880@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| NNTP-Posting-Host: host15.multiserv.com 194.200.135.15
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:5666
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
|
| Steven
|
| Im trying to use the custom column method explained in
|
http://msdn.microsoft.com/library/de...us/dnaspp/html
/creatingcustomcolumns.asp
|
| I have the following setup:
|
| I have created a user control called ctlMaint_List.ascx. I have added a
| datagrid to this control. I have a method called InitialiseControl which
I
| call from the Page_Load of the page that the control is placed on. I
pass
| an object that implements my IListData interface to the InitialiseControl
| method. Part of the IListData interface is a DataGridColumns property
which
| is a collection of clsDataGridColumn objects. The clsDataGridColumn is a
| class that is used to describe the properties of a field in my custom
| collection that I want to display in the grid.
| I have created a number of custom columns based on the code found in the
| article that you suggested that I look at. Ive included code for one of
the
| columns (label).
| When I run my page I get an error on the line
| ctl.Text = DGI.DataItem(DataField)
|
| of method
| Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As
| EventArgs)
|
| in my class clsDGCLabel
|
| The error that is displayed on my page is
|
| Server Error in '/CSAWeb' Application.
|
----------------------------------------------------------------------------
----
|
| Object reference not set to an instance of an object.
| Description: An unhandled exception occurred during the execution of the
| current web request. Please review the stack trace for more information
| about the error and where it originated in the code.
|
| Exception Details: System.NullReferenceException: Object reference not
set
| to an instance of an object.
|
| Source Error:
|
| Line 40: Throw New Exception("Specified DataField was not
| found.")
| Line 41: Catch OtherEx As Exception
| Line 42: Throw New Exception(OtherEx.InnerException.ToString)
| Line 43: End Try
| Line 44: End Sub
|
|
| but if I put a break on the line and execute the line and print the
| OtherEx.Message in the immediate window I get this error
|
| "No default member found for type 'clsStockCycleDisplay_INFO'."
|
| My class 'clsStockCycleDisplay_INFO' is what my collection consists of.
|
| I dont know why this is happening. If I use my custom column classes as
the
| column classes in the test project at the link that you sent me, it works
| ok.
| I hope this is not too confusing. Ive pasted what I believe to be
relevant
| code below
|
|
|
|
|
|
| ==================================
| Page_Load code
| ==================================
| Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Load
|
| 'Put Stock code to initialize the page here
|
| Dim Status As modEnums.enuStatus
|
| Status = Session("Status")
|
| m_objStockCycleDisplay_ROC =
| clsStockCycleDisplay_ROC.GetStockCycleDisplay_ROC( Status)
|
| ctlMaint_List1.InitialiseControl(m_objStockCycleDi splay_ROC)
| 'modEnums.enuStatus.NewStockFromOracle_AwaitingPMA pproval))
|
| End Sub
|
|
| ==================================
| InitialiseControl method of my user control
| ==================================
| Public Sub InitialiseControl(ByVal ListData As IListData)
|
| m_objROC = ListData.DataSource
|
| 'Add columns
|
| For Each o As clsDataGridColumn In ListData.DataGridColumns
|
| Select Case o.ColumnType
|
| Case enuColumnType.Checkbox
|
| 'DataGrid1.Columns.Add(New clsDGCCheckBoxColumn(o))
|
| Case enuColumnType.Dropdown
|
| 'DataGrid1.Columns.Add(New clsDGCDropDownColumn(o))
|
| Case enuColumnType.Textbox
|
| 'DataGrid1.Columns.Add(New clsDGCTextBox(o))
|
| Case enuColumnType.Label
|
| AddColumn_Label(o)
|
| 'DataGrid1.Columns.Add(New clsDGCLabel(o))
|
| Case Else
|
| AddColumn_Label(o)
|
| 'DataGrid1.Columns.Add(New clsDGCLabel(o))
|
| End Select
|
| Next
|
| 'Bind data
|
| With DataGrid1
|
| .HeaderStyle.CssClass = "GridHeader"
|
| .DataSource = m_objROC
|
| .DataBind()
|
| End With
|
| 'Add grid buttons
|
| With ListData
|
| If .ShowColumns("New") Then AddNewButton()
|
| If .ShowColumns("Edit") Then AddEditButton()
|
| If .ShowColumns("Delete") Then AddDeleteButton()
|
| End With
|
| End Sub
|
| ==================================
| IListData Implemetation in my Custom Collection
| ==================================
| #Region " IListData Implementation"
|
| Public ReadOnly Property DataSource() As CollectionBase Implements
| IListData.DataSource
|
| Get
|
| Return Me
|
| End Get
|
| End Property
|
| Public ReadOnly Property ShowColumns() As clsFlag_COL Implements
| IListData.ShowColumns
|
| Get
|
| Return m_objShowColumns
|
| End Get
|
| End Property
|
| Public ReadOnly Property DataGridColumns() As
| DGCCustomColumns.clsDataGridColumn_COL Implements
IListData.DataGridColumns
|
| Get
|
| Return m_objColumns
|
| End Get
|
| End Property
|
| #End Region
|
| ==================================
| clsDataGridColumn
| ==================================
| Option Strict On
|
| Public Enum enuColumnType
|
| Label
|
| Textbox
|
| Dropdown
|
| Checkbox
|
| End Enum
|
| Public Enum enuAlign
|
| Left
|
| Right
|
| Centre
|
| End Enum
|
| <Serializable()> _
|
| Public Class clsDataGridColumn
|
| Public DataField As String
|
| Public Caption As String
|
| Public ColumnType As enuColumnType
|
| Public Align As HorizontalAlign
|
| Public Width As Integer
|
| Private Sub New()
|
| End Sub
|
| Private Sub New(ByVal strDataField As String, ByVal strCaption As String,
| ByVal ColumnType As enuColumnType, ByVal Alignment As enuAlign, ByVal
| intWidth As Integer)
|
| DataField = strDataField
|
| Caption = strCaption
|
| ColumnType = ColumnType
|
| Align = GetHorizontalAlign(Alignment)
|
| intWidth = intWidth
|
| End Sub
|
| Public Shared Function NewObject(ByVal strDataField As String, _
|
| ByVal strCaption As String, _
|
| ByVal ColumnType As enuColumnType, _
|
| ByVal Align As enuAlign, _
|
| ByVal intWidth As Integer) As clsDataGridColumn
|
| Return New clsDataGridColumn(strDataField, strCaption, ColumnType, Align,
| intWidth)
|
| End Function
|
| Private Function GetHorizontalAlign(ByVal Align As enuAlign) As
| HorizontalAlign
|
| Select Case Align
|
| Case enuAlign.Left
|
| Return HorizontalAlign.Left
|
| Case enuAlign.Centre
|
| Return HorizontalAlign.Center
|
| Case enuAlign.Right
|
| Return HorizontalAlign.Right
|
| Case Else
|
| Return HorizontalAlign.NotSet
|
| End Select
|
| End Function
|
|
|
| End Class
|
| ==================================
| clsDGCLabel
| ==================================
| Imports System.Web.UI.WebControls
|
| Imports System.Web.UI
|
| Public Class clsDGCLabel
|
| Inherits DataGridColumn
|
| Private m_objDGC As clsDataGridColumn
|
| Public DataField As String
|
| Public Sub New(ByVal objDGC As clsDataGridColumn)
|
| m_objDGC = objDGC
|
| With m_objDGC
|
| HeaderText = .Caption
|
| DataField = .DataField
|
| Me.HeaderStyle.HorizontalAlign = .Align
|
| End With
|
| End Sub
|
| Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
| columnIndex As Integer, ByVal itemType As ListItemType)
|
| MyBase.InitializeCell(cell, columnIndex, itemType)
|
| Select Case itemType
|
| Case ListItemType.Header
|
| cell.Text = HeaderText
|
| Case ListItemType.Item, ListItemType.AlternatingItem, _
|
| ListItemType.EditItem
|
| AddHandler cell.DataBinding, AddressOf ItemDataBinding
|
| Dim ctl As New Label
|
| cell.Controls.Add(ctl)
|
| End Select
|
| End Sub
|
| Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As EventArgs)
|
| Dim cell As TableCell = CType(sender, TableCell)
|
| Dim ctl As Label = CType(cell.Controls(0), Label)
|
| Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem)
|
| Try
|
| ctl.Text = DGI.DataItem(DataField)
|
| Catch RangeEx As IndexOutOfRangeException
|
| Throw New Exception("Specified DataField was not found.")
|
| Catch OtherEx As Exception
|
| Throw New Exception(OtherEx.InnerException.ToString)
|
| End Try
|
| End Sub
|
| Private Sub EditItemDataBinding(ByVal sender As Object, ByVal e As
| EventArgs)
|
| Dim cell As TableCell = CType(sender, TableCell)
|
| Dim ctl As Label = CType(cell.Controls(0), Label)
|
| Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem)
|
| Try
|
| ctl.Text = DGI.DataItem(DataField)
|
| Catch RangeEx As IndexOutOfRangeException
|
| Throw New Exception("Specified DataField was not found.")
|
| Catch OtherEx As Exception
|
| Throw New Exception(OtherEx.InnerException.ToString)
|
| End Try
|
| End Sub
|
|
|
|
|
| End Class
|
|
|



All times are GMT. The time now is 10:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.