获取SfDataGrid中特定单元格值的技术



我正在创建一个非常简单的库存控制应用程序,供一个人使用。我想我会很喜欢并使用Syncfusion的一些控件来获得更好看的UI。不幸的是,SfDataGrid似乎不包含任何与DataGridView相同的函数。我在使用他们提供的获取单元格值的技术时遇到了问题。以下是我从他们的文档中得到的片段:

private static string GetCellValue(SfDataGrid dGrid, int rowIndex, int columnIndex)
{
string cellValue;
if (columnIndex < 0)
return string.Empty;
var mappingName = dGrid.Columns[columnIndex].MappingName;
var recordIndex = dGrid.TableControl.ResolveToRecordIndex(rowIndex);
if (recordIndex < 0)
return string.Empty;
if (dGrid.View.TopLevelGroup != null)
{
var record = dGrid.View.TopLevelGroup.DisplayElements[recordIndex];
if (!record.IsRecords)
return string.Empty;
var data = (record as RecordEntry).Data;
cellValue = (data.GetType().GetProperty(mappingName).GetValue(data, null).ToString());
}
else
{
var record1 = dGrid.View.Records.GetItemAt(recordIndex);
cellValue = (record1.GetType().GetProperty(mappingName).GetValue(record1, null).ToString());
}
return cellValue;
}

在cellValue赋值中调用GetProperty(mappingName(之前,此操作一直很好。我想获得有问题项目的系统ID,因为我的数据库将使用它来删除特定项目。这个方法将获得我需要的列的映射名称,但它不会从单元格中提取数据,我也不知道为什么。以下是自动窗口的捕获,显示与函数相关的变量:自动选项卡显示相关变量,如cellValue和mappingName。它清楚地展示了cellValue在应该包含整数字符串时返回null。cellValue应该包含一个整数字符串,然后我将以数据库为基础删除指定的项。我尝试过获取列的集合,并在其中循环,直到找到匹配的映射名称,然后硬连接列索引。据我所知,列和行索引不是问题所在。系统ID列的索引为6,行索引是通过使用e.DataRow.RowIndex的单击事件参数访问的。下面的函数是GetCellValue函数的调用,它可能在确定此问题时有一定的用处。

private void inventoryGridView_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
{
if (e.DataColumn.ColumnIndex == 0) 
{
return;
}
if (e.DataColumn.ColumnIndex == 7) 
{
if (MessageBox.Show("Are you sure you want to delete this item?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes) 
{
//TODO: Make delete buttons work
//this code below calls a function that may not be working properly
// Get the row index value        
var rowIndex = e.DataRow.RowIndex;
//Get the column index value
var columnIndex = 6;
//Get the cell value            
var cellValue = GetCellValue(inventoryGridView, rowIndex, columnIndex);
MessageBox.Show("Cell value t:  " + cellValue, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return;
}
}

inventoryGridView是我的SfDataGrid,而列索引7是一列删除按钮,它们应该删除与其共享一行的项。如果需要更多信息,请告诉我,我会提供。我确实在GitHub上找到了这个样本,但它与Syncfusion网站上的文档相同。我还记得读过一篇与Syncfusion论坛帖子、回复和其他内容完全相同的stackoverflow帖子。尽管这也没有帮助,因为这似乎意味着GetCellValue应该已经是一个成员方法,至少在我的情况下不是这样。

最后,这里是我的SfDataGrid的图片,所以你们都可以看到完整的图片:SfDataGridinventoryGridView示例图片。

因此,对于任何陷入与我相同深渊的人,以下是一些答案:Syncfusion批准的方法在他们的样本项目之外不起作用(至少对我和其他许多人来说是这样(。GetItemAt((函数实际上返回一个DataRowView,而不是您要查找的项对象。要真正从特定单元格中提取值,您需要替换以下代码行:

var record1 = dGrid.View.Records.GetItemAt(recordIndex);
cellValue = (record1.GetType().GetProperty(mappingName).GetValue(record1, null).ToString());

有了这行代码:

DataRowView record1 = dGrid.View.Records.GetItemAt(recordIndex) as DataRowView;
cellValue = (record1[mappingName].ToString()); 

我不能100%确定为什么这个修复程序对我有效,但在我弄清楚返回的确切内容后,DataRowView上的Microsoft文档引导我找到了所需的内容。在这里,我使用DataRowView.Item[]属性来获得我需要的内容。

我希望这能帮助任何人发现自己处于这种情况,这里有一个链接到我上面提到的属性文档:DataRowView.Item[]

根据提供的信息,报告的问题是由于在SfDataGrid中获取DataRow的类型转换不匹配而发生的。

您可以通过将record1数据对象类型转换为DataRowView来解决报告的问题,如下面提到的代码片段,

private static string GetCellValue(SfDataGrid dGrid, int rowIndex, int columnIndex)
{

string cellValue;
if (columnIndex < 0)
return string.Empty;
var mappingName = dGrid.Columns[columnIndex].MappingName;
var recordIndex = dGrid.TableControl.ResolveToRecordIndex(rowIndex);
if (recordIndex < 0)
return string.Empty;
if (dGrid.View.TopLevelGroup != null)
{
var record = dGrid.View.TopLevelGroup.DisplayElements[recordIndex];
if (!record.IsRecords)
return string.Empty;
var data = (record as RecordEntry).Data;
//below case using for ObservableCollection binded in dataGrid
//cellValue = (data.GetType().GetProperty(mappingName).GetValue(data, null).ToString());
//below case using for DataTable Collection binded in dataGrid 
cellValue = (data as DataRowView).Row[mappingName].ToString();
}
else
{
var record1 = dGrid.View.Records.GetItemAt(recordIndex);
//below case using for ObservableCollection binded in dataGrid  
//cellValue = (record1.GetType().GetProperty(mappingName).GetValue(record1, null).ToString());
//below case using for DataTable Collection binded in dataGrid 
cellValue = (record1 as DataRowView).Row[mappingName].ToString();
}
return cellValue;
}

示例链接:https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample-2052934720

相关内容

最新更新