Datagrid width on a Web Form

I

Iain

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
.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
 
A

Alberto Poblacion

Iain said:
I have a datagrid in a web form and I want to be able to determine the
width of the datagrid at runtime.
[...] dgWidth = dgWidth + dgProjects.Columns
.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.


You are trying to add the widths of the columns in code that is running
on the SERVER side. However, the widths at runtime are established on the
CLIENT. In other words, the server sends some HTML into the browser and then
the browser decides dynamically how to paint the grid. It does NOT send any
information back to the server about how the grid has been actually painted.
Therefore, there is no way to accurately determine the actual widths of the
columns on the server side, because this information is never sent to the
server.
The remedy is to determine the widths that you wish to know on the
client side, where that information is available. This has to be done by
means of code that runs in the browser, which will generally mean that you
have to write it in Javascript rather than C#. For the details of how to
write such code, it's best to ask in a newsgroup related to javascript; the
C# group is probably not the best place to seek such expertise.
 

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