Matthew Walk
Matthew Walk

Reputation: 1054

Why is the Telerik RadGrid KeyValues string empty when UpdateCommand is running?

While debugging the RadGrid1_UpdateCommand I noticed that the KeyValues of the GridEditableItem are empty (just a set a brackets). There are 49 SavedOldValues. The data table that sources the grid has a primary key on it.
The grids data source is manually specified in the C# code.

Why would the KeyValues be empty?

Upvotes: 0

Views: 396

Answers (1)

woodykiddy
woodykiddy

Reputation: 6455

Have you specified the key name(s) for DataKeyNames attribute on MasterTableView element of the RadGrid? Say your PK column is called, ID, then you can just assign it like this.

<telerik:RadGrid runat="server" ID="radgrid1">
    <MasterTableView DataKeyNames="ID">
        <Columns>...</Columns>
    </MasterTableView>
</telerik:RadGrid>

Then you can get the key value via DataKeyValues collection and then convert it as an integer for use.

protected void radgrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    var keyValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();
    int id = Convert.ToInt32(keyValue);
}

Upvotes: 2

Related Questions