【问题标题】:C# datagridview get current cell value before Drag&Drop valueC# datagridview 在拖放值之前获取当前单元格值
【发布时间】:2020-01-16 21:38:04
【问题描述】:

我有一个 dgv 和一个富文本框。用户可以从 rtb 中选择一个名称并拖动到 dgv 中的一个单元格。如果它们放入的当前单元格中没有值,我可以正常工作。但是,如果单元格有一个值并且它们在该单元格上放置一个值,我会丢失以前的值。这是我的拖放代码

if (e.Data.GetDataPresent(typeof(System.String)))
        {
            Point clientPoint = gridViewRaces.PointToClient(new Point(e.X, e.Y));
            gridViewRaces.Rows[gridViewRaces.HitTest(clientPoint.X,
            clientPoint.Y).RowIndex].Cells[gridViewRaces.HitTest(clientPoint.X,
            clientPoint.Y).ColumnIndex].Value =
            (System.String)e.Data.GetData(typeof(System.String));
        }

在将值插入单元格之前,我希望能够检索当前值(如果存在的话)..

string currentValue = gridViewRaces.Rows[e.Y].Cells[e.X].Value.ToString();

任何帮助将不胜感激!

【问题讨论】:

  • 为什么在继续删除新数据之前不能完全做到这一点?
  • @ŇɏssaPøngjǣrdenlarp 我得到一个 System.ArgumentOutOfRangeException
  • @ŇɏssaPøngjǣrdenlarp 我自己制定了一个解决方案。不过,感谢您花时间看它。

标签: c# datagridview drag-and-drop


【解决方案1】:

在拔掉头发几个小时后,我终于意识到我在整个 F*****G 时间里都得到了答案。

这是插入被拖动值的代码 -

if (e.Data.GetDataPresent(typeof(System.String)))
    {
        Point clientPoint = gridViewRaces.PointToClient(new Point(e.X, e.Y));
        gridViewRaces.Rows[gridViewRaces.HitTest(clientPoint.X,
        clientPoint.Y).RowIndex].Cells[gridViewRaces.HitTest(clientPoint.X,
        clientPoint.Y).ColumnIndex].Value =
        (System.String)e.Data.GetData(typeof(System.String));
    }

为了在插入新值之前获取之前的值,我所要做的就是这样做

string previousValue = gridViewRaces.Rows[gridViewRaces.HitTest(clientPoint.X,
            clientPoint.Y).RowIndex].Cells[gridViewRaces.HitTest(clientPoint.X,
            clientPoint.Y).ColumnIndex].Value.ToString();

            gridViewRaces.Rows[gridViewRaces.HitTest(clientPoint.X,
            clientPoint.Y).RowIndex].Cells[gridViewRaces.HitTest(clientPoint.X,
            clientPoint.Y).ColumnIndex].Value =
            (System.String)e.Data.GetData(typeof(System.String));

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2012-05-13
    相关资源
    最近更新 更多