-1

我有一个 Grid 视图,它有动态行和列。还有一些单元格有动态复选框。所以我想在复选框中对齐中心。

这是我的代码。

<asp:GridView ID="gridUserAcess" runat="server" >

</asp:GridView>



    protected void Page_Load(object sender, EventArgs e)
    {        

            IList<PAGES> pageName = (from c in db.PAGES
                                       select c).ToList();

            int pagecount = pageName.Count;

            IList<ROLES> RoleName = (from r in db.ROLES
                                       select r).ToList();
            int rolrcount = RoleName.Count;

            DataTable dt = new DataTable("UserAcess");
            DataColumn dc1 = new DataColumn("PageName");
            dt.Columns.Add(dc1);

            foreach (var item in RoleName)
            {                    
                DataColumn dc = new DataColumn(item.RoleName);
                dt.Columns.Add(dc);

            }

            int i = 0;
            foreach (var page in pageName)
            {
                i += 1;
                DataRow dr = dt.NewRow();
                dr["PageName"] = page.PAGE_NAME;                    
                dt.Rows.Add(dr);
            }

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


            for (int p = 1; p <= rolrcount; p++)
            {
                foreach (GridViewRow row in gridUserAcess.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox cb = new CheckBox();

                        string pName = row.Cells[0].Text;                           
                        cb.Checked = true;
                        cb.ID = "che" + row.ID + p;
                        row.Cells[p].Controls.Add(cb);
                        row.Cells[p].HorizontalAlign = HorizontalAlign.Center;//this is not working
                    }
                }
            } 
    }

我也试试这个..

protected void gridUserAcess_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = new GridViewRow(e.Row.RowIndex + 1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
            TableCell cell = new TableCell();

            cell.HorizontalAlign = HorizontalAlign.Left;

            Control c = new Control(); 
            cell.Controls.Add(c);
            row.Cells.Add(cell);


        }

或者

  protected void gridUserAcess_RowDataBound(object sender, GridViewRowEventArgs e)
       {      

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
                e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Center;
                e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Center;
            }
        }

两者都不起作用。那么如何在动态复选框中对齐中心?

4

1 回答 1

0

您可以通过将 CSS clss 应用于要居中的每个单元格来做到这一点,如下所示:

e.Row.Cells[1].CssClass = "center";
e.Row.Cells[2].CssClass = "center";
e.Row.Cells[3].CssClass = "center";
e.Row.Cells[4].CssClass = "center";

然后在您的标记中,您需要定义您的centerCSS 类,如下所示:

<style type="text/css">
    .center {
        text-align: center;
    }
</style>

或者

您可以创建一个.css文件来保存上述定义,然后通过<link>标记中的标签引用 CSS 文件。

于 2013-09-04T00:53:55.513 回答