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