edit row from datagrid while hiding the record ID

Discussion in 'ASP .Net' started by Dica, Aug 7, 2007.

  1. Dica

    Dica Guest

    i need to allow the user to select a row from my dataGrid for editing. as
    such, i include the record ID in the first column and then extract like so
    when retrieving the record details:

    protected void gvLocations_edit(object sender, GridViewEditEventArgs e)
    {
    // load the record details into the edit fields //
    GridView gvRecord = (GridView)sender;
    string sThisRecord = gvRecord.Rows[e.NewEditIndex].Cells[0].Text;
    ...
    }

    i've been told i need to exclude the record ID from the datagrid, so i set
    the column's visibility property to false. now my code fails as index[0] is
    no longer the record id but a varChar column.

    how to fix?

    tks
    Dica, Aug 7, 2007
    #1
    1. Advertising

  2. "Dica" <> wrote in message
    news:uE4ui.96672$xk5.46142@edtnps82...

    >i need to allow the user to select a row from my dataGrid for editing. as
    >such, i include the record ID in the first column and then extract like so
    >when retrieving the record details:
    >
    > protected void gvLocations_edit(object sender, GridViewEditEventArgs e)
    > {
    > // load the record details into the edit fields //
    > GridView gvRecord = (GridView)sender;
    > string sThisRecord = gvRecord.Rows[e.NewEditIndex].Cells[0].Text;
    > ...
    > }
    >
    > i've been told i need to exclude the record ID from the datagrid, so i set
    > the column's visibility property to false. now my code fails as index[0]
    > is no longer the record id but a varChar column.
    >
    > how to fix?


    You're using a GridView, not a DataGrid - it's really helpful to call things
    by their correct names when posting in these newsgroups...

    Hidden columns in GridViews are not rendered by default to the client as a
    security measure - the DataKeyNames property should be used instead:
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames(vs.80).aspx

    Alternatively, you can force the hidden column(s) to be databound and
    rendered, but this is not recommended:

    MyGridView.DataSource = <datasource>;
    MyGridView.Columns[0].Visible = true;
    MyGridView.DataBind();
    MyGridView.Columns[0].Visible = false;


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Aug 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Retrieving Record Key while creating the record.

    =?Utf-8?B?SnVzdGlu?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    622
    =?Utf-8?B?SnVzdGlu?=
    Oct 5, 2004
  2. Schultz
    Replies:
    3
    Views:
    546
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 14, 2005
  3. D
    Replies:
    0
    Views:
    197
  4. CGW
    Replies:
    0
    Views:
    91
  5. Ste
    Replies:
    41
    Views:
    789
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page