C# 实现更改的DataGridView扩展方法的最佳位置

C# 实现更改的DataGridView扩展方法的最佳位置,c#,winforms,datagridview,C#,Winforms,Datagridview,我希望这不是一个琐碎的问题,因为我对VSC和WINFORMS还是相当陌生的 T.Rahgooy在link上回答了一个问题,它没有得到任何投票,但看起来像是我想尝试的东西,到目前为止,它似乎运行得很好 作为VSC&Winforms的新手,我不确定在哪里安装它,在哪里调用它。我把它放在名称空间的顶部,必须先创建一个类来包含它,然后从我添加的RowDirtyStateRequired事件调用它 问题: 1是否有更好的地方安装扩展方法,即现有类? 2是否有更好的方法调用位置 namespace VX13

我希望这不是一个琐碎的问题,因为我对VSC和WINFORMS还是相当陌生的

T.Rahgooy在link上回答了一个问题,它没有得到任何投票,但看起来像是我想尝试的东西,到目前为止,它似乎运行得很好

作为VSC&Winforms的新手,我不确定在哪里安装它,在哪里调用它。我把它放在名称空间的顶部,必须先创建一个类来包含它,然后从我添加的RowDirtyStateRequired事件调用它

问题: 1是否有更好的地方安装扩展方法,即现有类? 2是否有更好的方法调用位置

namespace VX130
{
    public static class ExtensionHelpers
    {
        public static void ChangeEditModeToOnPropertyChanged(this DataGridView gv)
        {
            //Use this extension method. It works for all columns types, not just ComboBoxes:
            // This method commits every change just after the change is made.  When we have a text column, after typing a character, 
            //its value will commit to the DataSource and the editmode of the cell will end.  Therefore current cell should return to 
            //ed
            gv.CurrentCellDirtyStateChanged += (sender, args) =>
            {
                gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
                if (gv.CurrentCell == null)
                    return;
                if (gv.CurrentCell.EditType != typeof(DataGridViewTextBoxEditingControl))
                    return;
                gv.BeginEdit(false);
                var textBox = (TextBox)gv.EditingControl;
                textBox.SelectionStart = textBox.Text.Length;
            }
        }
    }
}

它是一个类,所以它应该在自己的代码文件中。我建议,创建一个扩展方法库是一个好主意,您可以在将来的任何项目中引用它。您可能希望将方法分成多个库或只使用一个。如果用户希望在当前值中间更改单元格值会发生什么情况?从代码我假设,用户将需要把光标回到中间后,他输入的每个字符。