Pergunta

Is there a way to access elements that are in another row, while you're in a RowDataBound event?

public void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // how to compare e.Row with row above or row below?
}
Foi útil?

Solução

One thing I think you can do is...

foreach (GridViewRow gvrow in GridView1)
{
    //loop through gridview's rows and find the row you're looking for
}

I believe that RowDataBound adds the rows in the same order that the data source has them in, therefore if there are 100 records in your DataTable and you bind that DataTable to the GridView, RowDataBound gets called 100 times, for each row, in the order they exist in the DataTable. Therefore, you mentioned using RowDataBound to compare e.Row with the row above OR row below...but if they are coming in sequentially, there is no row below. This is something I hadn't really thought about before but I recall when doing testing with breakpoints that RowDataBound functions in this manner.

If you can't get something like that working, and you need this to happen even if it isn't pretty and don't get any better answers, you can store a copy of your databound DataTable or whatever your source for data is into a ViewState or Session variable, like ViewState["myDataTable"]. You can then retrieve the DataTable on RowDataBound event, and look at the rows above and below the row that is represented by e.Row (provided that you have some kind of cursor like an ID to identify the rows). By doing this, you CAN look at the next row that will be added to the GridView by RowDataBound, because it will be the next row in the DataTable (which already exists and can be viewed).

If you're doing stuff on a massive scale though, I imagine all this looping could get cumbersome.

Outras dicas

From a foggy memory, you can reference GridView's array of Rows from there and iterate them. You should be able to get e's position and access in any case the rows above, not sure about rows below as they aren't databound by then yet I suppose, unless this is a one-row rebind shot.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top