郑国聪的程序员之路

gridview行高问题

当设定了gridview的属性值height设为一定值的时候,同时数据量比较少,例如只有1行的时候,gridview的行的高度就变得难以控制,即使设置RowStyle-Height也是不起作用的。

为了解决这个问题,我们可以在gridview DataBind以后,通过动态添加的方法,控制行高。代码如下:

 protected void imgSearch_Click(object sender, EventArgs e)
        {
            GridParameter objGridParameter = new GridParameter();
            DataTable positionDt = objPositionBL.GetPositionList(objGridParameter, txtPositionName.Text, hidDepartmentID.Value, ddlSalaryType.SelectedValue, ddlStatus.SelectedValue);
            GridRowCount.Value = positionDt.Rows.Count.ToString();
            SGItem.DataSource = positionDt;
            SGItem.DataBind();
            FixBlank();
        }

 

 private void FixBlank()
        {
            if (SGItem.Rows.Count < 10)
            {
                Control table = SGItem.Controls[0];
                for (int i = 0; i < 10 - SGItem.Rows.Count; i++)
                {
                    int rowindex = SGItem.Rows.Count + i + 1;
                    GridViewRow row = new GridViewRow(rowindex, -1, DataControlRowType.Separator, DataControlRowState.Normal);
                    row.CssClass = (rowindex % 2 == 0) ? "alternate" : "item";
                    for (int j = 0; j < SGItem.Columns.Count; j++)
                    {
                        if (j != 0 && j != 6 && j != 7)
                        {
                            TableCell cell = new TableCell();
                            cell.Text = "&nbsp;";
                            row.Controls.Add(cell);
                        }
                    }
                    table.Controls.AddAt(rowindex, row);

                }

            }
        }

 

PS:希望可以帮到各位吧。

posted on 2010-06-22 16:42  郑国聪  阅读(1951)  评论(0编辑  收藏  举报

导航