Solved

ArgumentOutofRangeException making GridView cell hidden at runtime

Posted on 2008-10-19
7
1,092 Views
Last Modified: 2013-12-17
I have a SQL databound GridView that has a field in it that I want to make hidden at runtime, but be able to access its value throughout the program.  I understand that setting Visible to true at design time (using the builder) will not bind the field.  I looked around and was under the impression that the code I've included would do it.  If I set Cells to anything but 0, I get an "ArgumentOutOfRangeException".  Setting it to "0" will hide the "Select" cell.  The column I want to hide is #13.  What gives?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // hide the Order ID
       
            e.Row.Cells[13].Visible = false;
        }

Open in new window

0
Comment
Question by:jvalescu
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 22753309
Is it a primary key for the gridview values? If so, you can put it in the DataKeyNames property of the gridview, and here is how you can access it in the code:

 protected void grvAnswer_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Response.Redirect(_answerUrl + "?AnswerId=" + grvAnswer.DataKeys[e.NewEditIndex].Value.ToString());
    }

Open in new window

0
 

Author Comment

by:jvalescu
ID: 22753339
No, it isn't a PK for the grid.  Thanks for the quick response, BTW!
0
 

Author Comment

by:jvalescu
ID: 22753367
My main goal is to be able to retrieve the value of that hidden column for the currently selected datarow.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Accepted Solution

by:
motocross_geek earned 250 total points
ID: 22753402
The error your receiving is something I'm very familiar with. Your receiving this error because there is no column with this index. This is why it works with index of 0. The index works left to right when your columns are created.

To use a column and retrienve it value, you are correct needs to be set true runtime.

Then to set false on RowDataBound, try this. Set your HeaderText value on column to "Order ID"
Then you can do this to guarntee the index everytime:
e.Row.Cells[getColumnID("Order ID")].Visible = false;


code to method:
public int getColumnID(string columnName)
{
   
    foreach (DataControlField column in GridView1.Columns) {
       
        if (column.HeaderText == columnName) {
           
            int columnID = GridView1.Columns.IndexOf(column);
           
            return columnID;
           
        }
       
    }
   
}  
This will get you through your problem. I do recommend using the DataKey. It will also perform better.
 
0
 
LVL 11

Assisted Solution

by:Salim Fayad
Salim Fayad earned 250 total points
ID: 22753433
You can put it in a templatefield, and inside the templatefield, you put a label with visible = false
0
 
LVL 2

Expert Comment

by:motocross_geek
ID: 22753518
I've seen HiddenField control used in a template field as well wish automatically is hidden.
0
 

Author Comment

by:jvalescu
ID: 22753720
Motocross and Eagle, turning the template field to visible=false keeps it off the grid, but there is no data being bound to the field.  When I run the program using your GetColumnID method (which works fine, BTW), I've tried doing this : lblOrderNumber.Text = GridView1.SelectedRow.Cells[getColumnID("OrderID")].Text;  and looking in the debugger, there is no data.  Am I doing something wrong?

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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: …

728 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