473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 14420
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: beton3000 | last post by:
Hello! I'm building a GridView using code to add template fields, because there is a random number of columns in result set from database. I'm using a class with ITemplate interface for defining...
2
by: manukaleel | last post by:
VB.NET(ASP.NET 2.0)-WEB .NET PLATFORM GridView Page Size is not working Hi, I have set the pagesize of a gridview to 22(both in aspx file as well as in the .vb file. But it's showing only...
0
by: manukaleel | last post by:
VB.NET(ASP.NET 2.0)-WEB .NET PLATFORM GridView Page Size is not working Hi, I have set the pagesize of a gridview to 22(both in aspx file as well as in the .vb file. But it's showing only...
3
by: ASPnewb1 | last post by:
I am currently filling a dataTable then adding this table to a dataset, setting the dataset to the Gridview's datasource. If I set the Gridview to generate columns automatically it will fill the...
9
by: lilOlMe | last post by:
Hi there! I have generated a GridView that looks something like: SportName| CompanyNameX |CompanyNameY |CompanyNameZ Hockey.....| Shipping------------ |Accounting-------- |Shipping------------...
1
by: jimmysjams | last post by:
I'm modifying someone else's code and I have hit a snag. I don't want to rewrite what's already there but I might have to. I have a gridview, in the gridview is a set number of columns that...
0
by: Charming12 | last post by:
Hi , I need to pass Dynamic Size in the statement Is there anyway to pass a dynamic value in sizeconst or is there any other statement likethis which can take dynamic Size. Help will be...
3
by: Kurt Jakobsen | last post by:
Hi, I would like to have a dynamic gridview (number of columns will change depending on project) and I would like to be able to change the value in a gridview cell and that the database tables will...
0
by: edurazee | last post by:
I am able to create BoundFields and Footer-rows dynamically like this in my GridView: protected void Page_Load(object sender, EventArgs e) { CreateGridView(); ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.