Dynamically create datagrid columns

T

Terry Holland

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 #########################
 
E

Elton Wang

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
 
S

Steven Cheng[MSFT]

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/default.asp?url=/library/en-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" <[email protected]>
| References: <[email protected]>
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: 204.101.136.100
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5639
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| 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
|
|
| | >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
| > #########################
| >
| >
|
|
|
 
T

Terry Holland

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
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host240.multiserv.com 194.200.135.240
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5659
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| 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
|
|
|
 
T

Terry Holland

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
 
T

Terry Holland

Steven

Im trying to use the custom column method explained in
http://msdn.microsoft.com/library/d...y/en-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_objStockCycleDisplay_ROC)
'modEnums.enuStatus.NewStockFromOracle_AwaitingPMApproval))

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
 
T

Tina

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
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: user-vcauojn.dsl.mindspring.com 216.175.98.119
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5676
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| 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
|
----------------------------------------------------------------------------
------------------
|
|
| | > 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/default.asp?url=/library/en-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" <[email protected]>
| > | References: <[email protected]>
| > | 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: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | NNTP-Posting-Host: 204.101.136.100
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.datagridcontrol:5639
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > |
| > | 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
| > |
| > |
| > | | > | >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
| > | > #########################
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host15.multiserv.com 194.200.135.15
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5666
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Steven
|
| Im trying to use the custom column method explained in
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-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_objStockCycleDisplay_ROC)
| 'modEnums.enuStatus.NewStockFromOracle_AwaitingPMApproval))
|
| 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
|
|
|
 
T

Terry Holland

Steven

I appreciate that you will come up with an example but I would like to
update you on something that has left me very confused.
Ive tried various tests to try to understand what the probelm is

Test 1
=====
My collection object is clsStockCycleDisplay_ROC. This inherits from
CollectionBase. This custom collection contains a collection of objects of
type clsStockCycleDisplay_INFO. If I do not use my custom columns, but use
bound columns and simply set the datadource = clsStockCycleDisplay_ROC, all
of my data displays. This tells me that my collection class and the class
that makes up the objects in the collection fullfill requirements to
databind to a datagrid.

Test 2
=====
Same as Test 1 except that I try to use my custom columns instead of
in-built bound columns. I get the error "No default member found for type
'clsStockCycleDisplay_INFO'". It is objects of this type that make up each
row of my datagrid. I am trying to display field ID, Name, CostPrice,
SalePrice from this class. I don't understand the error message in this
context.

As a test to try to get rid of the error I added the following default
property into my 'clsStockCycleDisplay_INFO'
Default Public ReadOnly Property Test(ByVal strID As String) As
String
Get
Return "TEST"
End Get
End Property

When I run the application again I did not get the error but I got a grid
with every cell showing "TEST". It therefore seems that I need a default
property in my 'Row' class and it is this property that will be displayed.
This is of no use to me and Im sure that I must be doing something wrong.
Hopefully you will be able to identify what it is that I am doing wrong.

tia

Terry Holland
 
S

Steven Cheng[MSFT]

Hi Terry,

Thanks for your further help. I've just managed to build a simple demo ,
one test page and two simple Custom DataGridColumns, One Textbox Column
and another CheckBoxColumn. I've attached the demo in this message:

And I missed that you're using VB.NET, (I usually use C# :)) , so if you
feel necessary I can build a VB.NET version later:

Also as for the problem in your new message, I'll take some further time to
have a look.

thanks for your understanding.

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" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: Dynamically create datagrid columns
| Date: Mon, 3 Oct 2005 11:47:21 +0100
| Lines: 45
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host240.multiserv.com 194.200.135.240
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5680
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Steven
|
| I appreciate that you will come up with an example but I would like to
| update you on something that has left me very confused.
| Ive tried various tests to try to understand what the probelm is
|
| Test 1
| =====
| My collection object is clsStockCycleDisplay_ROC. This inherits from
| CollectionBase. This custom collection contains a collection of objects
of
| type clsStockCycleDisplay_INFO. If I do not use my custom columns, but
use
| bound columns and simply set the datadource = clsStockCycleDisplay_ROC,
all
| of my data displays. This tells me that my collection class and the
class
| that makes up the objects in the collection fullfill requirements to
| databind to a datagrid.
|
| Test 2
| =====
| Same as Test 1 except that I try to use my custom columns instead of
| in-built bound columns. I get the error "No default member found for
type
| 'clsStockCycleDisplay_INFO'". It is objects of this type that make up
each
| row of my datagrid. I am trying to display field ID, Name, CostPrice,
| SalePrice from this class. I don't understand the error message in this
| context.
|
| As a test to try to get rid of the error I added the following default
| property into my 'clsStockCycleDisplay_INFO'
| Default Public ReadOnly Property Test(ByVal strID As String) As
| String
| Get
| Return "TEST"
| End Get
| End Property
|
| When I run the application again I did not get the error but I got a grid
| with every cell showing "TEST". It therefore seems that I need a default
| property in my 'Row' class and it is this property that will be
displayed.
| This is of no use to me and Im sure that I must be doing something wrong.
| Hopefully you will be able to identify what it is that I am doing wrong.
|
| tia
|
| Terry Holland
|
|
|
 
T

Terry Holland

Im not very familiar with C# code but working through your example, the
essential differences that I can see between yours and my code is

1) you create columns in Page_Init (OnInit) event
2) in Cell_DataBinding you use the following code:

PropertyDescriptor pd =
TypeDescriptor.GetProperties(item.DataItem).Find(_datafield, true);

if(pd == null)
{
throw new HttpException("Field_Not_Found: "+ this._datafield);
}

object obj1 = pd.GetValue(item.DataItem);

txt.Text = (string)obj1;

Where I use:
Dim cell As TableCell = CType(sender, TableCell)
Dim DGI As DataGridItem = CType(cell.NamingContainer, DataGridItem)
Dim ctl As Label = CType(cell.Controls(0), Label)

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.Message.ToString)
End Try

Which is based on code found at weblink that I posted in previous post.

I moved my code to build columns to Page_Init but the problem persists. Im
not sure what to do with PropertyDescriptor.
Do you have any other suggestions?


This problem is now becoming urgent. I appreciate that you are giving me a
lot of assistance but I need to find a solution very quickly now. Is this
something that I could resolve by using one of my support calls?
 
T

Tina

I'm having a problem, on this forum, trying to explain my problem....

I am not having any trouble whatsoever dynamically creating textboxes in a
grid. Whether or not I bind just in !Postback or bind every postback my
dynamically created grid looks marvelous.

My problem is that my dynamically created textboxes, that contain the users
entered data, don't get returned. There is apparently no ViewState for
them.

What possible good is there in creating textbox objects in code if you can't
get them back and process them?????

Am I making sense here?

thanks for your attention,
T
 
S

Steven Cheng[MSFT]

Hi Terry,

From the code you provided, I didn't find anything particular incorrect or
different from mine. What's the DataField you used for the custom columns?
Anyway, I've regenerated a VBNET version of the demo code, you can have a
reference before you thing it necessary to open a regular case on this.

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" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Dynamically create datagrid columns
| Date: Mon, 3 Oct 2005 17:16:25 +0100
| Lines: 44
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host84.multiserv.com 194.200.135.84
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5685
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Im not very familiar with C# code but working through your example, the
| essential differences that I can see between yours and my code is
|
| 1) you create columns in Page_Init (OnInit) event
| 2) in Cell_DataBinding you use the following code:
|
| PropertyDescriptor pd =
| TypeDescriptor.GetProperties(item.DataItem).Find(_datafield, true);
|
| if(pd == null)
| {
| throw new HttpException("Field_Not_Found: "+ this._datafield);
| }
|
| object obj1 = pd.GetValue(item.DataItem);
|
| txt.Text = (string)obj1;
|
| Where I use:
| Dim cell As TableCell = CType(sender, TableCell)
| Dim DGI As DataGridItem = CType(cell.NamingContainer,
DataGridItem)
| Dim ctl As Label = CType(cell.Controls(0), Label)
|
| 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.Message.ToString)
| End Try
|
| Which is based on code found at weblink that I posted in previous post.
|
| I moved my code to build columns to Page_Init but the problem persists.
Im
| not sure what to do with PropertyDescriptor.
| Do you have any other suggestions?
|
|
| This problem is now becoming urgent. I appreciate that you are giving me
a
| lot of assistance but I need to find a solution very quickly now. Is this
| something that I could resolve by using one of my support calls?
|
|
|
|
 
S

Steven Cheng[MSFT]

Hi Terry,

Thanks for your code sample. I think I've got the answer for this problem.
In fact, I should have noticed this earlier, the problem is still in the
Custom DataGrid Column. In the Cell_DataBinding function, we use the
following VB.NET code:
================
Private Sub Cell_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim cell As TableCell = sender
Dim item As DataGridItem = cell.NamingContainer
Dim txt As TextBox = item.FindControl(m_strID)

'Try
Dim obj1 As Object = item.DataItem(m_strDataField)


txt.Text = CType(obj1, String)
'Catch ex As Exception
' Throw New Exception("Invalid DataField for SCTextBoxColumnNot
")
'End Try
End Sub
====================

Notice this line :

Dim obj1 As Object = item.DataItem(m_strDataField)

since VB.NET bydefault support late binding, so we can use DataItem( ..)
without casting it to the DataRowView first. And this only works when the
DataSouce is DataSet/DataTable since it will generate DataView for the
final datasource. However, when you use custom collection class, we need
to access the property explicitly like

obj.PropertyName rather than obj("propertyname")

that's why our original code failed. In fact, my C# example, did use the
PropertyDescriptor to dynamically query the property value, so we just need
to change the above VBNET code to the below one:


Dim pd As PropertyDescriptor =
TypeDescriptor.GetProperties(item.DataItem).Find(m_strDataField, True)

Dim obj1 As Object = pd.GetValue(item.DataItem)


I've tested on my side on the page which use your custom collection as
DataSource.


Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security


--------------------
| From: "Terry Holland" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Dynamically create datagrid columns
| Date: Thu, 6 Oct 2005 13:20:06 +0100
| Lines: 440
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host31.multiserv.com 194.200.135.31
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5726
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Steven
|
| Thank you for your assistance. I am still having problems so I have
opened
| a support ticket. This is the email that I have sent with an attachment
| that contains code that highlights my problem. Maybe you could solve this
| using my code.
|
| Terry Holland
|
|
| Dear Mr. Holland,
|
| Thank you for calling Microsoft?Developer Support in reference to your
| development issue. In order to help me route your case to a suitably
| qualified engineer, could you please provide the following information:
|
| 1. Development Product and service pack(s) being used.
|
| Visual Studio.Net, ASP.Net, VB.Net
|
| 2. Operating System(s) and service pack(s) on which the problem
| occurs (client, server?).
|
| XP pro sp2
|
| 3. .Net framework version and service pack(s)(if relevant).
|
| not sure
|
| 4. Database version(s) and service pack(s)and technology used(if
| relevant).
|
| na
|
| 5. A detailed description of the problem.
|
| I have been getting assistance from the following on this matter:
|
| Support Person: Steven Cheng[MSFT] <[email protected]>
| Newsgroup: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| Post Title: Dynamically create datagrid columns
|
| Basically I am trying to create a web user control that is basically a
| datagrid. The datagrid will contain columns that are a collection of
| dynamically created custom columns. The data will come from a custom
| collection.
| In my attempts to get this to work I have tried a number of things and had
| some useful help from Setven Cheng but I still get the error
| "System.MissingMemberException: No Default member found for type Test"
when
| I try to DataBind.
|
| In the attached zip I have provided some test code to highlight the
problem.
| I have not used a web user control in this test because the error occurs
| whether I use web user control or not. It seems to be a data binding
| problem
|
| Explanation of code
|
| DataSources.vb
| This contains my custom collection and the class that this collection is a
| collection of
|
| clsDGCTB.vb
| This contains my test custom column. Code is pretty much as provided by
| Steven Cheng.
|
| TestCustomCollection.aspx & code behind page
| This page attempts to add 2 of my custom columns and then bind to my
custom
| collection.
| This produces the error when I attempt to to databind
|
| TestBoundColumns.aspx and code behind page
| This page adds 2 BoundColumns to datagrid and then binds to my custom
| collection.
| This works without error.
|
| TestDataTable.aspx & code behind page
| This page adds 2 custom columns to datagrid and then binds to a DataTable.
| This works without error.
|
| So from the above we can see that my Custom Columns (clsDGCTB) work if I
| bind to a DataTable but not to my CustomCollection
| and my CustomCollection works if I dynamically add BoundColumns to
datagrid
|
| WHat I need is for my CustomCollection to be able to bind to a grid made
up
| of my custom columns.
| 6. Any error messages that you may be receiving (if relevant).
|
| System.MissingMemberException: No Default member found for type Test
|
| 7. Any source code that reproduces the problem (if relevant).
|
| See Zip
|
| 8. Details of workarounds attempted and there results (if
| relevant).
|
| 9. Details of any online Microsoft Knowledge Base articles used
| (if relevant).
|
|
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/creatingcustomcolumns.asp
|
| 10. Details of any Virus Checkers Installed.
|
| McAfee VirusScan Enterprise 8.0
|
| 11. Given the issue discussed above what would you consider a
| satisfactory resolution to this case?
|
| To be able to bind my custom collection to my datagrid that is made up
of
| a number of custom columns
|
|
|
|
| | > Oh, what's a silly mistake, I attached the wrong files. Here is the
VB.NET
| > files.
| > Sorry for the inconvenience.
| >
| > 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" <[email protected]>
| > | References: <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > | Subject: Re: Dynamically create datagrid columns
| > | Date: Tue, 4 Oct 2005 22:49:13 +0100
| > | Lines: 5
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | NNTP-Posting-Host: 82.152.27.185
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.datagridcontrol:5707
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > |
| > | Steven
| > |
| > | The attachment is still C#
| > |
| > |
| > |
|
|
|
 
T

Terry Holland

Eureka! This code works!

Steven, thanks for your efforts. I think the lesson to be learnt from this
is "I should post my code so that you can see what Im trying"

Thanks again (looks like I wasted a support call :-( )

Steven Cheng said:
Hi Terry,

Thanks for your code sample. I think I've got the answer for this problem.
In fact, I should have noticed this earlier, the problem is still in the
Custom DataGrid Column. In the Cell_DataBinding function, we use the
following VB.NET code:
================
Private Sub Cell_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim cell As TableCell = sender
Dim item As DataGridItem = cell.NamingContainer
Dim txt As TextBox = item.FindControl(m_strID)

'Try
Dim obj1 As Object = item.DataItem(m_strDataField)


txt.Text = CType(obj1, String)
'Catch ex As Exception
' Throw New Exception("Invalid DataField for SCTextBoxColumnNot
")
'End Try
End Sub
====================

Notice this line :

Dim obj1 As Object = item.DataItem(m_strDataField)

since VB.NET bydefault support late binding, so we can use DataItem( ..)
without casting it to the DataRowView first. And this only works when the
DataSouce is DataSet/DataTable since it will generate DataView for the
final datasource. However, when you use custom collection class, we need
to access the property explicitly like

obj.PropertyName rather than obj("propertyname")

that's why our original code failed. In fact, my C# example, did use the
PropertyDescriptor to dynamically query the property value, so we just need
to change the above VBNET code to the below one:


Dim pd As PropertyDescriptor =
TypeDescriptor.GetProperties(item.DataItem).Find(m_strDataField, True)

Dim obj1 As Object = pd.GetValue(item.DataItem)


I've tested on my side on the page which use your custom collection as
DataSource.


Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security


--------------------
| From: "Terry Holland" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Dynamically create datagrid columns
| Date: Thu, 6 Oct 2005 13:20:06 +0100
| Lines: 440
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host31.multiserv.com 194.200.135.31
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5726
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Steven
|
| Thank you for your assistance. I am still having problems so I have
opened
| a support ticket. This is the email that I have sent with an attachment
| that contains code that highlights my problem. Maybe you could solve this
| using my code.
|
| Terry Holland
|
|
| Dear Mr. Holland,
|
| Thank you for calling Microsoft?Developer Support in reference to your
| development issue. In order to help me route your case to a suitably
| qualified engineer, could you please provide the following information:
|
| 1. Development Product and service pack(s) being used.
|
| Visual Studio.Net, ASP.Net, VB.Net
|
| 2. Operating System(s) and service pack(s) on which the problem
| occurs (client, server?).
|
| XP pro sp2
|
| 3. .Net framework version and service pack(s)(if relevant).
|
| not sure
|
| 4. Database version(s) and service pack(s)and technology used(if
| relevant).
|
| na
|
| 5. A detailed description of the problem.
|
| I have been getting assistance from the following on this matter:
|
| Support Person: Steven Cheng[MSFT] <[email protected]>
| Newsgroup: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| Post Title: Dynamically create datagrid columns
|
| Basically I am trying to create a web user control that is basically a
| datagrid. The datagrid will contain columns that are a collection of
| dynamically created custom columns. The data will come from a custom
| collection.
| In my attempts to get this to work I have tried a number of things and had
| some useful help from Setven Cheng but I still get the error
| "System.MissingMemberException: No Default member found for type Test"
when
| I try to DataBind.
|
| In the attached zip I have provided some test code to highlight the
problem.
| I have not used a web user control in this test because the error occurs
| whether I use web user control or not. It seems to be a data binding
| problem
|
| Explanation of code
|
| DataSources.vb
| This contains my custom collection and the class that this collection is a
| collection of
|
| clsDGCTB.vb
| This contains my test custom column. Code is pretty much as provided by
| Steven Cheng.
|
| TestCustomCollection.aspx & code behind page
| This page attempts to add 2 of my custom columns and then bind to my
custom
| collection.
| This produces the error when I attempt to to databind
|
| TestBoundColumns.aspx and code behind page
| This page adds 2 BoundColumns to datagrid and then binds to my custom
| collection.
| This works without error.
|
| TestDataTable.aspx & code behind page
| This page adds 2 custom columns to datagrid and then binds to a DataTable.
| This works without error.
|
| So from the above we can see that my Custom Columns (clsDGCTB) work if I
| bind to a DataTable but not to my CustomCollection
| and my CustomCollection works if I dynamically add BoundColumns to
datagrid
|
| WHat I need is for my CustomCollection to be able to bind to a grid made
up
| of my custom columns.
| 6. Any error messages that you may be receiving (if relevant).
|
| System.MissingMemberException: No Default member found for type Test
|
| 7. Any source code that reproduces the problem (if relevant).
|
| See Zip
|
| 8. Details of workarounds attempted and there results (if
| relevant).
|
| 9. Details of any online Microsoft Knowledge Base articles used
| (if relevant).
|
|
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/creatingcustomcolumns.asp
|
| 10. Details of any Virus Checkers Installed.
|
| McAfee VirusScan Enterprise 8.0
|
| 11. Given the issue discussed above what would you consider a
| satisfactory resolution to this case?
|
| To be able to bind my custom collection to my datagrid that is made up
of
| a number of custom columns
|
|
|
|
| | > Oh, what's a silly mistake, I attached the wrong files. Here is the
VB.NET
| > files.
| > Sorry for the inconvenience.
| >
| > 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" <[email protected]>
| > | References: <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > | Subject: Re: Dynamically create datagrid columns
| > | Date: Tue, 4 Oct 2005 22:49:13 +0100
| > | Lines: 5
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | NNTP-Posting-Host: 82.152.27.185
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.datagridcontrol:5707
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > |
| > | Steven
| > |
| > | The attachment is still C#
| > |
| > |
| > |
|
|
|
 
S

Steven Cheng[MSFT]

You're welcome Terry,

Also, as for the support call, don't worry, just tell them that you've
managed to resolve the problem yourself and ask our support guy to close it
as non-drecrement so as not to charge your incident number.

Anyway, it's my pleasure working with you. Good luck!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
--------------------
| From: "Terry Holland" <[email protected]>
| References: <[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Dynamically create datagrid columns
| Date: Fri, 7 Oct 2005 10:30:06 +0100
| Lines: 291
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host31.multiserv.com 194.200.135.31
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5738
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Eureka! This code works!
|
| Steven, thanks for your efforts. I think the lesson to be learnt from
this
| is "I should post my code so that you can see what Im trying"
|
| Thanks again (looks like I wasted a support call :-( )
|
| | > Hi Terry,
| >
| > Thanks for your code sample. I think I've got the answer for this
problem.
| > In fact, I should have noticed this earlier, the problem is still in the
| > Custom DataGrid Column. In the Cell_DataBinding function, we use the
| > following VB.NET code:
| > ================
| > Private Sub Cell_DataBinding(ByVal sender As Object, ByVal e As
| EventArgs)
| > Dim cell As TableCell = sender
| > Dim item As DataGridItem = cell.NamingContainer
| > Dim txt As TextBox = item.FindControl(m_strID)
| >
| > 'Try
| > Dim obj1 As Object = item.DataItem(m_strDataField)
| >
| >
| > txt.Text = CType(obj1, String)
| > 'Catch ex As Exception
| > ' Throw New Exception("Invalid DataField for
SCTextBoxColumnNot
| > ")
| > 'End Try
| > End Sub
| > ====================
| >
| > Notice this line :
| >
| > Dim obj1 As Object = item.DataItem(m_strDataField)
| >
| > since VB.NET bydefault support late binding, so we can use DataItem( ..)
| > without casting it to the DataRowView first. And this only works when
the
| > DataSouce is DataSet/DataTable since it will generate DataView for the
| > final datasource. However, when you use custom collection class, we
need
| > to access the property explicitly like
| >
| > obj.PropertyName rather than obj("propertyname")
| >
| > that's why our original code failed. In fact, my C# example, did use
the
| > PropertyDescriptor to dynamically query the property value, so we just
| need
| > to change the above VBNET code to the below one:
| >
| >
| > Dim pd As PropertyDescriptor =
| > TypeDescriptor.GetProperties(item.DataItem).Find(m_strDataField, True)
| >
| > Dim obj1 As Object = pd.GetValue(item.DataItem)
| >
| >
| > I've tested on my side on the page which use your custom collection as
| > DataSource.
| >
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| >
| >
| > --------------------
| > | From: "Terry Holland" <[email protected]>
| > | References: <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > | Subject: Re: Dynamically create datagrid columns
| > | Date: Thu, 6 Oct 2005 13:20:06 +0100
| > | Lines: 440
| > | 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: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | NNTP-Posting-Host: host31.multiserv.com 194.200.135.31
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.datagridcontrol:5726
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > |
| > | Steven
| > |
| > | Thank you for your assistance. I am still having problems so I have
| > opened
| > | a support ticket. This is the email that I have sent with an
attachment
| > | that contains code that highlights my problem. Maybe you could solve
| this
| > | using my code.
| > |
| > | Terry Holland
| > |
| > |
| > | Dear Mr. Holland,
| > |
| > | Thank you for calling Microsoft?Developer Support in reference to your
| > | development issue. In order to help me route your case to a suitably
| > | qualified engineer, could you please provide the following
information:
| > |
| > | 1. Development Product and service pack(s) being used.
| > |
| > | Visual Studio.Net, ASP.Net, VB.Net
| > |
| > | 2. Operating System(s) and service pack(s) on which the
problem
| > | occurs (client, server?).
| > |
| > | XP pro sp2
| > |
| > | 3. .Net framework version and service pack(s)(if relevant).
| > |
| > | not sure
| > |
| > | 4. Database version(s) and service pack(s)and technology
used(if
| > | relevant).
| > |
| > | na
| > |
| > | 5. A detailed description of the problem.
| > |
| > | I have been getting assistance from the following on this matter:
| > |
| > | Support Person: Steven Cheng[MSFT] <[email protected]>
| > | Newsgroup: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | Post Title: Dynamically create datagrid columns
| > |
| > | Basically I am trying to create a web user control that is basically a
| > | datagrid. The datagrid will contain columns that are a collection of
| > | dynamically created custom columns. The data will come from a custom
| > | collection.
| > | In my attempts to get this to work I have tried a number of things and
| had
| > | some useful help from Setven Cheng but I still get the error
| > | "System.MissingMemberException: No Default member found for type Test"
| > when
| > | I try to DataBind.
| > |
| > | In the attached zip I have provided some test code to highlight the
| > problem.
| > | I have not used a web user control in this test because the error
occurs
| > | whether I use web user control or not. It seems to be a data binding
| > | problem
| > |
| > | Explanation of code
| > |
| > | DataSources.vb
| > | This contains my custom collection and the class that this collection
is
| a
| > | collection of
| > |
| > | clsDGCTB.vb
| > | This contains my test custom column. Code is pretty much as provided
by
| > | Steven Cheng.
| > |
| > | TestCustomCollection.aspx & code behind page
| > | This page attempts to add 2 of my custom columns and then bind to my
| > custom
| > | collection.
| > | This produces the error when I attempt to to databind
| > |
| > | TestBoundColumns.aspx and code behind page
| > | This page adds 2 BoundColumns to datagrid and then binds to my custom
| > | collection.
| > | This works without error.
| > |
| > | TestDataTable.aspx & code behind page
| > | This page adds 2 custom columns to datagrid and then binds to a
| DataTable.
| > | This works without error.
| > |
| > | So from the above we can see that my Custom Columns (clsDGCTB) work
if I
| > | bind to a DataTable but not to my CustomCollection
| > | and my CustomCollection works if I dynamically add BoundColumns to
| > datagrid
| > |
| > | WHat I need is for my CustomCollection to be able to bind to a grid
made
| > up
| > | of my custom columns.
| > | 6. Any error messages that you may be receiving (if
relevant).
| > |
| > | System.MissingMemberException: No Default member found for type Test
| > |
| > | 7. Any source code that reproduces the problem (if
relevant).
| > |
| > | See Zip
| > |
| > | 8. Details of workarounds attempted and there results (if
| > | relevant).
| > |
| > | 9. Details of any online Microsoft Knowledge Base articles
| used
| > | (if relevant).
| > |
| > |
| > |
| >
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
| > /creatingcustomcolumns.asp
| > |
| > | 10. Details of any Virus Checkers Installed.
| > |
| > | McAfee VirusScan Enterprise 8.0
| > |
| > | 11. Given the issue discussed above what would you consider a
| > | satisfactory resolution to this case?
| > |
| > | To be able to bind my custom collection to my datagrid that is made
up
| > of
| > | a number of custom columns
| > |
| > |
| > |
| > |
| > | | > | > Oh, what's a silly mistake, I attached the wrong files. Here is the
| > VB.NET
| > | > files.
| > | > Sorry for the inconvenience.
| > | >
| > | > 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" <[email protected]>
| > | > | References: <[email protected]>
| > | > <#[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <#[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <#[email protected]>
| > | > <[email protected]>
| > | > | Subject: Re: Dynamically create datagrid columns
| > | > | Date: Tue, 4 Oct 2005 22:49:13 +0100
| > | > | Lines: 5
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
| > | > | X-RFC2646: Format=Flowed; Original
| > | > | Message-ID: <[email protected]>
| > | > | Newsgroups:
microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | > | NNTP-Posting-Host: 82.152.27.185
| > | > | Path:
| TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | > | Xref: TK2MSFTNGXA01.phx.gbl
| > | > microsoft.public.dotnet.framework.aspnet.datagridcontrol:5707
| > | > | X-Tomcat-NG:
| microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | > |
| > | > | Steven
| > | > |
| > | > | The attachment is still C#
| > | > |
| > | > |
| > | > |
| > |
| > |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top