Put data in 2nd row of a gridview

2019-02-25 15:39发布

问题:

I am using a foreach loop to insert data into a gridview such as this.

foreach (GridViewRow _row in grvbillDetail.Rows)
{
    _row = text;
    _row = text;
    _row = int;
    _row = int;
}

How can i make some of the data print on the first row and some print on the second row?

Thanks in advance!

回答1:

GridViewRow.RowIndex is the key to access your rows here.

Try this.

foreach (GridViewRow _row in grvbillDetail.Rows)
{
    if(_row.RowIndex == 0)
    {
        //this is the first row. do whatever you want here
    }
    if(_row.RowIndex == 1)
    {
        //this is the second row. do whatever you want here
    }
    //_row = text;
    //_row = text;
    //_row = int;
    //_row = int;
}

By the way, if you want to access only the first and second row, do something like this

GridViewRow firstRow = grvbillDetail.Rows[0] as GridViewRow;
GridViewRow secondRow = grvbillDetail.Rows[1] as GridViewRow;