DataGrid

T

Terry Holland

Im am trying to dynamically add columns to a datagrid control. I have
managed to add columns to display my data and I am able to add Edit and
Delete buttons that fire the Datatagrid1_EditCommand event. What I would
like to do is instead of having these buttons as either Link or PushButton,
I would like to have an image. I still need to be able to respond to a
click event on these columns.

Could someone point me to some sample vb code for this

tia

Terry Holland
 
S

Steven Cheng[MSFT]

Hi Terry,

Welcome to ASPNET newsgroup.
From your description, you're dynamically creating some datagrid colums and
one of them is a button column, instead of the buildin "PushButton" and
"LinkButton" style, you are wondering how to build a ImageButton style one,
yes?

As for this problem, based on my experience, we have the following two
approachs:
1. Still use the Buildin column , for example, the button column and choose
the "PushButton" style. However, we need to hook the datagrid's ItemCreated
event and find the button column and adjust it's internal content , such as
remove the original button(or hidden it ) and add our own image button.

2. A more thorough means for this is defining a new Custom DataGridColumn
type (which inherits from the System.Web.UI.WebControls.DataGridColumn).
Here is a msdn article discussing on this:

#Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/creatingcustomcolumns.asp

If we just want to display a static ImageButton which will send "Edit" post
back command, we can just override the DataGrid Column's InitializeCell
method and put the following code:

Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
If ((itemType <> ListItemType.Header) And (itemType <>
ListItemType.Footer)) Then

Dim control1 As WebControl = Nothing

Dim imgbtn = New System.Web.UI.WebControls.ImageButton


imgbtn.ImageUrl =
"http://msdn.microsoft.com/library/toolbar/3.0/images/banners/msdn_masthead_
ltr.gif"

imgbtn.CommandName = "Edit"

control1 = imgbtn

cell.Controls.Add(control1)
End If
End Sub


Just some of my advices. Hope helpes.


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]>
| Subject: DataGrid
| Date: Fri, 5 Aug 2005 15:47:30 +0100
| Lines: 14
| 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.webcontrols
| 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.webcontrols:10297
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Im am trying to dynamically add columns to a datagrid control. I have
| managed to add columns to display my data and I am able to add Edit and
| Delete buttons that fire the Datatagrid1_EditCommand event. What I would
| like to do is instead of having these buttons as either Link or
PushButton,
| I would like to have an image. I still need to be able to respond to a
| click event on these columns.
|
| Could someone point me to some sample vb code for this
|
| tia
|
| Terry Holland
|
|
|
 
T

Terry Holland

Thank you Steven

Im not entirely sure what to do with code that you included in your reply.
I have tried the following

Created a new user control that inherits
System.Web.UI.WebControls.DataGridColumn and then paste in the
Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As
Integer, ByVal itemType
As ListItemType)
method that you included in your posting.

When i try to compile I get an error " Event 'Init' cannot be found."
on the event in my new ImageButonColumn control.

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

If I delete this event then app compiles but Im a bit worried Im doing the
wrong thing


Terry
 
S

Steven Cheng[MSFT]

Hi Terry,

Thanks for the response. Here is my complete testcode:

#complete custom column class
========================
Public Class ColumnClass
Inherits System.Web.UI.WebControls.DataGridColumn

Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
If ((itemType <> ListItemType.Header) And (itemType <>
ListItemType.Footer)) Then

Dim control1 As WebControl = Nothing

Dim imgbtn = New System.Web.UI.WebControls.ImageButton


imgbtn.ImageUrl =
"http://msdn.microsoft.com/library/toolbar/3.0/images/banners/msdn_masthead_
ltr.gif"

imgbtn.CommandName = "Edit"

control1 = imgbtn

cell.Controls.Add(control1)
End If
End Sub



End Class
===================

#the code in my page which contains the datagrid:

=============here i dynamically add my custom datagrid column into the
grid=========
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
InitializeComponent()

Dim cc As New ColumnClass

dgDynamic.Columns.Add(cc)


.......... other dynamic code

End Sub

====================

I there're anything else unclear or I can help ,please feel free to post
here.
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.)

--------------------
| Thread-Topic: DataGrid
| thread-index: AcWb/jdG3dDvBk8USJKBuQOij5EFWQ==
| X-WBNR-Posting-Host: 194.200.135.240
| From: "=?Utf-8?B?VGVycnkgSG9sbGFuZA==?="
<[email protected]>
| References: <#[email protected]>
<[email protected]>
| Subject: RE: DataGrid
| Date: Mon, 8 Aug 2005 02:47:41 -0700
| Lines: 28
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10328
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Thank you Steven
|
| Im not entirely sure what to do with code that you included in your
reply.
| I have tried the following
|
| Created a new user control that inherits
| System.Web.UI.WebControls.DataGridColumn and then paste in the
| Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
| columnIndex As
| Integer, ByVal
itemType
| As ListItemType)
| method that you included in your posting.
|
| When i try to compile I get an error " Event 'Init' cannot be found."
| on the event in my new ImageButonColumn control.
|
| Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Init
| 'CODEGEN: This method call is required by the Web Form Designer
| 'Do not modify it using the code editor.
| InitializeComponent()
| End Sub
|
| If I delete this event then app compiles but Im a bit worried Im doing
the
| wrong thing
|
|
| Terry
|
 
T

Terry Holland

Steven

Thank you. I mangaged to get your sample code working. However I am having
difficulty getting the code in the article
#Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/creatingcustomcolumns.asp

I am able to get the example that you can download working but am unable to
get it working in an application of my own.

I get the following error which occurrs on the line
cell.text = dgi.DataItem(DataField)

error
=============
Line 34: Throw New Exception(ex.InnerException.ToString)
Line 35: End Try
Line 36: End Sub


Source File: C:\_Development\CSA\Dev\ControlExtenders\clsDropDownColumn.vb
Line: 34

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]

=============

Any ideas why this is happening?

Also, is it possible for me to add a customcolumn completely
programmatically (ie without the need to add a @register directive and a
<ddg:DropDownColumn .../> tag on my page?

thanks in advance
 
S

Steven Cheng[MSFT]

Hi Terry,

I've managed to got both this one and your new post. I've posted my reply
your new thread. I think we can continue to discuss in that one. Please
feel free to post there. 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.)

--------------------
| Thread-Topic: DataGrid
| thread-index: AcWczSl+CneD7ielQ4uXPsua4furrg==
| X-WBNR-Posting-Host: 194.200.135.240
| From: "=?Utf-8?B?VGVycnkgSG9sbGFuZA==?="
<[email protected]>
| References: <#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: DataGrid
| Date: Tue, 9 Aug 2005 03:29:02 -0700
| Lines: 176
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10358
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Steven
|
| Thank you. I mangaged to get your sample code working. However I am
having
| difficulty getting the code in the article
| #Creating Custom Columns for the ASP.NET Datagrid
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
| /creatingcustomcolumns.asp
|
| I am able to get the example that you can download working but am unable
to
| get it working in an application of my own.
|
| I get the following error which occurrs on the line
| cell.text = dgi.DataItem(DataField)
|
| error
| =============
| Line 34: Throw New Exception(ex.InnerException.ToString)
| Line 35: End Try
| Line 36: End Sub
|
|
| Source File:
C:\_Development\CSA\Dev\ControlExtenders\clsDropDownColumn.vb
| Line: 34
|
| Stack Trace:
|
|
| [NullReferenceException: Object reference not set to an instance of an
| object.]
|
| =============
|
| Any ideas why this is happening?
|
| Also, is it possible for me to add a customcolumn completely
| programmatically (ie without the need to add a @register directive and a
| <ddg:DropDownColumn .../> tag on my page?
|
| thanks in advance
|
|
|
|
|
|
| "Steven Cheng[MSFT]" wrote:
|
| > Hi Terry,
| >
| > Thanks for the response. Here is my complete testcode:
| >
| > #complete custom column class
| > ========================
| > Public Class ColumnClass
| > Inherits System.Web.UI.WebControls.DataGridColumn
| >
| > Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
| > columnIndex As Integer, ByVal itemType As ListItemType)
| > MyBase.InitializeCell(cell, columnIndex, itemType)
| > If ((itemType <> ListItemType.Header) And (itemType <>
| > ListItemType.Footer)) Then
| >
| > Dim control1 As WebControl = Nothing
| >
| > Dim imgbtn = New System.Web.UI.WebControls.ImageButton
| >
| >
| > imgbtn.ImageUrl =
| >
"http://msdn.microsoft.com/library/toolbar/3.0/images/banners/msdn_masthead_
| > ltr.gif"
| >
| > imgbtn.CommandName = "Edit"
| >
| > control1 = imgbtn
| >
| > cell.Controls.Add(control1)
| > End If
| > End Sub
| >
| >
| >
| > End Class
| > ===================
| >
| > #the code in my page which contains the datagrid:
| >
| > =============here i dynamically add my custom datagrid column into the
| > grid=========
| > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
| > System.EventArgs) Handles MyBase.Init
| > InitializeComponent()
| >
| > Dim cc As New ColumnClass
| >
| > dgDynamic.Columns.Add(cc)
| >
| >
| > .......... other dynamic code
| >
| > End Sub
| >
| > ====================
| >
| > I there're anything else unclear or I can help ,please feel free to
post
| > here.
| > 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.)
| >
| > --------------------
| > | Thread-Topic: DataGrid
| > | thread-index: AcWb/jdG3dDvBk8USJKBuQOij5EFWQ==
| > | X-WBNR-Posting-Host: 194.200.135.240
| > | From: "=?Utf-8?B?VGVycnkgSG9sbGFuZA==?="
| > <[email protected]>
| > | References: <#[email protected]>
| > <[email protected]>
| > | Subject: RE: DataGrid
| > | Date: Mon, 8 Aug 2005 02:47:41 -0700
| > | Lines: 28
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:10328
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Thank you Steven
| > |
| > | Im not entirely sure what to do with code that you included in your
| > reply.
| > | I have tried the following
| > |
| > | Created a new user control that inherits
| > | System.Web.UI.WebControls.DataGridColumn and then paste in the
| > | Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
| > | columnIndex As
| > | Integer, ByVal
| > itemType
| > | As ListItemType)
| > | method that you included in your posting.
| > |
| > | When i try to compile I get an error " Event 'Init' cannot be found."
| > | on the event in my new ImageButonColumn control.
| > |
| > | Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
| > | System.EventArgs) Handles MyBase.Init
| > | 'CODEGEN: This method call is required by the Web Form
Designer
| > | 'Do not modify it using the code editor.
| > | InitializeComponent()
| > | End Sub
| > |
| > | If I delete this event then app compiles but Im a bit worried Im
doing
| > the
| > | wrong thing
| > |
| > |
| > | Terry
| > |
| >
| >
|
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top