Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > DropDownList in a GridView

Thread Tools

DropDownList in a GridView

Posts: n/a
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(ob ject
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",
SqlParameter paSalesOrderID =
scStatus.Parameters.Add("@SalesOrderID", SqlDbType.Int);
SqlParameter paReleaseID = scStatus.Parameters.Add("@ReleaseID",
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;

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

Reply With Quote
Walter Wang [MSFT]
Posts: n/a
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" ...


<aspataGrid ID="grid1" runat="server" DataSourceID="SqlDataSource1">
<aspropDownList AutoPostBack="true" ID="myddl"
runat="server" OnSelectedIndexChanged ="myddl_OnSelectedIndexChanged">

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.

Walter Wang ((E-Mail Removed), 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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
GridView Hierarchical View - Gridview in Gridview =?Utf-8?B?bWdvbnphbGVzMw==?= ASP .Net 1 05-09-2006 06:48 PM
GridView: Filter DropDownList for another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-29-2005 07:44 AM
GridView: Filtr DropDownList from another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-23-2005 01:31 PM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM