如何在侧网格视图中对齐动态复选框
本文关键字:对齐 动态 复选框 视图 网格 | 更新日期: 2023-09-27 18:30:02
我有一个网格视图,它有动态的行和列。还有一些单元格有动态复选框。所以我想在复选框中居中对齐。
这是我的密码。
<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;
}
}
两者都不起作用。那么,如何在动态复选框中居中对齐呢?
您可以通过将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";
然后在标记中,您需要定义center
CSS类,如下所示:
<style type="text/css">
.center {
text-align: center;
}
</style>
或
您可以创建一个.css
文件来保存上面的定义,然后通过标记中的<link>
标记引用CSS文件。