My issue is almost the same from this one: Why DataColumn.Caption doesn't work?
But there, he manually created his DataTable, while in my case I'm binding it to an Access database through an OleDbConnection with a connection string. In that database, I have set the captions for each column of the tables so that I could only do the following:
DataTable t = cnn.Consulta(sSQL); //returns a DataTable with a string SQL select query.
DataGridView dg = new DataGridView();
dg.DataSource = t;
int i = 0;
string caption;
foreach (DataGridViewColumn cln in dg.Columns)
{
caption = t.Columns[i].Caption;
cln.HeaderText = caption;
i++;
}
Note that my foreach
block is basically the same as the one given as solution in the post mentioned above, but as I have set the captions directly in the Access file, instead of doing it in the code, C# is keeping binding it as the column name, as if they were empty (???).
Now imagine if I had tons of tables, each of them with tons of columns with a non-friendly name? Well, it's true that I'd still have to set a ton of captions anyway, but it would be better than writing 300 lines of code to assign it to each DataColumn.
Unless there is another property where I can store a string to use as header text, I'm stuck with this.