2010-10-13 85 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对象),那么你可以通过值而不是通过文本查找的项目,减轻另一失败点。