2010-10-13 86 views
0

我正在使用下面的代碼從gridview中提取數據並將其填充到文本框的日期和項目和類別的兩個下拉列表中。必須選擇gridview行兩次下拉才能正確選擇

對於gridview中的一些行,除了類ddl正確填充之外的所有行。如果我再次單擊該行,類別ddl將顯示正確的類別。

誰能告訴我爲什麼我必須點擊兩次的一些行?我該如何解決這個問題?

謝謝

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //// Get the currently selected row using the SelectedRow property. 
    GridViewRow row = GridView1.SelectedRow; 

    txtSunday.Text = (row.Cells[6].Controls[0] as DataBoundLiteralControl).Text.Trim(); 
    txtMonday.Text = (row.Cells[7].Controls[0] as DataBoundLiteralControl).Text.Trim(); 
    txtTuesday.Text = (row.Cells[8].Controls[0] as DataBoundLiteralControl).Text.Trim(); 
    txtWednesday.Text = (row.Cells[9].Controls[0] as DataBoundLiteralControl).Text.Trim(); 
    txtThursday.Text = (row.Cells[10].Controls[0] as DataBoundLiteralControl).Text.Trim(); 
    txtFriday.Text = (row.Cells[11].Controls[0] as DataBoundLiteralControl).Text.Trim(); 
    txtSaturday.Text = (row.Cells[12].Controls[0] as DataBoundLiteralControl).Text.Trim(); 

    // Set ProjectList ddl to Project in selected row 
    if (ProjectList.Items.FindByText(row.Cells[2].Text.Trim()) != null) 
    { 
     ProjectList.ClearSelection(); 
     ProjectList.Items.FindByText(row.Cells[2].Text.Trim()).Selected = true; 
    } 


/// This is the ddl that doesn't always populate correctly unless you click the 
/// gridview row selector twice 

    // Set CategoryList ddl to Category in selected row 
    if (CategoryList.Items.FindByText(row.Cells[4].Text.Trim()) != null) 
    { 
     CategoryList.ClearSelection(); 
     CategoryList.Items.FindByText(row.Cells[4].Text.Trim()).Selected = true; 
    } 
} 

回答

0

我想我明白了這一點。在設置項目後,我需要重新綁定類別ddl

// Set ProjectList ddl to Project in selected row 
    if (ProjectList.Items.FindByText(row.Cells[2].Text.Trim()) != null) 
    { 
     ProjectList.ClearSelection(); 
     ProjectList.Items.FindByText(row.Cells[2].Text.Trim()).Selected = true; 
    } 

    // Set CategoryList ddl to Category in selected row 


// I added this line and it seems to work now 
     CategoryList.DataBind(); 

    if (CategoryList.Items.FindByText(row.Cells[4].Text.Trim()) != null) 
    { 
     CategoryList.ClearSelection(); 
     CategoryList.Items.FindByText(row.Cells[4].Text.Trim()).Selected = true; 
    } 
0

我不知道爲什麼它採取兩次點擊,讓您的下拉列表中正確地選擇,但它可能與回發事件排序/ ViewState的問題做。你可能要考慮的一件事是使用綁定網格的數據而不是網格中控件的文本。 IOW,假設你綁定到對象的集合是這樣的:

public class ProjectSchedule 
{ 
    public string Project {get;set;} 
    public int CategoryId {get;set;} 
    public string Category {get;set;} 
    public string Sunday {get;set;} 
    public string Monday {get;set;} 
    public string Tuesday {get;set;} 
    public string Wednesday {get;set;} 
    public string Thursday {get;set;} 
    public string Friday {get;set;} 
    public string Saturday {get;set;} 
} 

然後,在SelectedIndexChanged事件處理程序,讓您的數據是這樣的:

GridViewRow row = GridView1.SelectedRow; 
ProjectSchedule ps = row.DataItem as ProjectSchedule; 
if (ps != null) 
{ 
    txtSunday.Text = ps.Sunday; 
    // the rest of the days... 
    ListItem categoryItem = CategoryList.Items.FindByText(ps.Category); 
    if (categoryItem != null) 
    { 
     CategoryList.ClearSelection(); 
     categoryItem.Selected = true; 
    } 
    // same with ProjectList 
} 

假設你的控件將要降落在同一列中每次都限制可維護性。例如,假設需求發生變化,表示具有日期的列在項目列之前。這是很多需要改變的指標。

,如果您有您的類別,並通過一些諸如此類的索引它甚至會更好(例如,CategoryId財產我走私到上述ProjectSchedule對象),那麼你可以通過值而不是通過文本查找的項目,減輕另一失敗點。