473,372 Members | 1,038 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,372 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 14422
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(); ...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.