how to get value of a hidden column of gridview in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • denvercr
    New Member
    • Aug 2006
    • 13

    how to get value of a hidden column of gridview in C#

    Hi

    how do i possibly get the value of a hidden column (id) of a gridview binded in sqldatasource? i have this code in C# usng GridViewRow and DataKey Classes to retrieve it, I also set the DataKeyNames to "id".. here is the code:

    GridViewRow gridViewRow;
    DataKey dataKey;

    gridViewRow = GridView.Select edRow;
    dataKey = GridView.DataKe ys[gridViewRow.Row Index];
    Label1.text = Convert.ToStrin g(dataKey.Value ); // This should display id value.

    Once i fire this up, an exception occured: "Object reference not set to an instance of an object".

    The code works fine in VB but in C# it doesnt. Please help me out.. Thank you so much.
  • aaronsandoval
    New Member
    • Nov 2007
    • 13

    #2
    I know this may have been posted a while back, but here is the answer for future readers who may be searching for this answer:

    GridViewRow DataKeyName Value

    IMPORTANT: Make sure you have set the DataKeyNames value equal to the ID your trying to find (example: EmployeeID from a database table with a list of Employee Information)

    Scenarios:

    1.Scenario)
    If you have a GridView control and set OnRowEditing="m yGridView_RowEd iting"

    1.Answer)
    [code=cpp]
    public void myGridView_RowE diting(Object sender, GridViewEditEve ntArgs e)
    {
    myGridView.Edit Index = e.NewEditIndex;
    DataKey dk = myGridView.Data Keys[e.NewEditIndex];
    int myDataKeyID = (int)dk.Values["MyDataKeyI D"];

    Label1.Text = myDataKeyID.ToS tring();
    }
    [/code]
    2.Scenario)
    If you enstantiate the call through GridView TemplateField Column that has an ImageButton with an OnClick="btnEdi tGridData_Click " method

    2.Answer)
    [code=cpp]
    protected void btnEditGridData _Click(object sender, EventArgs e)
    {
    ImageButton btnEditGridData = sender as ImageButton;
    GridViewRow row = (GridViewRow)bt nEditGridData.N amingContainer;
    int rowIndex = row.RowIndex;
    DataKey dk = myGridView.Data Keys[rowIndex];
    int value = Convert.ToInt32 (dk.Value);

    Label1.Text = value.ToString( );
    }[/code]
    Last edited by Frinavale; Nov 21 '07, 09:09 PM. Reason: Added [code] tags

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      There should be no difference in accessing hidden vs non-hidden columns in a gridview.
      What you need to remember to check is if .SelectedRow ==null, and other checks FIRST

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by aaronsandoval
        I know this may have been posted a while back, but here is the answer for future readers who may be searching for this answer:

        GridViewRow DataKeyName Value

        IMPORTANT: Make sure you have set the DataKeyNames value equal to the ID your trying to find (example: EmployeeID from a database table with a list of Employee Information)
        Wow thanks for the detailed reply!

        Comment

        Working...