Gridview and dynamic size columns with a dataset

G

Guest

Greetings,

I have a gridview which is sitting on a multi view control (witin a view),
and it is bound to a dataset which I dynamically apply to the control within
asp.net page. The problem is that I can't seem to resize my dataview's
columns which are auto generated. Is there a way to do this? I have tried
several different things, but I can't seem to get at the column collection to
change the widths?

MY development environment
- System XP Pro, using Visual Studio 2005.
- A content page which hosts a multiview control with a single view in the
control.
- The single view hosts a view with my gridview on it which is dynamically
linked to
the datagrid control.
- Dataview control is bound to a dataset which has been dynamically built.

// A dataset is built, and the gridview is set to auto create the columns.
DataSet data = CreateDataSetForUse();
GridView1.DataSource = data;
GridView1.DataMember = "MyDataSet";
GridView1.DataBind();

The data is correct, and the columns are appearing correctly, BUT
I can't seem to resize the columns.

I have tried overriding the RowDataBound call like the sample in msdn shows,
but I am not getting access to the column collection.

(ie:)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
System.Data.DataRowView drv = (System.Data.DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv != null)
{

/*
widestData = catNameLen;
GridView1.Columns[2].ItemStyle.Width =
widestData * 30;
GridView1.Columns[2].ItemStyle.Wrap = false;
*/

// Always returns 9, for my bound columns
int count = drv.Row.Table.Columns.Count;

// Never returns anything but 0?
count = GridView1.Columns.Count; }
}

Any ideas on how I can resize the columns with a databound gridview?

Thanks in advance for any suggestions!
 
W

Walter Wang [MSFT]

Hi,

You can set the GridView's columns width in its RowCreated event as
following example:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Width = Unit.Pixel(500);
}
}

In generated html source, you can verify that the column is set to:

<th scope="col" style="width:500px;">

Hope this helps. Please feel free to post here if anything is unclear.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi,

Another note is that you need to set the GridView's Width explicitly too,
otherwise, when the container of the generated html Table is resized, the
column width will not be fixed.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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