From: Iain on
Hi all

I have a datagrid in a web form and I want to be able to determine the
width of the datagrid at runtime.

I have tried both of the following

// Get the width using the datagrid width attribute.
double datagridWidth = 0;
datagridWidth = dgProjects.Width.Value;

and

// Add the widths of all columns
double dgWidth = 0;

for (int i = 0; i <= dgProjects.Columns.Count - 1; i++)
{
dgWidth = dgWidth + dgProjects.Columns
[i].ItemStyle.Width.Value;
}


Neither of these returns the width. All values are 0.0.

Is there any kind soul out there who knows how to do this.

Many thanks in advance for any assistance offered

Iain
From: Abdul Sami on
Try .Value.ToString()

--
Abdul Sami


"Iain" wrote:

> Hi all
>
> I have a datagrid in a web form and I want to be able to determine the
> width of the datagrid at runtime.
>
> I have tried both of the following
>
> // Get the width using the datagrid width attribute.
> double datagridWidth = 0;
> datagridWidth = dgProjects.Width.Value;
>
> and
>
> // Add the widths of all columns
> double dgWidth = 0;
>
> for (int i = 0; i <= dgProjects.Columns.Count - 1; i++)
> {
> dgWidth = dgWidth + dgProjects.Columns
> [i].ItemStyle.Width.Value;
> }
>
>
> Neither of these returns the width. All values are 0.0.
>
> Is there any kind soul out there who knows how to do this.
>
> Many thanks in advance for any assistance offered
>
> Iain
> .
>
From: Mark Rae [MVP] on
"Iain" <email.iainwilson(a)gmail.com> wrote in message
news:d7d44f69-a59e-44a2-aa8a-6f3d5aaae54a(a)m16g2000yqc.googlegroups.com...

> I have a DataGrid ...

Firstly, the DataGrid is obsolete these days. It was superceded by the
GridView back in ASP.NET v2.0:
http://www.google.co.uk/search?rlz=1C1CHMA_en-GBGB328GB328&sourceid=chrome&ie=UTF-8&q=DataGrid+GridView
It is retained only to support legacy systems written in ASP.NET v1.x - you
should seriously consider upgrading...


> in a web form and I want to be able to determine the width of the datagrid
> at runtime.

This is notoriously difficult, mainly because server-side code has no
knowledge of how the HTML it streams to the browser will actually be
rendered by that browser - the best it can hope for is to send well-formed
and standards-compliant markup and hope that the browser "plays ball"...

If this is even possible to determine server-side, you'll need to do it in
the Page_PreRender method:
http://www.google.co.uk/search?hl=en&rlz=1C1CHMA_en-GBGB328GB328&q=Page_Prerender&btnG=Search&meta=&aq=f&oq=

Even then, though, you may not actually get a value back...

However, GridView controls (and DataGrid controls) are rendered as standard
HTML <table /> controls, so you could (probably) determine the width using
client-side JavaScript. Problem here, of course, is that by the time
client-side JavaScript runs, the server has completely finished with the
page...

It might be beneficial if you explain what you are trying to do...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

From: Iain on
Thanks Abdul, but that just converts a zero value to a string.

Mark. I will certainly look at the GridView control and see if I can
get any better results there.

Watch this space.

Thanks guys.
From: Mark Rae [MVP] on
"Iain" <email.iainwilson(a)gmail.com> wrote in message
news:48442b92-2f5d-4ac9-9075-46531a0d0f84(a)j24g2000yqa.googlegroups.com...

> Mark. I will certainly look at the GridView control and see if I can
> get any better results there.

The GridView won't give you any better results than the DataGrid in this
particular case because, as mentioned, you almost certainly won't be able to
do this server-side so, at that level, it won't matter one iota which
webcontrol you use...

My suggestion to upgrade the DataGrid to the GridView was simply to
encourage you to move away from obsolete technology...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net