Solved

Gridview column width.

Posted on 2009-05-07
30
534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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