ASPxGridView GetSelectedFieldValues 不起作用

本文关键字:不起作用 GetSelectedFieldValues ASPxGridView | 更新日期: 2023-09-27 18:34:33

我想选择一行并对其 Id 列执行某些操作,但它不起作用,并且我遇到了最后提到的错误。这是我得到的——

ASPxGridView 代码段 -

<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>

注意:网格通过数据表填充

数据表代码 -

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

页面加载代码 -

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();

选择更改的功能 -

    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];                    
            }
    }        

我得到的错误 -

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.

ASPxGridView GetSelectedFieldValues 不起作用

尝试将代码从 Page_Load 移动到 Page_Init 方法。 在这种情况下,一切都应该按预期工作。

 <dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" DataSourceID="AccessDataSource1"
            KeyFieldName="CustomerID" Width="100%">
                    </dx:ASPxGridView>

您必须提供唯一 Ky 列名称键字段名称="唯一列名称">

最终有效的是使用 RowClick 事件而不是 SelectionChanged。