Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

DataGrid - best way to configure column names?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David++ - 19 Jul 2006 15:03 GMT
Hi folks,

I am using the DataGrid control. I have linked it to a data source and am
currently reading in some data. However, the names of the columns are taking
on the names of the columns from the database i.e. job_number, job_title,
job_address etc... I would like to name the columns myself i.e. Job Number,
Job Title, Job Address etc...

How is this achievable??

Thanks for any help,

Best Regards,
David
Apoxy - 19 Jul 2006 19:03 GMT
Firstly, if you're using or would like to use .NET 2.0, I would highly
suggest you use the DataGridView control.  You'll save yourself a lot
of headaches if you stay far, far away from the DataGrid control.

If that's not an option, though, you'll have to create a new
DataGridTableStyle.  For every column, even the ones that you don't
want to rename the headers for, you must create a
DataGridTextBoxColumn, set up your desired style elements, and then add
that column to the DataGridTableStyle's GridColumnStyles collection.
Finally, you add that DataGridTableStyle you created to your DataGrid's
TableStyles collection.

Thusly:

private FormatGrid()
    //dg is our datagrid
    //colWidthOffset = 4

    Graphics g = Graphics.FromHwnd(dg.Handle);

    //customize columns
    DataGridTableStyle ts = new DataGridTableStyle();
    ts.MappingName = "DataBindingNameGoesHere";

    DataGridTextBoxColumn station = new DataGridTextBoxColumn();
    station.MappingName = DataLayer.eOrderDataCols.station.ToString();
    station.HeaderText = "Station";
    station.Alignment = HorizontalAlignment.Center;
    station.Width =
Convert.ToInt32(Math.Ceiling(g.MeasureString(station.HeaderText.ToString(),
dg.Font).Width) + colWidthOffset);
    station.ReadOnly = true;

    ts.GridColumnStyles.Add(station);

    dg.TableStyles.Add(ts);
}

This will show a grid with one column with "Station" in its header,
even though the name of the column in the bound object is "station".

If you want more columns, you've got to create more
DataGridTextBoxColumns and add them to the ts.GridColumnStyles
collection.  You don't absolutely have to set all those properties I
did, but they help.

---Andy

> Hi folks,
>
[quoted text clipped - 10 lines]
> Best Regards,
> David
David++ - 20 Jul 2006 16:52 GMT
Thanks Andy,

That helped a lot! Like you suspected I am unable to use the DataGridView
for my application (afaik)...it is for a mobile app. One potential limitation
I think the DataGrid may have is the lack tick box support for a column? i.e.
instead of displaying yes/no or true/false I would like a check box with a
tick, dont think this is possible with the data grid ;-(

Cheers,
David

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.