Tuesday, March 13, 2012

Setting the SelectedIndex of a DropDownList in a Datagrid

Hi There,
I have two datagrids on a webform both of which display DropDownLists when
edit mode is invoked.
That being said, does anyone have a generic or straightforward way in C# so
that when I go into the edit mode of a row, the dropdownlists display the
field values of that row? (They currently jump to the first value in the
dropdownlist)
Any help would be GREATLY appreciated,
_K.K Bryan wrote:

> Hi There,
> I have two datagrids on a webform both of which display DropDownLists when
> edit mode is invoked.
> That being said, does anyone have a generic or straightforward way in C# s
o
> that when I go into the edit mode of a row, the dropdownlists display the
> field values of that row? (They currently jump to the first value in the
> dropdownlist)
> Any help would be GREATLY appreciated,
> _K.
>
>
grab the dropdown list from the datagrid
something like
for each item in DataGrid1.Items
dim ddl as DropDownList = item.FindControl("DropDownList1")
ddl.selectedItemIndex = MyIndexValue
next
remember to do it after databinding has been done, and not in Item_create
you can do it at Item_DataBound,
should work
hth
-ashish
You need to find the ddl in the collection of controls.
So you do something like this.
DropDownList ddl = (DropDownList) e.Item.FindControl("lstID");
then select the correct item as required
A
"K Bryan" <KevinNoSPAMMM@.campi.org> wrote in message
news:exd04gmIEHA.3664@.TK2MSFTNGP11.phx.gbl...
> Hi There,
> I have two datagrids on a webform both of which display DropDownLists when
> edit mode is invoked.
> That being said, does anyone have a generic or straightforward way in C#
so
> that when I go into the edit mode of a row, the dropdownlists display the
> field values of that row? (They currently jump to the first value in the
> dropdownlist)
> Any help would be GREATLY appreciated,
> _K.
>
>
Xref: kermit microsoft.public.dotnet.framework.aspnet:261292
Thanks for the reply.
Where would I put this code? What event?
"Andrew de la Harpe" <andrew_dela@.hotmail.com> wrote in message
news:u8SKr%23oIEHA.2556@.TK2MSFTNGP12.phx.gbl...
> You need to find the ddl in the collection of controls.
> So you do something like this.
> DropDownList ddl = (DropDownList) e.Item.FindControl("lstID");
> then select the correct item as required
> A
> "K Bryan" <KevinNoSPAMMM@.campi.org> wrote in message
> news:exd04gmIEHA.3664@.TK2MSFTNGP11.phx.gbl...
when
> so
the
>
In the OnDataBound event for the grid.
Heres as example.
private void setDataBinding(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if(lit == ListItemType.EditItem)
{
DataRowView drv = (DataRowView) e.Item.DataItem;
DropDownList dll = (DropDownList) e.Item.FindControl("lstLaterability");
etc..
}
}
"K Bryan" <KevinNoSPAMMM@.campi.org> wrote in message
news:uHPpD3xIEHA.716@.TK2MSFTNGP12.phx.gbl...
> Thanks for the reply.
> Where would I put this code? What event?
>
> "Andrew de la Harpe" <andrew_dela@.hotmail.com> wrote in message
> news:u8SKr%23oIEHA.2556@.TK2MSFTNGP12.phx.gbl...
> when
C#
> the
the
>

0 comments:

Post a Comment