Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1009439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:55:46+00:00 2026-05-16T08:55:46+00:00

This is probably easy, but I couldn’t find an answer anywhere. I’m filling a

  • 0

This is probably easy, but I couldn’t find an answer anywhere. I’m filling a previously created DataGridView on an ASP.NET Web Form dynamically with a DataTable, but I want to change the name of the columns that appear on that DataGridView, and I simply can’t figure out what I’m doing wrong. Below is the code:

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";

Setting the values changing the caption of the DataTable doesn’t work, and after the DataBind, the Columns.Count property for the DataGridView return zero columns! After executing the code, everything appears normally at my asp page, but the captions are wrong. What I am doing wrong?
Tks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:55:46+00:00Added an answer on May 16, 2026 at 8:55 am

    I thought I had dug enough, but not enough, apparently. The answer I found here:
    Any way to manipulate the columns in GridView with AutoGenerateColumns = true?

    Below is the resulting code:

    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";
            }
        }
    }
    

    The RowDataBound did the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably easy, but sadly I can't really find the answer. Let's say
This is probably an easy Visual Studio question, but I couldn't find it on
This is probably an easy question but I can't figure out the best answer
This is probably a really easy question to answer, but for some reason I'm
This probably has probably been asked before, but I couldn't find anything relevant. Would
This problem is probably very easy to solve but somehow I cannot find a
There is probably an easy answer for this, but when I added DateTime.Now.ToString() to
Probably really easy for a pro, but could someone re-write this from it's PHP
This is probably really easy but I can't seem to figure out how to
This is probably an easy solution but I can't get my head around it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.