Gridview adding row dynamically on RowDataBound with the same RowState (Alternate or Normal)

rob waminal picture rob waminal · Jan 17, 2012 · Viewed 9.7k times · Source

I am adding rows dynamically on code behind depending on the Row currently bounded on RowDataBound event. I want that added row to be the same state (Alternate or Normal) of the currently bounded row is this possible?

I'm doing something like this but it doesn't follow what I want. I'm expecting the added row dynamically would be the same as the current row. But it is not.

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        GVData data = e.Row.DataItem as GVData;  // not original object just for brevity
        if(data.AddHiddenRow)
        {
            // the row state here should be the same as the current
            GridViewRow tr = new GridViewRow(e.Row.RowIndex +1, e.Row.RowIndex + 1, DataControlRowType.DataRow, e.Row.RowState);
            TableCell newTableCell = new TableCell();
            newTableCell.Style.Add("display", "none");
            tr.Cells.Add(newTableCell);

            ((Table)e.Row.Parent).Rows.Add(tr);
        }
    }
}

Answer

Dave Walker picture Dave Walker · Jan 18, 2012

Ok the issue is that the GridView doesnt recognise display:none as meaning that this row will be hidden and so counts it as part of its alternating style. In this instance what you need to do is implement the styling explicitly yourself and remove it from the gridview definiton.

I will add a semi-example shortly have a meeting.