rename a columns title in a dataGridView
Hi,
I'm filling a dataGrid with an Access database. But of course I want to change tittles of the fields. It seems only fields of type "string" change name. All other columns are staing the same name as originally in the database. How is this possible ? Someone know ? Do I use wrong code ? To be sure I started a new project and a fresh dataGridView to be sure I did not touch any properties.
The name "New" appears only on the columns that have a fieldtype: 'string'.
Code:
partial class Form1: Form
{
OleDbConnection DBConnection;
private void Form1_Load(object sender, EventArgs e)
{
string DataSource = "C:\\1.mdb";
DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + DataSource);
DBConnection.Open();
try {
OleDbDataAdapter Adapter = new OleDbDataAdapter("SELECT * " +
"FROM Rx ",
DBConnection);
DataSet ds = new DataSet();
Adapter.Fill(ds);
dataGrid.DataSource = ds;
ds.Tables[0].TableName = "Rx";
dataGrid.DataMember = "Rx";
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
dataGrid.Columns[i].HeaderText = "New";
} finally {
DBConnection.Close();
Re: rename a columns title in a dataGridView
There isn't a Columns collection accessible from the DataGrid (at least not in VS.NET 2003),
You access the layout of the Grid via the TableStyles and GridColumnStyles properties, i.e.
Code:
// Create an example DataSet
DataSet ds = new DataSet();
// Create a table with various column data types
DataTable tbl = new DataTable("Example");
tbl.Columns.Add("Field1", typeof(string));
tbl.Columns.Add("Field2", typeof(int));
tbl.Columns.Add("Field3", typeof(decimal));
tbl.Columns.Add("Field4", typeof(double));
tbl.Columns.Add("Field5", typeof(DateTime));
// Plugin some data
tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
// Add the table to the DataSet
ds.Tables.Add(tbl);
// Bind the DataGrid to the DataSet
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "Example";
// This is the section you're most interested in:
// Create a DataGridTableStyle for the table
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "Example";
// Add the style to the Grid auto-generating the Column Styles
dataGrid1.TableStyles.Add(ts);
// Enumerate the Column Styles changing the Header Text
for( int index=0; index<ts.GridColumnStyles.Count; index++ )
ts.GridColumnStyles[index].HeaderText = "Renamed" + index.ToString();
Regards,
- Aaron.
Re: rename a columns title in a dataGridView
Hi Aaron,
Many thanks for your (very complete reply). However it seems that .NET versions are not compatible with each other :( I use c# 2005 version..
It has the Columns property accessible but it only seems to work half. I try your code but next problem arrive:
DataGridView does not contain a definition for TableStyles
now what's next ? You have other idea ?
The strange part is still that all string fields work !!! Any explanation ?
Re: rename a columns title in a dataGridView
Hi,
It seems more complicated than I thought, but this code works. It is needed to first generate all columns in code, give the title to it and then connnect to the dataset.
Code:
dataGrid.AutoGenerateColumns = false;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++) {
DataGridViewColumn colNaam = new DataGridViewColumn();
colNaam.DataPropertyName = ds.Tables[0].Columns[i].ColumnName;
colNaam.HeaderText = i.ToString(); // the title
colNaam.CellTemplate = new DataGridViewTextBoxCell();
dataGrid.Columns.Add(colNaam);
}
dataGrid.DataSource = ds;
dataGrid.DataMember = "Rx";
Re: rename a columns title in a dataGridView
Hi,
Forgot something:
Code:
colNaam.Name = ds.Tables[0].Columns[i].ColumnName;