Setting selected item in a DropDownList in a DataGrid
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngdatagridrepeaterdatalist' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Nick North
I seem unable to set the selected item in a DropDownList in a DataGrid. My
DataGrid has a TemplateColumn, which is added at run time. The
EditItemTemplate is an object of a class, DropDownCell, implementing
ITemplate.

The DropDownCell.InstantiateIn method creates and binds a DropDownList, and
then adds a DataBinding event handler, like this:

Dim mDb As New DatabaseWrapper(), MyCombo As New DropDownList()

MyCombo.ID = mParamName

MyCombo.DataSource = mDb.ReturnSQL(mSql)

MyCombo.DataValueField = "Value"

MyCombo.DataTextField = "Text"

MyCombo.DataBind()

' This has to be done after the DataBind above, otherwise it gets called
immediately and it all falls over.
AddHandler MyCombo.DataBinding, AddressOf Me.DataBindingHandler
container.Controls.Add(MyCombo)

mParamName and mSql are object variables set in the DropDownCell.New method.

The DropDownCell.DataBindingHandler attempts to set the selected item of the
DropDownList to the current value in the selected DataGrid row, like this:

Dim MyCtrl As DropDownList, MyItem As DataGridItem, MyDRView As DataRowView
Dim Display As String, Item As ListItem, mDB As New DatabaseWrapper() MyCtrl
= CType(sender, DropDownList)

MyItem = CType(MyCtrl.NamingContainer, DataGridItem)

MyDRView = CType(MyItem.DataItem, DataRowView)

Display = MyDRView(mParamName)

' Default to the current value - this doesn't work for some reason.

MyCtrl.ClearSelection()

Item = MyCtrl.Items.FindByText(Display)

If Not (Item Is Nothing) Then

Item.Selected = True

End If

When the DataGrid goes into edit mode, the drop down list is populated
properly, the DataBindingHandler method is called properly, and all executes
OK. When debugging I can see the the FindByText call is identifying the
right item, and its Selected property is being set to True. However, the
resulting web page has the first element of the DropDownList selected. I
tried just short-curcuiting the whole thing and setting
MyCtrl.SelectedIndex=3, and that was also ignored.

If anyone can help with this problem, I'd be very grateful. Thanks,

Nick North

Orbis Investment Advisory Ltd

Reply to this message...
 
    
Eric Landes (VIP)
Nick,
Here's a link to something I put together on this subject. I hope it =
helps:
http://www.aspalliance.com/corporatecoder/Articles2002/PopulateDropDownIn=
Dat
agrid.aspx

*************************=20
Eric Landes=20
Senior Programmer/Analyst=20
For Articles on Crystal.Net, Datagrids and more check out
http://www.aspalliance.com/corporatecoder/
*************************=20
All statements expressed are my own, and do not reflect the opinion of =
my
employer, unless stated otherwise.=20

-----Original Message-----
From: Nick North [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 5:59 AM
To: aspngDataGridRepeaterDatalist
Subject: [aspngdatagridrepeaterdatalist] Setting selected item in a
DropDownList in a DataGrid

I seem unable to set the selected item in a DropDownList in a DataGrid. =
My
DataGrid has a TemplateColumn, which is added at run time. The
EditItemTemplate is an object of a class, DropDownCell, implementing
ITemplate.

The DropDownCell.InstantiateIn method creates and binds a DropDownList, =
and
then adds a DataBinding event handler, like this:

Dim mDb As New DatabaseWrapper(), MyCombo As New DropDownList()

MyCombo.ID =3D mParamName

MyCombo.DataSource =3D mDb.ReturnSQL(mSql)

MyCombo.DataValueField =3D "Value"

MyCombo.DataTextField =3D "Text"

MyCombo.DataBind()

' This has to be done after the DataBind above, otherwise it gets called
immediately and it all falls over.
AddHandler MyCombo.DataBinding, AddressOf Me.DataBindingHandler
container.Controls.Add(MyCombo)

mParamName and mSql are object variables set in the DropDownCell.New =
method.

The DropDownCell.DataBindingHandler attempts to set the selected item of =
the
DropDownList to the current value in the selected DataGrid row, like =
this:

Dim MyCtrl As DropDownList, MyItem As DataGridItem, MyDRView As =
DataRowView
Dim Display As String, Item As ListItem, mDB As New DatabaseWrapper() =
MyCtrl
=3D CType(sender, DropDownList)

MyItem =3D CType(MyCtrl.NamingContainer, DataGridItem)

MyDRView =3D CType(MyItem.DataItem, DataRowView)

Display =3D MyDRView(mParamName)

' Default to the current value - this doesn't work for some reason.

MyCtrl.ClearSelection()

Item =3D MyCtrl.Items.FindByText(Display)

If Not (Item Is Nothing) Then

Item.Selected =3D True

End If

When the DataGrid goes into edit mode, the drop down list is populated
properly, the DataBindingHandler method is called properly, and all =
executes
OK. When debugging I can see the the FindByText call is identifying the
right item, and its Selected property is being set to True. However, the
resulting web page has the first element of the DropDownList selected. I
tried just short-curcuiting the whole thing and setting
MyCtrl.SelectedIndex=3D3, and that was also ignored.

If anyone can help with this problem, I'd be very grateful. Thanks,

Nick North

Orbis Investment Advisory Ltd

Confidentiality Notice: This e-mail message, including any
attachments, is for the sole use of the intended recipient(s) and may
contain confidential and privileged information. Any unauthorized
review, use, disclosure or distribution is prohibited. If you are not
the intended recipient, please contact the sender by reply e-mail and
destroy all copies of the original message.

PARTNERS Health Plan Phone: 574-233-4899
100 E. Wayne St., Suite 502 Fax: 574-234-7484
South Bend, IN 46601 www.partnersindiana.com

Reply to this message...
 
    
Nick North
Thanks for this - I will give it a go. The implication seems to be that =
the selected item cannot be set during grid data binding, but can only =
be done afterwards. How annoying.

Nick

-----Original Message-----
From:     Eric Landes [mailto:Click here to reveal e-mail address]=20
Sent:    7 August 2002 13:41
To:    aspngDataGridRepeaterDatalist
Subject:    [aspngdatagridrepeaterdatalist] RE: Setting selected item in a =
DropDownList in a DataGrid

Nick,
Here's a link to something I put together on this subject. I hope it =
helps:
http://www.aspalliance.com/corporatecoder/Articles2002/PopulateDropDownIn=
Dat
agrid.aspx

*************************=20
Eric Landes=20
Senior Programmer/Analyst=20
For Articles on Crystal.Net, Datagrids and more check out
http://www.aspalliance.com/corporatecoder/
*************************=20
All statements expressed are my own, and do not reflect the opinion of =
my
employer, unless stated otherwise.=20

-----Original Message-----
From: Nick North [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 5:59 AM
To: aspngDataGridRepeaterDatalist
Subject: [aspngdatagridrepeaterdatalist] Setting selected item in a
DropDownList in a DataGrid

I seem unable to set the selected item in a DropDownList in a DataGrid. =
My
DataGrid has a TemplateColumn, which is added at run time. The
EditItemTemplate is an object of a class, DropDownCell, implementing
ITemplate.

The DropDownCell.InstantiateIn method creates and binds a DropDownList, =
and
then adds a DataBinding event handler, like this:

Dim mDb As New DatabaseWrapper(), MyCombo As New DropDownList()

MyCombo.ID =3D mParamName

MyCombo.DataSource =3D mDb.ReturnSQL(mSql)

MyCombo.DataValueField =3D "Value"

MyCombo.DataTextField =3D "Text"

MyCombo.DataBind()

' This has to be done after the DataBind above, otherwise it gets called
immediately and it all falls over.
AddHandler MyCombo.DataBinding, AddressOf Me.DataBindingHandler
container.Controls.Add(MyCombo)

mParamName and mSql are object variables set in the DropDownCell.New =
method.

The DropDownCell.DataBindingHandler attempts to set the selected item of =
the
DropDownList to the current value in the selected DataGrid row, like =
this:

Dim MyCtrl As DropDownList, MyItem As DataGridItem, MyDRView As =
DataRowView
Dim Display As String, Item As ListItem, mDB As New DatabaseWrapper() =
MyCtrl
=3D CType(sender, DropDownList)

MyItem =3D CType(MyCtrl.NamingContainer, DataGridItem)

MyDRView =3D CType(MyItem.DataItem, DataRowView)

Display =3D MyDRView(mParamName)

' Default to the current value - this doesn't work for some reason.

MyCtrl.ClearSelection()

Item =3D MyCtrl.Items.FindByText(Display)

If Not (Item Is Nothing) Then

Item.Selected =3D True

End If

When the DataGrid goes into edit mode, the drop down list is populated
properly, the DataBindingHandler method is called properly, and all =
executes
OK. When debugging I can see the the FindByText call is identifying the
right item, and its Selected property is being set to True. However, the
resulting web page has the first element of the DropDownList selected. I
tried just short-curcuiting the whole thing and setting
MyCtrl.SelectedIndex=3D3, and that was also ignored.

If anyone can help with this problem, I'd be very grateful. Thanks,

Nick North

Orbis Investment Advisory Ltd

Confidentiality Notice: This e-mail message, including any
attachments, is for the sole use of the intended recipient(s) and may
contain confidential and privileged information. Any unauthorized
review, use, disclosure or distribution is prohibited. If you are not
the intended recipient, please contact the sender by reply e-mail and
destroy all copies of the original message.

PARTNERS Health Plan Phone: 574-233-4899
100 E. Wayne St., Suite 502 Fax: 574-234-7484
South Bend, IN 46601 www.partnersindiana.com

| [aspngdatagridrepeaterdatalist] member Click here to reveal e-mail address =3D YOUR ID
| http://www.aspfriends.com/aspfriends/aspngdatagridrepeaterdatalist.asp =
=3D JOIN/QUIT

Reply to this message...
 
    
Andy Smith
take a look at my BoundLookupColumn on www.metabuilders.com
makes it insanely easy to put a DDL in a DataGrid.

__
Andy Smith
Keyboard Jockey #3a7-2.78.1

[Original message clipped]


| [aspngdatagridrepeaterdatalist] member Click here to reveal e-mail address =3D YOUR =
ID
| http://www.aspfriends.com/aspfriends/aspngdatagridrepeaterdatalist.asp" target="_blank">http://www.aspfriends.com/aspfriends/aspngdatagridrepeaterdatalist.asp =
=3D JOIN/QUIT

Reply to this message...
 
 
System.Data.DataRowView
System.Web.UI.DataBinding
System.Web.UI.Design.DataBindingHandler
System.Web.UI.ITemplate
System.Web.UI.WebControls.DataGrid
System.Web.UI.WebControls.DataGridItem
System.Web.UI.WebControls.DropDownList
System.Web.UI.WebControls.ListItem
System.Web.UI.WebControls.TemplateColumn
System.Windows.Forms.DataGrid




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com