DataGrid (or ListBox) "Viewport" Size

  • Thread starter Thread starter Kristin
  • Start date Start date
K

Kristin

How do I find out the size (specifically width) of a listbox's (specifically datagrid) "viewport" where the "viewport" does not contain borders (Fixed3D and otherwise), scrollbars, etc. ?

Microsoft defines a "viewport" in help text for the "DataGrid.VisibleColumnCount" property as so: "The viewport is the rectangular area through which the grid is visible." Well that's exactly what I need but where do I get it and it's attributes?

I'm asking because I'm writing a method which fills the width of a DataGrid with the columns defined in the active TableStyle. The problem is that I can't get the width of the datagrid which does not include the border, scrollbars, etc.

I've tried the following and they all refer to the width of the ClientRectangle which inclues scrollbars, borders, etc.

dataGrid.Width
dataGrid.ClientRectangle.Width
dataGrid.Bounds.Width
dataGrid.DisplayRectangle.Width
dataGrid.Size.Width

If I allocate column widths such that the total of all column widths is equal to, say, dataGrid.Width, I still get a horizontal scrollbar. The difference between dataGrid.Width and this total of all column widths is the width of say a 3D border, vertical scrollbar, etc.

Any suggestions?

Kristin

Kristin Barker
Principal Software Engineer
Email: (e-mail address removed)
http://www.natureserve.org
 
Have you checked out the Control.ClientRectangle property?

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


How do I find out the size (specifically width) of a listbox's (specifically datagrid) "viewport" where the "viewport" does not contain borders (Fixed3D and otherwise), scrollbars, etc. ?

Microsoft defines a "viewport" in help text for the "DataGrid.VisibleColumnCount" property as so: "The viewport is the rectangular area through which the grid is visible." Well that's exactly what I need but where do I get it and it's attributes?

I'm asking because I'm writing a method which fills the width of a DataGrid with the columns defined in the active TableStyle. The problem is that I can't get the width of the datagrid which does not include the border, scrollbars, etc.

I've tried the following and they all refer to the width of the ClientRectangle which inclues scrollbars, borders, etc.

dataGrid.Width
dataGrid.ClientRectangle.Width
dataGrid.Bounds.Width
dataGrid.DisplayRectangle.Width
dataGrid.Size.Width

If I allocate column widths such that the total of all column widths is equal to, say, dataGrid.Width, I still get a horizontal scrollbar. The difference between dataGrid.Width and this total of all column widths is the width of say a 3D border, vertical scrollbar, etc.

Any suggestions?

Kristin

Kristin Barker
Principal Software Engineer
Email: (e-mail address removed)
http://www.natureserve.org
 
Yes. Control.ClientRectangle is inherited by DataGrid as the DataGrid.ClientRectangle and includes borders, scrollbars, etc.

BTW, I reread my original posting and should clarify that the sum of all column widths does in fact equal the DataGrid.Width (and all of the other names by which this attribute goes). The problem is that this width includes the items outside the "viewport" and thus my columns are too wide, resulting in the horizontal scroll bar on the DataGrid.

Thanks for any help.

Kristin
Have you checked out the Control.ClientRectangle property?

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


How do I find out the size (specifically width) of a listbox's (specifically datagrid) "viewport" where the "viewport" does not contain borders (Fixed3D and otherwise), scrollbars, etc. ?

Microsoft defines a "viewport" in help text for the "DataGrid.VisibleColumnCount" property as so: "The viewport is the rectangular area through which the grid is visible." Well that's exactly what I need but where do I get it and it's attributes?

I'm asking because I'm writing a method which fills the width of a DataGrid with the columns defined in the active TableStyle. The problem is that I can't get the width of the datagrid which does not include the border, scrollbars, etc.

I've tried the following and they all refer to the width of the ClientRectangle which inclues scrollbars, borders, etc.

dataGrid.Width
dataGrid.ClientRectangle.Width
dataGrid.Bounds.Width
dataGrid.DisplayRectangle.Width
dataGrid.Size.Width

If I allocate column widths such that the total of all column widths is equal to, say, dataGrid.Width, I still get a horizontal scrollbar. The difference between dataGrid.Width and this total of all column widths is the width of say a 3D border, vertical scrollbar, etc.

Any suggestions?

Kristin

Kristin Barker
Principal Software Engineer
Email: (e-mail address removed)
http://www.natureserve.org
 
From the remarks for ClientRectangle:

The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


Yes. Control.ClientRectangle is inherited by DataGrid as the DataGrid.ClientRectangle and includes borders, scrollbars, etc.

BTW, I reread my original posting and should clarify that the sum of all column widths does in fact equal the DataGrid.Width (and all of the other names by which this attribute goes). The problem is that this width includes the items outside the "viewport" and thus my columns are too wide, resulting in the horizontal scroll bar on the DataGrid.

Thanks for any help.

Kristin
Have you checked out the Control.ClientRectangle property?

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


How do I find out the size (specifically width) of a listbox's (specifically datagrid) "viewport" where the "viewport" does not contain borders (Fixed3D and otherwise), scrollbars, etc. ?

Microsoft defines a "viewport" in help text for the "DataGrid.VisibleColumnCount" property as so: "The viewport is the rectangular area through which the grid is visible." Well that's exactly what I need but where do I get it and it's attributes?

I'm asking because I'm writing a method which fills the width of a DataGrid with the columns defined in the active TableStyle. The problem is that I can't get the width of the datagrid which does not include the border, scrollbars, etc.

I've tried the following and they all refer to the width of the ClientRectangle which inclues scrollbars, borders, etc.

dataGrid.Width
dataGrid.ClientRectangle.Width
dataGrid.Bounds.Width
dataGrid.DisplayRectangle.Width
dataGrid.Size.Width

If I allocate column widths such that the total of all column widths is equal to, say, dataGrid.Width, I still get a horizontal scrollbar. The difference between dataGrid.Width and this total of all column widths is the width of say a 3D border, vertical scrollbar, etc.

Any suggestions?

Kristin

Kristin Barker
Principal Software Engineer
Email: (e-mail address removed)
http://www.natureserve.org
 
Yes, that's the definition in the documentation, but consider the following
class containing a method to fill the datagrid with currently visible
columns. It works, but only by starting with ClientRectangle.Width (or it's
equivalent) and *subtracting* the width of the scrollbar and border.

I would love to remove the calculation of ViewportWidth if I just knew what
to use in it's place.

===========================

public class BetterDataGrid : DataGrid
{
....

public void FillWidthFromColumns (DataGridTableStyle tableStyle)
{
int totalColumnWidth = 0;
foreach (DataGridColumnStyle col in tableStyle.GridColumnStyles)
{
totalColumnWidth += col.Width;
}
// Determine the difference between the datagrid's viewport width
and the total column width
int toBeAllocated = this.ViewPortWidth - totalColumnWidth;
// this.ViewPortWidth is defined below

// Allocate that width over the currently visible columns
int count = tableStyle.GridColumnStyles.Count;
int allocation = toBeAllocated / count;
int leftover = toBeAllocated % count;
DataGridColumnStyle lastVisibleCol = null;

BetterDataGridTextBoxColumn btrCol;
// BetterDataGridTextBoxColumn supports attribute Visible (among
other improvemnts)

foreach (DataGridColumnStyle col in tableStyle.GridColumnStyles)
{
btrCol = col as BetterDataGridTextBoxColumn;
if (btrCol == null || btrCol.Visible)
{
col.Width += allocation;
lastVisibleCol = col;
}
}
if (lastVisibleCol != null)
lastVisibleCol.Width += leftover;
}

public int ViewPortWidth
{
get
{
// This isn't readily available so we'll calculate it from the
// dataGrid width and other control features.
int viewPortWidth = this.Width;
// this.Width is equalivaent to this.ClientRectangle.Width

if (this.RowHeadersVisible && this.TableStyles[0].RowHeadersVisible)
viewPortWidth -= this.RowHeaderWidth;
switch (this.BorderStyle)
{
case BorderStyle.Fixed3D:
viewPortWidth -= 4;
break;
case BorderStyle.FixedSingle:
viewPortWidth -= 4;
break;
case BorderStyle.None:
break;
}
if (this.VertScrollBar.Visible)
viewPortWidth -= this.VertScrollBar.Width;
return viewPortWidth;
}
}
}

===============================

Any thoughts?

Kristin


From the remarks for ClientRectangle:

The client area of a control is the bounds of the control, minus the
nonclient elements such as scroll bars, borders, title bars, and menus.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


Yes. Control.ClientRectangle is inherited by DataGrid as the
DataGrid.ClientRectangle and includes borders, scrollbars, etc.

BTW, I reread my original posting and should clarify that the sum of all
column widths does in fact equal the DataGrid.Width (and all of the other
names by which this attribute goes). The problem is that this width
includes the items outside the "viewport" and thus my columns are too wide,
resulting in the horizontal scroll bar on the DataGrid.

Thanks for any help.

Kristin
Have you checked out the Control.ClientRectangle property?

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


How do I find out the size (specifically width) of a listbox's (specifically
datagrid) "viewport" where the "viewport" does not contain borders (Fixed3D
and otherwise), scrollbars, etc. ?

Microsoft defines a "viewport" in help text for the
"DataGrid.VisibleColumnCount" property as so: "The viewport is the
rectangular area through which the grid is visible." Well that's exactly
what I need but where do I get it and it's attributes?

I'm asking because I'm writing a method which fills the width of a DataGrid
with the columns defined in the active TableStyle. The problem is that I
can't get the width of the datagrid which does not include the border,
scrollbars, etc.

I've tried the following and they all refer to the width of the
ClientRectangle which inclues scrollbars, borders, etc.

dataGrid.Width
dataGrid.ClientRectangle.Width
dataGrid.Bounds.Width
dataGrid.DisplayRectangle.Width
dataGrid.Size.Width

If I allocate column widths such that the total of all column widths is
equal to, say, dataGrid.Width, I still get a horizontal scrollbar. The
difference between dataGrid.Width and this total of all column widths is the
width of say a 3D border, vertical scrollbar, etc.

Any suggestions?

Kristin

Kristin Barker
Principal Software Engineer
Email: (e-mail address removed)
http://www.natureserve.org
 
Back
Top