You need two things...
A reference to the datagrid
The ItemIdentity of the DataGridItem that contains the DropDownList.
And you can get all this from the sender object in your event handler by
walking back up the object tree...
private void DropDownList_SelectedIndexChanged(object sender,
System.EventArgs e)
{
//ref to the drop down that triggered the event
DropDownList ddl = (DropDownList)sender;
//ref to The DataGridItem that holds the dropdown is the grandparent
//of the dropdown (the parent is a TableCell)
DataGridItem dgi = (DataGridItem)ddl.Parent.Parent;
//ref to the grid is the grandparent of the DataGridItem
//could also just set the ref to the grid control at a module level
//declaration
DataGrid dg = (DataGrid)dgi.Parent.Parent;
//then get the key
int iKey = (int)dg.DataKeys[dgi.ItemIndex];
}
HTH,
Dean Fiala
-----------------------------
Very Practical Software, Inc.
http://www.vpsw.com/links.aspx
-----Original Message-----
From: bh0526 [mailto:[EMAIL PROTECTED]
Sent: Thursday, August 19, 2004 3:07 PM
To: [EMAIL PROTECTED]
Subject: [AspNetAnyQuestionIsOk] DropdownList in Datagrid problem
Hi All,
I am having a problem with a dropdownlist in my datagrid. Let me
explain what I am trying to do first. I have a datagrid with a
small list of rows which I bind from a database table. This works
fine. I have several bound columns and one dropdownlist which is
also bound to another database table. I have my dropdownlist in an
itemtemplate. I also have code that selects the proper value from
the database and then selects the the appropiate item in the
dropdownlist. This all happens in the page load event and
everything is ok at this point.
Here is what I want to do:
I want to allow the user to be able to click in the dropdownlist for
each row in the datagrid, select an item, and update my database
table that filled the datagrid. I have Autopostback=True in my
dropdownlist. I also have a SelectedIndexChanged event in my code
behind. My problem is that I cannot seem to be able to get the
value of the DataKeyField in my datagrid. I need this value to
update my database file. Does anyone know how I get this value? I
know how to do this using the Editcommand and Updatecommand but I
want to do this just by allowing the user to click in the
dropdownlist.
Thanks,
Bob
Yahoo! Groups Links
------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/