DataGridView - strange

C

ColoradoGeiger

I have a datagrid view that I have made in the designer. When
compiled and data is loaded into it, it looks great.

I have this on page 1 of a tab control. Pages 2 through N are created
on the fly in a loop, the code for that loop is below, as with the
datagrids on their pages. The code below tries to copy all properties
from the grid that looks good (dgridMaster) to the one that I am
creating on the fly... but it doesn't work. I am missing something?

-----------------------------------------------------------------------------
TabPage tab = new TabPage();
DataGridView dgv = new DataGridView();

dgv.DataSource = MyMagicDLL.GetTableData();
dgv.Name = dtCurrent.ToShortDateString();
dgv.Location = dgridMaster.Location;
dgv.Size = dgridMaster.Size;
dgv.SelectionMode = dgridMaster.SelectionMode;
dgv.ReadOnly = dgridMaster.ReadOnly;
dgv.DefaultCellStyle = dgridMaster.DefaultCellStyle;
dgv.AlternatingRowsDefaultCellStyle =
dgridMaster.AlternatingRowsDefaultCellStyle;
dgv.ColumnHeadersDefaultCellStyle =
dgridMaster.ColumnHeadersDefaultCellStyle;
dgv.RowHeadersDefaultCellStyle =
dgridMaster.RowHeadersDefaultCellStyle;
dgv.AutoSizeColumnsMode = dgridMaster.AutoSizeColumnsMode;
dgv.ScrollBars = dgridMaster.ScrollBars;

for (i = 0; i <= (dgv.Columns.Count - 1); i++)
{
dgv.Columns.SortMode = DataGridViewColumnSortMode.NotSortable;
}

tab.Controls.Add(dgv);
tab.Name = tab.Text = "My table"
TabControl.TabPages.Add(tab);
-----------------------------------------------------------------------------

The differences i notice:
1. The grid created on the fly has sortable columns. And because of
this it takes up more space in the X axis since beside each Header
cell there is space to the right for the up or down arrow.

2. The size looks good until i maximize the window. The original grid
adjust because it's anchor properties are set. But I lose my scroll
bars on my new grid when I add the line

dgv.Anchor = dgridMaster.Anchor;

Any ideas?

Jason
 
M

Marc Gravell

When you run it in debug, do any columns exist at the point you
enumerate them? Only: you haven't added it to the parent form yet, so
there is no currency-manager, hence no metadata provider at this
point. I would expect to manipulate the columns only *after* it is on
a control. The simplest approach may be in the ColumnAdded event.

As for the others; check what other properties you have edited - the
ones in bold in the designer. See if you missed any... Dock for
example. This is one of those occasions when it would be tempting not
to use the IDE but to use the same block of code to gerenate all grids
(even the one on the first page) - at least they should look the
same ;-p

Marc
 
C

ColoradoGeiger

I had gone through my settings in the designer but I will double check
my work. And I will look into the first idea too. Thanks a bunch!

Jason
 

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