我想选择一行并用它的 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>
注意:网格通过 DataTable 填充
数据表代码 -
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();
SelectionChanged 函数 -
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.