0

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.

Community
  • 1
  • 1
  • how are the datacolumn.captions set by or loaded from access? – Ňɏssa Pøngjǣrdenlarp May 23 '15 at 21:37
  • As I said, I've set an OleDbConnection through a connection string which links to an .accdb file. I did this in a separated class (cnn). In that .accdb file I have set the captions for each column so I could use it in t.Columns[i].Caption. – Vitor Assis May 25 '15 at 00:08

0 Answers0