Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ArgumentOutofRangeException making GridView cell hidden at runtime

Posted on 2008-10-19
7
Medium Priority
?
1,111 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
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
Independent Software Vendors: 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!

 
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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Loops Section Overview
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

581 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