Renaming Column headers using Adapter.fill

Ismael

Member
Joined
Mar 20, 2020
Messages
18
Programming Experience
1-3
Hi, folks. I'm retrieving data from a table with an adapter and using thesse data to populate a datagridview. It's working fine, but now I want to change the column header, but I don't know how. I found nothing here or in other foruns aboute this. Can any one help me?
The following code (simplified) is inside a routine that returns the table.
C#:
var con = new SqlConnection(ConfigurationManager.AppSettings["TextoConexao"]);
SqlCommand Cmd = new SqlCommand(TextoSLQ, con);
DataTable Tab1 = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter();

Cmd.CommandType = CommandType.Text;
ad.SelectCommand = Cmd;
ad.Fill(Tab1);

return Tab1;
The above routine mounts the Select command.

In the main program there is:
C#:
dataGridView1.DataSource = tb.LeTabelaComplexa(Campos, " from ITEM T1 ", "", " order by T1.ITEM_TX_NOME");
The parameters in the routine calling indicate the columns and the commands used.
As I said, it works fine, but I need to change the column headers in the datagridview. I've tried many alternatives, but none has worked. How can I do this?
Thanks.
 
Last edited by a moderator:
Every column has a HeaderText property that you can set. Alternatively, you can alias the column in your SQL code and the default header text will use that, e.g.
SQL:
SELECT SomeColumn AS Alias FROM SomeTable
You're already aliasing your table. If you use an alias though, you'll have to account for that in any INSERT and UPDATE statements.
 
You've got a couple of options:
1) You can take advantage of the AS keyword in your SQL query to change the column name as return by the query.
2) You can set the Name property of the columns.
 
Back
Top Bottom