Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Changing Column Headers in DataGridView

I've got a datagridview in my project, which is populated from a SQL database with the following code:

 public cToDoList(string paramUser, DateTime paramDueDate)
    {
        string sqlStat = "SELECT * FROM tblDiary " +
                         "WHERE DiaryUserFor = @User " +
                         "AND DiaryDueDate <= @DueDate;";
        SqlCommand sqlCom = new SqlCommand(sqlStat);
        sqlCom.Parameters.Add("@User", SqlDbType.VarChar);
        sqlCom.Parameters.Add("@DueDate", SqlDbType.Date);
        sqlCom.Parameters["@User"].Value = paramUser;
        sqlCom.Parameters["@DueDate"].Value = paramDueDate.Date;

        cSqlQuery sqlQ = new cSqlQuery(sqlCom, "table");
        this.cTable = sqlQ.cQueryResults;
    }

The above code works fine and the datagridview is populated, however the column headers are the field names from the SQL database which aren't very user friendly.

I've tried a couple of things to try and change the default column names, but nothing isworking. So far I've tried -

dataToDoList.Columns[0].Name = "TEST1";

dataToDoList.Columns["DiaryCompletedDate"].Name = "TEST2";

But neither do anything.

Can anyone tell me how to change column header names in a datagridview please?

like image 517
PJW Avatar asked Jan 18 '23 18:01

PJW


2 Answers

Try DataGridViewColumn.HeaderText & AutoGenerateColumns (MSDN)

Hope this works for you.

like image 74
Amar Palsapure Avatar answered Jan 20 '23 07:01

Amar Palsapure


What are the exact Columns you need..?

Replace the SELECT * with

 SELECT ColumnName AS 'TEST1', ColumnName2 AS 'TEST2' FROM ...etc
like image 37
MethodMan Avatar answered Jan 20 '23 06:01

MethodMan