Gridview - Loop through multiple header rows

痞子三分冷 提交于 2019-12-13 05:07:46

问题


In a console app, a gridview is created dynamically.

        GridView gv = new GridView();
        gv.AutoGenerateColumns = false;
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            BoundField boundfield = new BoundField();
            boundfield.DataField = dt.Columns[i].ColumnName.ToString();
            boundfield.HeaderText = arr[i].ToString();// dt.Columns[i].ColumnName.ToString();
            gv.Columns.Add(boundfield);
        }

Then, two more header rows are added. Here is a partial of one row:

        GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
        TableCell HeaderCell = new TableCell();

        HeaderCell.Text = "";
        HeaderCell.ColumnSpan = 4;
        HeaderGridRow.Cells.Add(HeaderCell);

        //more header cells added then another row here

        gv.DataSource = dt;
        gv.DataBind();

        gv.Controls[0].Controls.AddAt(0, HeaderGridRow);

Using a foreach loop, adjusting separate cell borders not in the header is done:

        foreach (GridViewRow row in gv.Rows)
        {               
            TableCell tCell = row.Cells[0];
            tCell.Attributes["style"] = "border-right:0";               
        }

I have been trying to find a way to loop through the header rows and adjust some borders on the cells.

Tried using foreach (GridViewRow row in gv.HeaderRow) but gv.HeaderRow does not have a public definition for GetEnumerator.

Tried for(int i = 0; i < gv.HeaderRow.Cells.Count; i++) but that doesn't seem to work either.

Anyone have any suggestions?

Thanks!!


回答1:


Try this

GridViewRow headerRow = gv.HeaderRow;            
TableCell hCell = headerRow.Cells[0];
hCell.Attributes["style"] = "border-right:0";               

Hope it helps



来源:https://stackoverflow.com/questions/22559072/gridview-loop-through-multiple-header-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!