Setting the SelectedIndex of a DropDownList in a Datagrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K Bryan

    #1

    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.




  • Ashish

    #2
    Re: Setting the SelectedIndex of a DropDownList in a Datagrid

    K Bryan wrote:
    [color=blue]
    > 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.
    >
    >
    >
    >[/color]

    grab the dropdown list from the datagrid

    something like

    for each item in DataGrid1.Items
    dim ddl as DropDownList = item.FindContro l("DropDownList 1")
    ddl.selectedIte mIndex = 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

    Comment

    • Andrew de la Harpe

      #3
      Re: Setting the SelectedIndex of a DropDownList in a Datagrid

      You need to find the ddl in the collection of controls.
      So you do something like this.
      DropDownList ddl = (DropDownList) e.Item.FindCont rol("lstID");

      then select the correct item as required

      A
      "K Bryan" <KevinNoSPAMMM@ campi.org> wrote in message
      news:exd04gmIEH A.3664@TK2MSFTN GP11.phx.gbl...[color=blue]
      > 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#[/color]
      so[color=blue]
      > 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.
      >
      >
      >
      >[/color]


      Comment

      • K Bryan

        #4
        Re: Setting the SelectedIndex of a DropDownList in a Datagrid

        Thanks for the reply.

        Where would I put this code? What event?



        "Andrew de la Harpe" <andrew_dela@ho tmail.com> wrote in message
        news:u8SKr%23oI EHA.2556@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > You need to find the ddl in the collection of controls.
        > So you do something like this.
        > DropDownList ddl = (DropDownList) e.Item.FindCont rol("lstID");
        >
        > then select the correct item as required
        >
        > A
        > "K Bryan" <KevinNoSPAMMM@ campi.org> wrote in message
        > news:exd04gmIEH A.3664@TK2MSFTN GP11.phx.gbl...[color=green]
        > > Hi There,
        > >
        > > I have two datagrids on a webform both of which display DropDownLists[/color][/color]
        when[color=blue][color=green]
        > > edit mode is invoked.
        > >
        > > That being said, does anyone have a generic or straightforward way in C#[/color]
        > so[color=green]
        > > that when I go into the edit mode of a row, the dropdownlists display[/color][/color]
        the[color=blue][color=green]
        > > field values of that row? (They currently jump to the first value in the
        > > dropdownlist)
        > >
        > > Any help would be GREATLY appreciated,
        > >
        > > _K.
        > >
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Andrew de la Harpe

          #5
          Re: Setting the SelectedIndex of a DropDownList in a Datagrid

          In the OnDataBound event for the grid.
          Heres as example.
          private void setDataBinding( object sender,
          System.Web.UI.W ebControls.Data GridItemEventAr gs e)

          {


          ListItemType lit = e.Item.ItemType ;

          if(lit == ListItemType.Ed itItem)

          {

          DataRowView drv = (DataRowView) e.Item.DataItem ;

          DropDownList dll = (DropDownList) e.Item.FindCont rol("lstLaterab ility");

          etc..

          }

          }



          "K Bryan" <KevinNoSPAMMM@ campi.org> wrote in message
          news:uHPpD3xIEH A.716@TK2MSFTNG P12.phx.gbl...[color=blue]
          > Thanks for the reply.
          >
          > Where would I put this code? What event?
          >
          >
          >
          > "Andrew de la Harpe" <andrew_dela@ho tmail.com> wrote in message
          > news:u8SKr%23oI EHA.2556@TK2MSF TNGP12.phx.gbl. ..[color=green]
          > > You need to find the ddl in the collection of controls.
          > > So you do something like this.
          > > DropDownList ddl = (DropDownList) e.Item.FindCont rol("lstID");
          > >
          > > then select the correct item as required
          > >
          > > A
          > > "K Bryan" <KevinNoSPAMMM@ campi.org> wrote in message
          > > news:exd04gmIEH A.3664@TK2MSFTN GP11.phx.gbl...[color=darkred]
          > > > Hi There,
          > > >
          > > > I have two datagrids on a webform both of which display DropDownLists[/color][/color]
          > when[color=green][color=darkred]
          > > > edit mode is invoked.
          > > >
          > > > That being said, does anyone have a generic or straightforward way in[/color][/color][/color]
          C#[color=blue][color=green]
          > > so[color=darkred]
          > > > that when I go into the edit mode of a row, the dropdownlists display[/color][/color]
          > the[color=green][color=darkred]
          > > > field values of that row? (They currently jump to the first value in[/color][/color][/color]
          the[color=blue][color=green][color=darkred]
          > > > dropdownlist)
          > > >
          > > > Any help would be GREATLY appreciated,
          > > >
          > > > _K.
          > > >
          > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...