feat: 更新业务及控件功能

This commit is contained in:
cyf
2026-08-21 17:21:18 +08:00
parent d3587a16f1
commit dd58f12374
16 changed files with 220 additions and 86 deletions
@@ -21,6 +21,7 @@ namespace Lskj.Control.Model
public class GridViewMenuStrip : BaseRightMenu
{
private int[] _oldSelectRows;
private HashSet<object> _oldSelectPrimaryValues;
private DataRow _oldDataRow;
private int _oldRowHandler;
private GridView _gridView;
@@ -231,41 +232,116 @@ namespace Lskj.Control.Model
this._oldSelectRows = this._gridView.GetSelectedRows();
this._oldDataRow = this._gridView.GetFocusedDataRow();
this._oldRowHandler = this._gridView.FocusedRowHandle;
// 复选框模式下保存选中行的主键值,避免刷新后行号对应到其他数据。
this._oldSelectPrimaryValues = null;
string primaryKey = this.GridControlObj == null ? string.Empty : this.GridControlObj.MultiplePrimary;
bool isCheckBoxRowSelect = this._gridView.OptionsSelection.MultiSelect == true
&& this._gridView.OptionsSelection.EnableAppearanceFocusedCell == false
&& this._gridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.CheckBoxRowSelect;
if (isCheckBoxRowSelect && !string.IsNullOrWhiteSpace(primaryKey) && this._oldSelectRows != null)
{
foreach (int rowHandle in this._oldSelectRows)
{
DataRow row = this._gridView.GetDataRow(rowHandle);
if (row == null || !row.Table.Columns.Contains(primaryKey) || row.IsNull(primaryKey))
continue;
object primaryValue = row[primaryKey];
if (string.IsNullOrWhiteSpace(primaryValue + ""))
continue;
if (this._oldSelectPrimaryValues == null)
this._oldSelectPrimaryValues = new HashSet<object>();
this._oldSelectPrimaryValues.Add(primaryValue);
}
}
}
protected override void MenuStripClickAfter(ToolStripMenuItem item, GridRightMenuModel model)
{
DataTable dataTable = this.GridControlObj.GridView.GetGridViewFilteredAndSortedDataToDataTable() as DataTable;
List<DataRow> rows = new List<DataRow>() { this._oldDataRow };
IEnumerable<DataRow> QBJ = dataTable.Select().AsEnumerable().Intersect(rows.AsEnumerable(), DataRowComparer.Default);
int NRowHandle = 0;
try
string ParmaryKey = string.Empty;
if(this.GridControlObj!=null) ParmaryKey = GridControlObj.MultiplePrimary;
bool isCheckBoxRowSelect = this._gridView.OptionsSelection.MultiSelect == true&& this._gridView.OptionsSelection.EnableAppearanceFocusedCell == false && this._gridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.CheckBoxRowSelect;
DataTable currentDataTable = this.GridControlObj == null ? null : this.GridControlObj.GridControl.DataSourceTable();
if (isCheckBoxRowSelect&& !string.IsNullOrWhiteSpace(ParmaryKey) && this._oldSelectPrimaryValues != null&& this._oldSelectPrimaryValues.Count > 0&& currentDataTable != null&& currentDataTable.Columns.Contains(ParmaryKey))
{
DataTable NDataTable = QBJ.CopyToDataTable();
if (NDataTable.Rows.Count > 0)
//2026-08-17添加,如果是复选框模式,并且有主键,右键后选中之前的行用主键值判断
// 只恢复刷新后仍存在的主键,已删除的数据不再按原行号选中。
this._gridView.ClearSelection();
if (!model.RefreshCheckState)
{
NRowHandle = dataTable.Rows.IndexOf(QBJ.First());
this._oldRowHandler = NRowHandle >= 0 ? NRowHandle : this._oldRowHandler;
}
}
catch (System.InvalidOperationException ex) //如果差集为null,就回进当前报错
{
}
if (this._gridView.OptionsSelection.MultiSelect == true && this._gridView.OptionsSelection.EnableAppearanceFocusedCell == false && this._gridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.CheckBoxRowSelect)
{
if (model.RefreshCheckState)
this._gridView.ClearSelection();
else if (!model.RefreshCheckState && this._oldSelectRows != null)
{
foreach (int index in this._oldSelectRows)
DataColumn[] tablePrimaryKey = currentDataTable.PrimaryKey;
bool canFindByPrimaryKey = this.GridControlObj.GridControl.DataSource is DataTable
&& tablePrimaryKey != null
&& tablePrimaryKey.Length == 1
&& tablePrimaryKey[0].ColumnName.Equals(ParmaryKey, StringComparison.OrdinalIgnoreCase);
if (canFindByPrimaryKey)
{
this._gridView.SelectRow(index);
// DataTable设置了PrimaryKey时,使用其内部索引直接查找。
foreach (object primaryValue in this._oldSelectPrimaryValues)
{
DataRow row = currentDataTable.Rows.Find(primaryValue);
if (row == null)
continue;
int rowHandle = this._gridView.GetRowHandle(currentDataTable.Rows.IndexOf(row));
if (rowHandle >= 0)
this._gridView.SelectRow(rowHandle);
}
}
else
{
// MultiplePrimary只是字段配置时没有数据索引,单次扫描比逐个LocateByValue更稳定。
HashSet<object> remainingPrimaryValues = new HashSet<object>(this._oldSelectPrimaryValues);
for (int rowHandle = 0; rowHandle < this._gridView.DataRowCount && remainingPrimaryValues.Count > 0; rowHandle++)
{
DataRow row = this._gridView.GetDataRow(rowHandle);
if (row == null || !row.Table.Columns.Contains(ParmaryKey) || row.IsNull(ParmaryKey))
continue;
if (remainingPrimaryValues.Remove(row[ParmaryKey]))
this._gridView.SelectRow(rowHandle);
}
}
}
}
else
{
this._gridView.ClearSelection();
this._gridView.SelectRowHandler(this._oldRowHandler);
DataTable dataTable = this.GridControlObj.GridView.GetGridViewFilteredAndSortedDataToDataTable() as DataTable;
List<DataRow> rows = new List<DataRow>() { this._oldDataRow };
IEnumerable<DataRow> QBJ = dataTable.Select().AsEnumerable().Intersect(rows.AsEnumerable(), DataRowComparer.Default);
int NRowHandle = 0;
try
{
DataTable NDataTable = QBJ.CopyToDataTable();
if (NDataTable.Rows.Count > 0)
{
NRowHandle = dataTable.Rows.IndexOf(QBJ.First());
this._oldRowHandler = NRowHandle >= 0 ? NRowHandle : this._oldRowHandler;
}
}
catch (System.InvalidOperationException ex) //如果差集为null,就回进当前报错
{
}
if (this._gridView.OptionsSelection.MultiSelect == true && this._gridView.OptionsSelection.EnableAppearanceFocusedCell == false && this._gridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.CheckBoxRowSelect)
{
if (model.RefreshCheckState)
this._gridView.ClearSelection();
else if (!model.RefreshCheckState && this._oldSelectRows != null)
{
foreach (int index in this._oldSelectRows)
{
this._gridView.SelectRow(index);
}
}
}
else
{
this._gridView.ClearSelection();
this._gridView.SelectRowHandler(this._oldRowHandler);
}
}
}
protected override List<string> HandleRightMenuParams(List<string> paramList, DataRow rowData, DataRow[] rowDatas = null, int actionType = 0)