This is a migrated thread and some comments may be shown as answers.

Question about DataKeyNames

2 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Boris
Top achievements
Rank 1
Boris asked on 30 Oct 2012, 04:21 PM
With regard to Atchut's thread: http://www.telerik.com/community/forums/aspnet-ajax/grid/datakeynames.aspx

Please clarify.  I needed to access the value of a bound column so I added it to DataKeyNames as suggested.  The grid still appears to be working normally and I'm getting the data I need. 

However, this column is not a key field in the database sense, not in this table.  Does this matter?

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 31 Oct 2012, 04:01 AM
Hi Boris,

DataKeyNames represent the primary key of the data source. You don't need DataKeyNames/value to access a GridBoundColumn value. DataKeyNames/values is used while editing when primary key is needed for database update operation. You can set a unique field as DataKeyName.

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
    {
        GridDataItem ditem = (GridDataItem)e.Item;
        String value = ditem["ColumnUniqueName"].Text;      
    }
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem eitem = (GridEditableItem)e.Item;
        TextBox txt = (TextBox)eitem["ColumnUniqueName"].Controls[0];
        string editvalue = txt.Text;
    }
}

Thanks,
Shinu.
0
Boris
Top achievements
Rank 1
answered on 31 Oct 2012, 12:45 PM

Actually I find that I can't get that to work at all.  It just crashes.
What I'm going to do for now is assume that

if (ditem["FieldName"].Text.Trim() == " ")

   {
// Do something here
}

 

 

 

 

Tags
Grid
Asked by
Boris
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Boris
Top achievements
Rank 1
Share this question
or