自定义列显示文本被应用到它不应该应用的单元格
Custom column display text gets applied to cells it shouldn't
我在使用 CustomColumnDisplayText 事件时卡住了。我正在使用此代码:
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
if (e.Column == colVehicle_FahrzeugartID && e.ListSourceRowIndex >= 0)
{
for (int i = 0; i < gridViewList.DataRowCount; i++)
{
object cellValue = gridViewList.GetRowCellValue(i, "Vehicle_FahrzeugartID");
this.clsFahrzeugart.ReadFromDb((int)cellValue);
if (this.clsFahrzeugart.Systemstatus == 11 && e.ListSourceRowIndex == i)
{
e.DisplayText = "Deleted...";
}
}
}
}
如果值 Systemstatus 为 11 表示已删除,这在我的数据库中表示已删除,并且一切正常,但如果我切换我的开关,我的列中有空值,并且一些值正在更改为已删除,尽管它们没有值为 11.
这是切换开关时的样子:
它应该是这样的:
bool isOn = toggleSwitch1.EditValue is bool ? (bool)toggleSwitch1.EditValue : false;
if (isOn)
{
tbAutoBindingSource1.Filter = "Vehicle_Systemstatus = 1";
btn_UnDel.Visible = false;
}
else
{
tbAutoBindingSource1.Filter = "Vehicle_Systemstatus IS NOT NULL";
btn_UnDel.Visible = true;
}
有人知道如何解决这个问题吗?
您不需要遍历所有行,因为此事件是针对单个行引发的。由于此事件提供了 e.ListDataSourceRowIndex property associated with a datasource row index, not a row handle, you need to use the GridView.GetListSourceRowCellValue 方法来访问单元格值。
请参阅 Rows 帮助主题以了解数据源索引和网格行句柄之间的区别。
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
GridView view = sender as GridView;
if (e.Column == colVehicle_FahrzeugartID && e.ListSourceRowIndex >= 0)
{
object cellValue = view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Vehicle_FahrzeugartID");
this.clsFahrzeugart.ReadFromDb((int)cellValue);
if (this.clsFahrzeugart.Systemstatus == 11)
e.DisplayText = "Deleted...";
}
}
你的代码有几个错误。
0。事件无意义的循环。
CustomColumnDisplayText
事件用于在当前处理的单元格中显示文本。因此,您只需要根据该单元格获取值即可。
1. GetRowCellValue
方法使用错误。
您需要为 GetRowCellValue
使用 RowHandle
。 ListSourceRowIndex
这里是错误的。 RowHandle
和 ListSourceRowIndex
不一样。您需要使用 GetRowHandle
方法从 ListSourceRowIndex
.
获取 RowHandle
示例如下:
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
if (e.Column == colVehicle_FahrzeugartID && e.ListSourceRowIndex >= 0)
{
int rowHandle = gridViewList.GetRowHandle(e.ListSourceRowIndex);
object cellValue = gridViewList.GetRowCellValue(rowHandle, "Vehicle_FahrzeugartID");
this.clsFahrzeugart.ReadFromDb((int)cellValue);
if (this.clsFahrzeugart.Systemstatus == 11)
{
e.DisplayText = "Deleted...";
}
}
}
我在使用 CustomColumnDisplayText 事件时卡住了。我正在使用此代码:
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
if (e.Column == colVehicle_FahrzeugartID && e.ListSourceRowIndex >= 0)
{
for (int i = 0; i < gridViewList.DataRowCount; i++)
{
object cellValue = gridViewList.GetRowCellValue(i, "Vehicle_FahrzeugartID");
this.clsFahrzeugart.ReadFromDb((int)cellValue);
if (this.clsFahrzeugart.Systemstatus == 11 && e.ListSourceRowIndex == i)
{
e.DisplayText = "Deleted...";
}
}
}
}
如果值 Systemstatus 为 11 表示已删除,这在我的数据库中表示已删除,并且一切正常,但如果我切换我的开关,我的列中有空值,并且一些值正在更改为已删除,尽管它们没有值为 11.
这是切换开关时的样子:
它应该是这样的:
bool isOn = toggleSwitch1.EditValue is bool ? (bool)toggleSwitch1.EditValue : false;
if (isOn)
{
tbAutoBindingSource1.Filter = "Vehicle_Systemstatus = 1";
btn_UnDel.Visible = false;
}
else
{
tbAutoBindingSource1.Filter = "Vehicle_Systemstatus IS NOT NULL";
btn_UnDel.Visible = true;
}
有人知道如何解决这个问题吗?
您不需要遍历所有行,因为此事件是针对单个行引发的。由于此事件提供了 e.ListDataSourceRowIndex property associated with a datasource row index, not a row handle, you need to use the GridView.GetListSourceRowCellValue 方法来访问单元格值。
请参阅 Rows 帮助主题以了解数据源索引和网格行句柄之间的区别。
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
GridView view = sender as GridView;
if (e.Column == colVehicle_FahrzeugartID && e.ListSourceRowIndex >= 0)
{
object cellValue = view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Vehicle_FahrzeugartID");
this.clsFahrzeugart.ReadFromDb((int)cellValue);
if (this.clsFahrzeugart.Systemstatus == 11)
e.DisplayText = "Deleted...";
}
}
你的代码有几个错误。
0。事件无意义的循环。
CustomColumnDisplayText
事件用于在当前处理的单元格中显示文本。因此,您只需要根据该单元格获取值即可。
1. GetRowCellValue
方法使用错误。
您需要为 GetRowCellValue
使用 RowHandle
。 ListSourceRowIndex
这里是错误的。 RowHandle
和 ListSourceRowIndex
不一样。您需要使用 GetRowHandle
方法从 ListSourceRowIndex
.
RowHandle
示例如下:
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
if (e.Column == colVehicle_FahrzeugartID && e.ListSourceRowIndex >= 0)
{
int rowHandle = gridViewList.GetRowHandle(e.ListSourceRowIndex);
object cellValue = gridViewList.GetRowCellValue(rowHandle, "Vehicle_FahrzeugartID");
this.clsFahrzeugart.ReadFromDb((int)cellValue);
if (this.clsFahrzeugart.Systemstatus == 11)
{
e.DisplayText = "Deleted...";
}
}
}