472,096 Members | 1,193 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Gridview and dynamic size columns with a dataset

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!
Aug 7 '06 #1
3 14351
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 (wa****@online.microsoft.com, 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.

Aug 8 '06 #2
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 (wa****@online.microsoft.com, 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.

Aug 8 '06 #3
Hello Walter,

GridView1_RowCreated() is exactly what I needed!
Thanks!

Aug 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by jimmysjams | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.