2010-02-12 39 views
1

當我在DropDownList中調用事件時,我無法在Datagridview中查找行索引。在ASP.NET的Datagridview中找不到行索引?

的代碼是:

protected void DescriptionOfProduct_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DropDownList _DescriptionOfProduct = (DropDownList)gvSales.FindControl("DescriptionOfProduct"); 

    Label _Unit = (Label)gvSales.FindControl("Unit"); 
    Label _PriceType = (Label)gvSales.FindControl("PriceType"); 
    //... 
} 

請給我的解決方案。

+0

是GridView控件內的下拉列表? – Aaron 2010-02-12 16:08:15

+0

是 http://www.freeimagehosting.net/image.php?038af0f2db.jpg – Nasir 2010-02-12 16:10:49

回答

1

如果下拉列表位於gridview內部,那麼您可以通過意識到它位於一行內的單元格中來找到該行。所以,它的父母的父母因此是行。

GridViewRow currentRow = (GridViewRow)_DescriptionOfProduct.Parent.Parent; 
int index = currentRow.RowIndex; 

編輯

如果你仍然有這個問題,我建議什麼米切爾賣家在他的回答說。通過將發件人轉換爲下拉列表來查找您的下拉列表。然後你可以找到你的行。從那裏,你可以通過使用行的單元格來獲取每個控件來獲取控件。

這裏是我怎麼這樣做過一個例子...

protected void actionList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList list = (DropDownList)sender; 
     GridViewRow row = (GridViewRow)list.Parent.Parent; 
     Label id = (Label)row.Cells[0].Controls[1];   

     // etc...... 
    } 
1

事件的發件人應該是您的下拉列表。

DropDownList myList = sender as DropDownList; 
if(myList != null) 
{ 
    //Now you have your selected index myList.SelectedIndex 
} 

如果發送給其他人,發件人顯示爲其他內容。