这可能很容易,但我无法在任何地方找到答案。我正在使用DataTable动态填充ASP.NET Web窗体上以前创建的DataGridView,但我想更改DataGridView上显示的列的名称,我根本无法弄清楚我做错了什么。以下是代码:
DataTable dtUsuarios = DBManager.RunSqlGetDataTable(
@"select b.UserName, c.Email, c.IsLockedOut, c.LastLoginDate,
case
when e.RoleName is not null then 1
else 0 end Admin
from dbo.aspnet_Applications a join dbo.aspnet_Users b
on a.ApplicationId = b.ApplicationId
join dbo.aspnet_Membership c
on b.ApplicationId = c.ApplicationId
and b.UserId = c.UserId
left join dbo.aspnet_UsersInRoles d
on d.UserId = b.UserId
left join dbo.aspnet_Roles e
on d.RoleId = e.RoleId
where a.ApplicationName = 'Mont Blanc Catalogo'");
dtUsuarios.Columns["UserName"].Caption = "Nome Usuário";
dtUsuarios.Columns["Email"].Caption = "E-mail";
dtUsuarios.Columns["IsLockedOut"].Caption = "Bloqueado";
dtUsuarios.Columns["LastLoginDate"].Caption = "Último Login";
dtUsuarios.Columns["Admin"].Caption = "Administrador";
DataView dvUsuarios = new DataView(dtUsuarios) { Sort = "UserName" };
gdvUsuarios.DataSource = dvUsuarios;
gdvUsuarios.DataBind();
//gdvUsuarios.Columns[0].HeaderText = "Nome Usuário";
//gdvUsuarios.Columns[1].HeaderText = "E-mail";
//gdvUsuarios.Columns[2].HeaderText = "Bloqueado";
//gdvUsuarios.Columns[3].HeaderText = "Último Login";
//gdvUsuarios.Columns[4].HeaderText = "Administrador";
设置更改DataTable标题的值不起作用,在DataBind之后,DataGridView的Columns.Count属性返回零列!执行代码后,一切都出现在我的asp页面上,但字幕错误。我做错了什么? TKS
答案 0 :(得分:1)
我认为我已经挖得够了,但显然还不够。我在这里找到了答案: Any way to manipulate the columns in GridView with AutoGenerateColumns = true?
以下是生成的代码:
public partial class admin_CadUsuarios : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dtUsuarios = DBManager.RunSqlGetDataTable(
@"select b.UserName, c.Email, c.IsLockedOut, c.LastLoginDate,
case
when e.RoleName is not null then 1
else 0 end Admin
from dbo.aspnet_Applications a join dbo.aspnet_Users b
on a.ApplicationId = b.ApplicationId
join dbo.aspnet_Membership c
on b.ApplicationId = c.ApplicationId
and b.UserId = c.UserId
left join dbo.aspnet_UsersInRoles d
on d.UserId = b.UserId
left join dbo.aspnet_Roles e
on d.RoleId = e.RoleId
where a.ApplicationName = 'Mont Blanc Catalogo'");
DataView dvUsuarios = new DataView(dtUsuarios) { Sort = "UserName" };
gdvUsuarios.DataSource = dvUsuarios;
gdvUsuarios.DataBind();
}
protected void gdvUsuarios_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Nome Usuário";
e.Row.Cells[1].Text = "E-mail";
e.Row.Cells[2].Text = "Bloqueado";
e.Row.Cells[3].Text = "Último Login";
e.Row.Cells[4].Text = "Administrador";
}
}
}
RowDataBound成功了。
答案 1 :(得分:0)
您可以尝试列别名,当然取决于您的特定sql应用程序/版本。
DataTable dtUsuarios = DBManager.RunSqlGetDataTable(
@"select
b.UserName [Nome Usuário],
c.Email [E-mail],
c.IsLockedOut [Bloqueado],
c.LastLoginDate [Último Login],
case
when e.RoleName is not null then 1
else 0 end [Administrador]
from dbo.aspnet_Applications a join dbo.aspnet_Users b
on a.ApplicationId = b.ApplicationId
join dbo.aspnet_Membership c
on b.ApplicationId = c.ApplicationId
and b.UserId = c.UserId
left join dbo.aspnet_UsersInRoles d
on d.UserId = b.UserId
left join dbo.aspnet_Roles e
on d.RoleId = e.RoleId
where a.ApplicationName = 'Mont Blanc Catalogo'");
DataView dvUsuarios = new DataView(dtUsuarios) { Sort = "[Nome Usuário]" };
gdvUsuarios.DataSource = dvUsuarios;
gdvUsuarios.DataBind();
答案 2 :(得分:0)
如果要重命名DataTable中的列,只需执行(ColumnName而不是Caption):
dtUsuarios.Columns["UserName"].ColumnName= "Nome Usuário"
如果要将显示的输出更改为用户,可能需要在标记内执行此操作。如果是GridView,请在HeaderText
BoundColumn