Search code examples
c#asp.netdevexpressaspxgridviewselectionchanged

ASPxGridView GetSelectedFieldValues not working


I want to select a row and do something with it's Id column, however it isn't working and I'm running into the error mentioned at the end. Heres what I got -

The ASPxGridView snippet -

<dx:ASPxGridView ID="ASPxGridView1" runat="server" Font-Names="Arial" Font-Size="Small"
  Width="100%" ClientInstanceName="grid" oncustomcallback="grid_CustomCallback"                                              
  onbeforegetcallbackresult="ASPxGridView1_BeforeGetCallbackResult" 
  EnableCallBacks="False" EnableRowsCache="False" KeyFieldName="ID">

... Columns here ...

<ClientSideEvents ContextMenu="OnContextMenu" SelectionChanged="OnSelectionChanged" />      
</dx:ASPxGridView>

Note: The grid gets populated via a DataTable

DataTable code -

protected DataTable GetHeadlineData(SqlDataReader rdr)
{
    DataTable headlineTable = new DataTable();        
    headlineTable.Load(rdr)
    headlineTable.PrimaryKey = new DataColumn[] { headlineTable.Columns["ID"] };
    return headlineTable;
}

PageLoad code -

DataTable dt= new DataTable();    
dt= FillGrid(); //this function internally calls the above GetHeadlineData function
Session["headTable"] = dt;
ASPxGridView1.DataSource = Session["headTable"];
ASPxGridView1.KeyFieldName = "ID";
ASPxGridView1.DataBind();

SelectionChanged functions -

    function OnSelectionChanged(s, e) {              
        grid.GetSelectedFieldValues("ID", OnGetSelectedFieldValues);            
    }

    function OnGetSelectedFieldValues(result) {
        for (var i = 0; i < result.length; i++)
            for (var j = 0; j < result[i].length; j++) {
                document.getElementById('selectedRowDiv').innerHTML = result[i];                    
            }
    }        

Error I'm getting -

A primary key field specified via the KeyFieldName property is not found in the 
underlying data source. Make sure the field name is spelled correctly. Pay 
attention to the character case.

Solution

  • What eventually worked was using the RowClick event instead of the SelectionChanged.