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 / December 2004

Tip: Looking for answers? Try searching our database.

How to specify width of each of the columns of a DataGrid?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
~~~ .NET Ed ~~~ - 03 Dec 2004 10:24 GMT
I am feeding a class as the DataSource of the DataGrid. When the processing
is done this data grid then displays the data in four columns. Now, I know
you can specify table styles for a data grid, but what about being able to
specify the width of each and every one of the columns of the datagrid
rather than as a whole (same width for all columns which is easy)? I don't
want to have to resize the columns at runtime every time the application is
run.

Thx,
Emilio
Rakesh Rajan - 03 Dec 2004 10:39 GMT
Hi,

Use the DataGridColumnStyle.Width property.

HTH,
Rakesh Rajan

> I am feeding a class as the DataSource of the DataGrid. When the processing
> is done this data grid then displays the data in four columns. Now, I know
[quoted text clipped - 6 lines]
> Thx,
> Emilio
~~~ .NET Ed ~~~ - 03 Dec 2004 19:35 GMT
Thanks for the hint, unfortunately I have not gotten this to work yet. See
below.

The data grid object is given a DataSource (grid.DataSource =
someobject.FileList) which is an ArrayList of
objects with four members (FullName, Length, Extension, Name). Somehow no
matter what is put on the AddStyles there seems to be no coupling between
the two so that the grid is displayed with the correct proportions.

{
      :
     grid.DataSource = myObject.FileList;  // FileList is returns an
ArrayList, see above
     AddGridStyle(grid);
     :
}

private void AddGridStyle(DataGrid dg)

{

DataGridTableStyle ts = new DataGridTableStyle();

ts.MappingName = "FileData";

DataGridTextBoxColumn cs1 = new DataGridTextBoxColumn();

cs1.HeaderText = "FullName";

cs1.MappingName = "FullName";

cs1.Width = 170;

ts.GridColumnStyles.Add(cs1);

DataGridTextBoxColumn cs2 = new DataGridTextBoxColumn();

cs2.HeaderText = "Name";

cs2.MappingName = "Name";

cs2.Width = 80;

ts.GridColumnStyles.Add(cs2);

DataGridTextBoxColumn cs3 = new DataGridTextBoxColumn();

cs3.HeaderText = "Extension";

cs3.MappingName = "Extension";

cs3.Width = 20;

ts.GridColumnStyles.Add(cs3);

DataGridTextBoxColumn cs4 = new DataGridTextBoxColumn();

cs4.HeaderText = "Length";

cs4.MappingName = "Length";

cs4.Width = 20;

ts.GridColumnStyles.Add(cs4);

dg.TableStyles.Add(ts);

}

> Hi,
>
[quoted text clipped - 18 lines]
>> Thx,
>> Emilio
Rakesh Rajan - 04 Dec 2004 04:03 GMT
Hi,

Since you are using an ArrayList, you need to set the MappingName of the
DataGridTableStyle as "ArrayList" (case sensitive).

Also, you need to ensure that the objects within the arraylist have public
properties - it's the name of these public properties you will use as
MappingNames for the GridColumnStyle of the columns.

So, in your case, change the MappingName from "FileData" to "ArrayList".
The MappingNames for the GridColumnStyles you have used are "FullName",
"Name", "Length", and "Extension"; so ensure you have public properties with
the same name in the FileData type.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemwindowsformsdatagridtablestyleclassmappingnametopic.asp

for an example.

HTH,
Rakesh Rajan

> Thanks for the hint, unfortunately I have not gotten this to work yet. See
> below.
[quoted text clipped - 87 lines]
> >> Thx,
> >> Emilio
~~~ .NET Ed ~~~ - 04 Dec 2004 12:08 GMT
Thanks a lot, that did it. That was exactly the piece of information I was
looking for. Somehow I didn't get a hit on it on Visual Studio's
documentation or examples.

> Hi,
>
[quoted text clipped - 112 lines]
>> >> Thx,
>> >> Emilio
Rakesh Rajan - 04 Dec 2004 12:57 GMT
Hi,

Glad that it helped :)

Regards,
Rakesh Rajan

> Thanks a lot, that did it. That was exactly the piece of information I was
> looking for. Somehow I didn't get a hit on it on Visual Studio's
[quoted text clipped - 116 lines]
> >> >> Thx,
> >> >> Emilio

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.