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?
Try DataGridViewColumn.HeaderText & AutoGenerateColumns (MSDN)
Hope this works for you.
What are the exact Columns you need..?
Replace the SELECT * with
SELECT ColumnName AS 'TEST1', ColumnName2 AS 'TEST2' FROM ...etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With