473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

DataGridView Column Headers

How can I set a bound DataGridView control to use a dataset table's column
captions, instead of column names?

I'm working with a DataGridView control, which I have bound to a table in a
dataset. The dataset is created at run time, so I don't know column headers
in advance. The table's column captions are set at run time, using the
DataColumn.Caption property. The table is bound to the grid using the grid's
DataSource property. Here is my problem: The grid displays column names,
rather than captions, as it's header text.

Right now, I'm inserting the captions using a loop:

// Initialize Investments grid
gridInvestments.DataSource = myDataset.Tables["Investments"];
for (int i = 0; i < gridInvestments.ColumnCount; i++)
{
gridInvestments.Columns[i].HeaderText =
myDataset.Tables["Investments"].Columns[i].Caption;
}

I'm looking for a simpler way to get the job done.

Is there a DataGridView property that will tell the grid to get column
headers from the DataColumns' Caption property, rather than the Name
property? Thanks in advance

--
David Veeneman
Foresight Systems.
Nov 17 '05 #1
2 9642
David,

That's pretty much the way I would do it. Unfortunately, the binding
mechanism is pretty generic (using reflection and implemented interfaces to
get this information). This mechanism, however, does not have any way for
the data source to expose the column header information (after all, you can
bind to anything that implements IList).

What you have is fine.

If anything, you could derive a class from DataGridView which will do
this for you.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David Veeneman" <da****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
How can I set a bound DataGridView control to use a dataset table's column
captions, instead of column names?

I'm working with a DataGridView control, which I have bound to a table in
a dataset. The dataset is created at run time, so I don't know column
headers in advance. The table's column captions are set at run time, using
the DataColumn.Caption property. The table is bound to the grid using the
grid's DataSource property. Here is my problem: The grid displays column
names, rather than captions, as it's header text.

Right now, I'm inserting the captions using a loop:

// Initialize Investments grid
gridInvestments.DataSource = myDataset.Tables["Investments"];
for (int i = 0; i < gridInvestments.ColumnCount; i++)
{
gridInvestments.Columns[i].HeaderText =
myDataset.Tables["Investments"].Columns[i].Caption;
}

I'm looking for a simpler way to get the job done.

Is there a DataGridView property that will tell the grid to get column
headers from the DataColumns' Caption property, rather than the Name
property? Thanks in advance

--
David Veeneman
Foresight Systems.

Nov 17 '05 #2
Thanks--and thanks for the prompt response!
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob | last post by:
Thinking two things, 1- Creating a userControl -yeah you guessed it, a multi column drop down combobox - I've looked at several articles and did not find what I need, one that's bindable and that...
1
by: mark carew | last post by:
Hi, Problem - An extra column to the left (even with row headers disabled) ---------------------------------------------------------- Apologies if this posting is already in the newsgroup; but...
3
by: Mike | last post by:
Dear group, I'm currrently investigating a bug within a piece of our software whereby if a DataGridView (bound directly to a DataTable) is ordered by column headers (containing lookup combo...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
3
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I'm creating a control derived from DataGridView. I'd like the user to click a row and have the row highlight in yellow. I'd then like the user to click a column and have...
1
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
2
by: Steve | last post by:
Hi All I am using VB.net 2005 windows forms I use the Datagridview.cellformatting event to change the colours of individual cells as required and all works fine How can I do the same for...
0
by: theamesy | last post by:
I have a datagridview on a windows form, AllowUserToResizeColumns is set to false, it has 3 columns, as you'd expect i cant resize the columns, but i CAN resize the last column? I have column...
1
by: =?Utf-8?B?QWxleCBLLg==?= | last post by:
Hi all No matter how hard I try, I cannot force my DataGridView to use specific font in column headers: it seems to be always the same as container's font (which is form in my case). I set...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.