DropDownList in a GridView

Discussion in 'ASP .Net Web Controls' started by BrassicaNigra, Oct 22, 2007.

  1. I have a datagrid that has a dropdownlist in each row. When a user changes
    one of these drop down values, the autopostback allows me to handle the event
    using the OnSelectedIndexChanged event for the drop down.

    This works fine, but I cannot find a way to retrieve which row the drop down
    was changed in. The function is passed Sender and EventArgs, but neither of
    them seem to contain access to the row number where the drop down list was
    changed. In some other functions that are passed similar arguments I can
    access e.rows.... and get to row number, but for some reason this is not
    available in the EventArgs for my function. I do not want to use the
    EditItemTemplate on the gridview because that is overkill for what I am
    trying to accomplish. How do i get the row number of the drop down that is
    being changed?

    Following is the code of the function;

    protected void ProductionStatusDropDown_OnSelectedIndexChanged(object
    sender, EventArgs e)
    {
    SqlConnection connStatus = new SqlConnection("Data
    Source=.\\SQLEXPRESS;Database=AAPCB;Integrated Security=True;Connect
    Timeout=30;User Instance=False");
    SqlCommand scStatus = new SqlCommand("spUpdateProductionStatus",
    connStatus);
    SqlParameter paSalesOrderID =
    scStatus.Parameters.Add("@SalesOrderID", SqlDbType.Int);
    SqlParameter paReleaseID = scStatus.Parameters.Add("@ReleaseID",
    SqlDbType.Int);
    SqlParameter paWorkCenter = scStatus.Parameters.Add("@WorkCenter",
    SqlDbType.NVarChar, 50);
    SqlParameter paWorkCenterStatus =
    scStatus.Parameters.Add("@WorkCenterStatus", SqlDbType.NVarChar, 50);
    TableCell tcReleaseID = (TableCell)GridView1.Rows[0].Cells[9];
    DropDownList ddlWorkCenter = (DropDownList)sender;
    scStatus.CommandType = CommandType.StoredProcedure;
    paSalesOrderID.Value = Request.QueryString["SalesOrderID"];
    paReleaseID.Value = tcReleaseID.Text;
    paWorkCenterStatus.Value = "Queued";
    paWorkCenter.Value = ddlWorkCenter.SelectedValue;
    connStatus.Open();
    scStatus.ExecuteNonQuery();
    connStatus.Close();
    }

    The line

    TableCell tcReleaseID = (TableCell)GridView1.Rows[0].Cells[9];

    has the row hardwired to '0'. This is where I would like to use a variable
    that contains the row number of the line that had the drop down changed.

    Let me know if you need additional clarification.

    Thank you.

    Dale Hoffman
    BrassicaNigra, Oct 22, 2007
    #1
    1. Advertising

  2. Hi Dale,

    How are you defining the DropDownList in DataGrid's row? Are you using
    TemplateColumn and ItemTemplate?

    Usually you can turn on Trace and see the control hierarchy and get the
    DataGridItem where the DropDownList resides using Parent property.

    For example:

    <%@ Page Trace="true" ...

    ...

    <asp:DataGrid ID="grid1" runat="server" DataSourceID="SqlDataSource1">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:DropDownList AutoPostBack="true" ID="myddl"
    runat="server" OnSelectedIndexChanged ="myddl_OnSelectedIndexChanged">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>


    protected void myddl_OnSelectedIndexChanged(object sender, EventArgs e)
    {
    DropDownList myddl = (DropDownList)sender;
    DataGridItem item = (DataGridItem)myddl.Parent.Parent;
    int index = item.ItemIndex;
    }


    From the trace output, you can see:

    grid1$ctl02 System.Web.UI.WebControls.DataGridItem 317 0 0
    grid1$ctl02$ctl00 System.Web.UI.WebControls.TableCell 280 0 0
    grid1$ctl02$ctl01 System.Web.UI.LiteralControl 18 0 0
    grid1$ctl02$myddl System.Web.UI.WebControls.DropDownList

    This means the DropDownList.Parent.Parent is the DataGridItem.


    Hope this helps.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Oct 23, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,593
    mr2_93
    Oct 2, 2005
  2. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,860
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,293
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  4. keithb
    Replies:
    1
    Views:
    11,950
    shauns1
    Nov 1, 2006
  5. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,956
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
Loading...

Share This Page