winforms 如何在编辑后重新选择DGV中的同一单元格?

wwtsj6pe  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(141)

抱歉,如果这是一个微不足道的问题,我几乎没有开始C#和编程一般。
默认行为似乎是跳到下一行和单元格0。我尝试了如下操作:

private void DBUpdate()
    {
        da = new MySqlDataAdapter("SELECT * FROM WPVersand WHERE erledigt =''", Globals.cn);
        MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
        cb.ConflictOption = ConflictOption.OverwriteChanges;

        System.Data.DataTable changes = dt.GetChanges();
        if (changes != null)
        {
            if (dt.Rows.Count > 0)
            {
                // Set the current cell to the updated cell
                int row = dgvDaten.CurrentCell.RowIndex;
                int column = dgvDaten.CurrentCell.ColumnIndex;

                DialogResult res = MessageBox.Show("Änderung übernehmen?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    

                    da.Update(changes);
                    dt.AcceptChanges();

                    
                    dgvDaten.CurrentCell = dgvDaten[column, row];
                    dgvDaten.Refresh();
                }
                else if (res == DialogResult.No)
                {
                    bs = new BindingSource();
                    bs.DataSource = dt;

                    dgvDaten.AutoGenerateColumns = true;
                    dgvDaten.Columns.Clear();

                    dgvDaten.DataSource = bs;
                }
            }
        }
    }

    private void dgvDaten_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {         
        bs.EndEdit();
        dgvDaten.EndEdit();
        DBUpdate();

    }

但这只会重新选择下一行的同一个单元格。
我错过了什么?
非常感谢!

fcipmucu

fcipmucu1#

实现该结果的一种方法是继承DataGridView,用新的DataGridViewEx控件替换Designer文件中的控件,然后更改其默认行为,自定义它以执行您想要的操作,即在编辑后重新选择DGV中的同一单元格
这是我测试的第一个定制DataGridViewEx,看起来很成功。不管这是否是你所寻找的行为,这就是你可以做到的,这些方法与行/列/单元格/当前单元格选择有关。

class DataGridViewEx : DataGridView
{
    protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
    {
        _isChanging = true;
        base.OnCellValueChanged(e);
        BeginInvoke(new Action(() => _isChanging = false));
    }
    bool _isChanging = false;
    protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
    {
        if (_isChanging)
        {
            base.SetSelectedCellCore(CurrentCell.ColumnIndex, CurrentCell.RowIndex, selected);
        }
        else
        {
            base.SetSelectedCellCore(columnIndex, rowIndex, selected);
        }
    }
    protected override void SetSelectedColumnCore(int columnIndex, bool selected)
    {
        if (_isChanging)
        {
            base.SetSelectedColumnCore(CurrentCell.ColumnIndex, selected);
        }
        else
        {
            base.SetSelectedColumnCore(columnIndex, selected && !_isChanging);
        }
    }
    protected override void SetSelectedRowCore(int rowIndex, bool selected)
    {
        if (_isChanging)
        {
            base.SetSelectedRowCore(CurrentCell.RowIndex, selected);
        }
        else
        {
            base.SetSelectedRowCore(rowIndex, selected && !_isChanging);
        }
    }
    protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)
    {
        if (_isChanging)
        {
            return base.SetCurrentCellAddressCore(CurrentCell.ColumnIndex, CurrentCell.RowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);
        }
        else
        {
            return base.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);
        }
    }
}
    • 测试**

我用这个最小的代码来测试,因为这个问题 * 是 * 关于单元格选择的,而 * 不是 * 关于DataTable绑定的,所以我选择为DGV做一个非常简单的DataSource来测试。

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        dgvDaten.AllowUserToAddRows = false;

        dgvDaten.DataSource = Records;
        Records.Add(new Record());
        dgvDaten.Columns[nameof(Record.Guid)].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        dgvDaten.Columns[nameof(Record.Description)].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        Records.Clear();

        Records.Add(new Record { Description = "Apple" });
        Records.Add(new Record { Description = "Orange" });
        Records.Add(new Record { Description = "Grape" });
    }

    BindingList<Record> Records = new BindingList<Record>();
}
class Record
{
    public string Guid { get; set; } = System.Guid.NewGuid().ToString().Substring(0, 15).ToUpper();
    public string Description { get; set; }
}

相关问题