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

  • Thread starter ~~~ .NET Ed ~~~
  • Start date
N

~~~ .NET Ed ~~~

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
 
N

~~~ .NET Ed ~~~

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);

}
 
G

Guest

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/d...msdatagridtablestyleclassmappingnametopic.asp
for an example.

HTH,
Rakesh Rajan
 
N

~~~ .NET Ed ~~~

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top