Solved

ArgumentOutofRangeException making GridView cell hidden at runtime

Posted on 2008-10-19
7
1,043 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Salim Fayad
Comment Utility
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
Comment Utility
No, it isn't a PK for the grid.  Thanks for the quick response, BTW!
0
 

Author Comment

by:jvalescu
Comment Utility
My main goal is to be able to retrieve the value of that hidden column for the currently selected datarow.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Accepted Solution

by:
motocross_geek earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I've seen HiddenField control used in a template field as well wish automatically is hidden.
0
 

Author Comment

by:jvalescu
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 35
Total in textboxes 9 33
File Upload Control on a ASP.NET Overlay Page 1 17
Setting runtime form location 4 17
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now