A__
A__

Reputation: 37

How to set headers for columns in DataGridView, where data is populated from a sql query

I have the following code working, I just want to display my own column names for each column rather than the ones in the database.

Is there any way I can do that?

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "total_registration";
cmd.Connection = con;
using (SqlDataAdapter a = new SqlDataAdapter(cmd.CommandText, con))
{        
    DataTable t = new DataTable();
    a.Fill(t);

    grid.DataSource = t;
}

Upvotes: 2

Views: 3184

Answers (2)

ASh
ASh

Reputation: 35733

after binding datasource set column header text explicitly using the DataGridView.Columns[].HeaderText property:

grid.DataSource = t;
grid.Columns["ID"].HeaderText  = "Identifier";

Upvotes: 3

dfadfsadfs
dfadfsadfs

Reputation: 11

Yes, there ist a special SQL-Statement for this.

For example you have a table in your DB with the following columns ID, Name, Age and you want to name them like the following Identification LastName AgeYear

you could use the following sql-statement:

SELECT ID as Identification, Name as LastName, Age as AgeYear FROM db;

Hope it help.

Upvotes: 1

Related Questions