473,408 Members | 1,699 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,408 software developers and data experts.

Column Width in a Gridview

How can I change the column width in a gridview filled with a dataset? I
can't get TableStyle to work. Has anyone any sample code, please?

Thanks,
Helen

PS. I am creating a web page in ASP.Net 2.0 using C#.

This is my code so far:

SqlConnection cn; // Connection to the SQL database
SqlDataAdapter da;
DataSet ds;

cn = new SqlConnection();
da=new SqlDataAdapter("Select * From Patient",cn);
ds = new DataSet();

OpenConnection(cn); // Connects to the database
da.Fill(ds,"PatientTable");
dgPatients.DataSource = ds.Tables["PatientTable"];
dgPatients.DataBind();
cn.Close();

Feb 13 '07 #1
1 3699

Helen Trim wrote:
How can I change the column width in a gridview filled with a dataset? I
can't get TableStyle to work. Has anyone any sample code, please?

Thanks,
Helen

PS. I am creating a web page in ASP.Net 2.0 using C#.

This is my code so far:

SqlConnection cn; // Connection to the SQL database
SqlDataAdapter da;
DataSet ds;

cn = new SqlConnection();
da=new SqlDataAdapter("Select * From Patient",cn);
ds = new DataSet();

OpenConnection(cn); // Connects to the database
da.Fill(ds,"PatientTable");
dgPatients.DataSource = ds.Tables["PatientTable"];
dgPatients.DataBind();
Hi,
Add event handler for the RowCreated event:

.... OnRowCreated="GridView1_RowCreated" ...

protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
DataControlFieldCell cell =
(DataControlFieldCell)e.Row.Cells[i];
//here add custom logic of column width settings, for example:
//if (cell.ContainingField.HeaderText == "Id")
// e.Row.Cells[i].Width = new Unit("100px");
}
}
}

cn.Close();
Feb 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Bugden | last post by:
Hi, I would like to set the column widths in the ASP.NET GridView control at runtime I have tried the following: http://msdn2.microsoft.com/en-us/library/ms178296.aspx Which uses the...
1
by: msch-prv | last post by:
I have difficulties adjusting the column width of a gridview control. The column width changes appear in the design grid, but at run-time the width changes back to what I presume are the grid's...
4
by: Chuck | last post by:
I'm setting the column with for a gridview (25+- columns) and have paging turned on. When the gridview is first displayed, the column widths are all set to the default. But after paging to...
0
by: landesjoe | last post by:
Hi, here's my problem in short: Text boxes in gridview don't seem to hold their value if the column's .Visible property is changed back and forth. I've got a form with a gridview populated from...
6
by: =?Utf-8?B?TWlrZSBQYXJyaXM=?= | last post by:
I am trying to set the width of one of the columns of an asp.net 2.0 Gridview control. I am using a line - MyGridview.Columns.ItemStyle.Width = Unit.Percentage(40); The problem is that there...
0
by: GMartin | last post by:
I have a pop-up form with a three columned Grid that has checkboxes in a Template Column in the first/left-most column. (The form is to allow users to select "Members" of a group, where they check...
3
by: =?Utf-8?B?SmVu?= | last post by:
I would like to set the width on 2 columns of a asp.net gridview control, but I'm having trouble making this work. Here is the code I use to create the gridview: 'Create a new data table...
5
by: mohaaron | last post by:
OK, I have now gone through lots of posts, and there are lots, looking for a anwer as to why I can't set the width of the columns in my gridview. So far I have tried the following methods to set...
4
by: Yin99 | last post by:
I have a Gridview binding to a DataTable source. I'd like to set the column with of the second column. I cannot do this apparently because when AutoGenerateColumns=true, they do not appear in the...
0
by: progman417 | last post by:
I have a gridview with a dropdown list in an EditItemTemplate. I haven't set a width for the columns, so that they dynamically size according to the data displayed. When it goes into edit...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.