views:

19

answers:

2

I'm using the code below to extract data from a gridview and populate it into textboxes for the days and two drop downs for Project and Category.

For some rows in the gridview everything but the category ddl populates correctly. If I click the row a second time the category ddl displays the correct category.

Can anyone tell me why I have to click twice for some rows? And how do I fix this?

Thank you

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;
    }
}
A: 

I'm not sure why it's taking two clicks to get your drop down list to select correctly, but it might have to do with postback event ordering/ViewState issues. One thing you may want to consider is using the data you're binding the grid to rather than the text of the controls in the grid. IOW, assuming you're binding to an collection of objects like this:

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;}
}

Then, in the SelectedIndexChanged event handler, get your data like this:

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
}

Assuming your controls are going to land in the same column every time limits maintainability. For instance, say the requirements change to say the columns with the days are before the Project column. That's a lot of indices to change.

It would be even better if you have your categories and whatnot indexed by something (e.g., the CategoryId property I smuggled into the ProjectSchedule object above), then you could look up the item by value instead of by text, relieving another point of failure.

Mike McCaughan
A: 

I think I figured this out. I needed to rebind the category ddl after setting the project

    // 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;
    }
Joshua Slocum