DropDownList in a GridView

B

BrassicaNigra

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
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top