我有一个 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;
}
}
两者都不起作用。那么如何在动态复选框中对齐中心?