?
Solved

ArgumentOutofRangeException making GridView cell hidden at runtime

Posted on 2008-10-19
7
Medium Priority
?
1,098 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 2

Accepted Solution

by:
motocross_geek earned 750 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 750 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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