rai nalasa rai nalasa - 8 days ago 6
ASP.NET (C#) Question

return row number where cell is similar to string

How could I get the row number of my if statement?

private void load_session_value()
{
string trans_id = Session["transaction_id_report"].ToString();
string trans_number = Session["transaction_no_report"].ToString();

//string grid_value_id = GridView1
//string grid_value_num
if (trans_id != null && trans_number != null)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[1].Text.ToString() == trans_id && row.Cells[2].Text.ToString() == trans_number)
{

row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
//GridView1.SelectedIndex = rowIndex;
GridView1.SelectedIndex = ???

GridView1_SelectedIndexChanged(new object(), new EventArgs());
}
}
}
}


I need the row number for my
GridView1.SelectedIndex
currently the
row.color
is getting on the right row. But if possible could I get the
rownumber
that has the
row.color
?

Answer

Just switch your foreach statement with a for statement..

for (int i = 0; i <  GridView1.Rows.Count; i++)
{
    ...
    GridView1.SelectedIndex = i;
}
Comments