NoviceToDotNet
NoviceToDotNet

Reputation: 10805

asp.net gridview

I have a gridview which contains columns of my desire to display. But I want to access a value for data row of a gridview which is not displayed in gridview but it is in datatable for that particular row.
Initially what I did is display it to gridview as well as I accessed its value using index, but I really don't want to display its value in gridview. In that case I set visible=false but then it doesn't recognize to cell so what should I do to achieve this?
Please let me know so that I don't have to display that particular row and I can access the value for that row which do exist in data table.

Upvotes: 0

Views: 109

Answers (2)

Dindar
Dindar

Reputation: 3235

You can access all column values if you set their visible="false" , In this situation You need to set datakey name for your gridview .

    //In GridView RowCommand Event:
    int index = Convert.ToInt32(e.CommandArgument);

    //If we have more than one DataKeys ( ItemId is the field that is visbile = false and myGridView.DatakeyName = Itemid)
    int ItemId = Convert.ToInt32(GridView1.DataKeys[index].Values["ItemId"]);
    //else
    int ItemId = Convert.ToInt32(GridView1.DataKeys[index].Value);

If your desired column is not datakey , So you can use gridViewRow

  GridViewRow row = GridView1.Rows[index];

and Then you can access all the column of that row .

Hope this help

Upvotes: 1

Rebecca Chernoff
Rebecca Chernoff

Reputation: 22605

Are you trying to use this for something like a primary key from the table? Here's a blog post I wrote a couple years ago that explains how you can leverage a field like this from the database without it displaying. The gist of it is that you should look into using DataKeys.

Using a Primary Key with ASP.NET Data Controls

Upvotes: 0

Related Questions