Solved

Gridview column width.

Posted on 2009-05-07
30
524 Views
Last Modified: 2013-12-17
Hi,
I am trying to increase the size of column in my gridview.

I have tried to use

 Referral_Gridview.Columns[1].ItemStyle.Width = Unit.Pixel(100);

but i get the following error:

Index was out of range. Must be non-negative and less than the size of the collection.

For reasons too long to explain I am not binding my data using asp, but rather C#

Can anybody help.
0
Comment
Question by:Mr_Shaw
  • 14
  • 11
  • 3
  • +2
30 Comments
 
LVL 4

Expert Comment

by:Andy1988
ID: 24326952
Use (i) in place of [1]
0
 

Author Comment

by:Mr_Shaw
ID: 24326998
I only want to effect the width of column 1
0
 
LVL 4

Expert Comment

by:Andy1988
ID: 24327105
try this gridProducts.Columns[1].HeaderStyle.Width = 100;
0
 

Author Comment

by:Mr_Shaw
ID: 24327166
Nope..same error
0
 
LVL 4

Assisted Solution

by:Andy1988
Andy1988 earned 100 total points
ID: 24327382
You can try with putting BreakPoint on that statement and open the
Immediate Windows and type e.Item.ItemIndex in that and hit the ENTER. You will get the current value for the INdex and see whether it is less than 0 or greater than the total number of rows in the grid.

To open Immediate Windows Menu - Debud>Windows>Immediate


0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24327391
See whether your getting data to gridview?
0
 
LVL 6

Assisted Solution

by:HarryNS
HarryNS earned 25 total points
ID: 24327641
In which event you trying to increase the width of the column???

Check this,

If (Referral_Gridview.Columns != null && Referral_Gridview.Columns.Count >0)
{
Referral_Gridview.Columns[1].ItemStyle.Width = Unit.Pixel(100);
}

If you replace this line Referral_Gridview.Columns[1].ItemStyle.Width = Unit.Pixel(100);  to above if condition and if NO error throws up then it is problem in you binding or event you are increasing the width.

In this case just move this line after Referral_Gridview.DataBind(); statement
0
 

Assisted Solution

by:dev_osama
dev_osama earned 25 total points
ID: 24328499
you should check first if there is a column with this index or not, as in the comment of HarryNS, after that you can use this line of code to adjust column width :

dataGridView1.Columns[1].Width = 100;;
0
 

Author Comment

by:Mr_Shaw
ID: 24334774
How do I check if there is an index in the column.

0
 

Author Comment

by:Mr_Shaw
ID: 24334775
I am not sure what the error means

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
0
 
LVL 11

Accepted Solution

by:
srikanthreddyn143 earned 350 total points
ID: 24335670
Where ever you are binding data to gridview, check the data.

Or debug the code at the place you are assingng width and look whether you have column or not from watch window or immediate window


That error means that you dont column with index 1
0
 

Author Comment

by:Mr_Shaw
ID: 24336784
I am not really sure how to use the watch window.

Here is a screen shot of my watch window. Is it possible for you to mark on it where I have to look.
watch-window-screen-shot.bmp
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24336913
Look at the Columns. Are there any Columns in the gridview?
0
 

Author Comment

by:Mr_Shaw
ID: 24336969
yes. my gridview is populated.... i just need to expand one of the columns.
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24337015
Check whether the gridview has column collection by clicking '+' sign before Columns in watch window. If you then after binding keep the piece of code specified by Harry/dev
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Mr_Shaw
ID: 24337087
hmm not sure... here is a screen shot for you to have a look.

Shall I put the piece of code specified by Harry/dev in the page load or on RowDataBound
watch-window-screen-shot-collect.bmp
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24337122
Use Column index as 0 not 1
0
 

Author Comment

by:Mr_Shaw
ID: 24337177
Ok.. now error..

But how do i specify which column I want to change the width..
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24337211
You can specify the column name or column index. your snapshot shows only one column. So index will be 0

dataGridView1.Columns[0].Width = 100;

or

If (Referral_Gridview.Columns != null && Referral_Gridview.Columns.Count >0)
{
Referral_Gridview.Columns[0].ItemStyle.Width = Unit.Pixel(100);
}

as Harry/Dev said
0
 

Author Comment

by:Mr_Shaw
ID: 24337276
I have ten columns.

I am binding my data dynamically

example
gridview1.datasource = dataset;
gridveiw.databind();
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24337293
Specify Columnm name instead of index

If (Referral_Gridview.Columns != null && Referral_Gridview.Columns.Count >0)
{
Referral_Gridview.Columns["Col1"].ItemStyle.Width = Unit.Pixel(100);
}
0
 

Author Comment

by:Mr_Shaw
ID: 24337419
it does not like me putting a string in place of an int
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24337475
can you post your code?
0
 

Author Comment

by:Mr_Shaw
ID: 24337543
Referral_Gridview.Columns["Providers"].ItemStyle.Width = Unit.Pixel(100);

The Columns overload is an int not a string.
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24337596
Can you post the part of code where you are binding data assigning col width
0
 

Author Comment

by:Mr_Shaw
ID: 24337641
       Referral_Gridview.DataSource = DataSet1;
        Referral_Gridview.DataBind();

        Referral_Gridview.Columns["Providers"].ItemStyle.Width = Unit.Pixel(100);
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24338377
-->  Referral_Gridview.DataSource = DataSet1;
        Referral_Gridview.DataBind();

        Referral_Gridview.Columns["Providers"].ItemStyle.Width = Unit.Pixel(100);

At the arrowed point pls keep breakpoint and check the data in dataset
0
 

Author Comment

by:Mr_Shaw
ID: 24352542
the syntax which you are suggesting will not work because Column[ ] requires a int overload not a string.
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24354249
You can have index. thats fine. Look at the dataset and check whether do u have any data or not?
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31579041
the code which worked was

 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[4].Width = 500;
        }
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now