C# Winforms DataGridView正在添加两行而不是一行

C# Winforms DataGridView正在添加两行而不是一行,c#,winforms,datagridview,C#,Winforms,Datagridview,当我向DataGridView中的一行添加数据时,我希望添加一个空行。这种情况会发生,但当我关注当前行中的某个字段且未输入任何数据时,将填充该行的默认值,新行也将填充默认值。然后添加另一个空行,正如我在下面粘贴的动画gif所示 我想看看是否有人可以帮助我确定如何防止空白行填充默认值以及当前行。在当前行中退出字段而不输入值之后,默认值填充该行是可以的,但我不希望任何填充空白行。 我已经粘贴了下面表单类中的相关代码。我试图消除课堂上与问题无关的部分: public partial class fr

当我向DataGridView中的一行添加数据时,我希望添加一个空行。这种情况会发生,但当我关注当前行中的某个字段且未输入任何数据时,将填充该行的默认值,新行也将填充默认值。然后添加另一个空行,正如我在下面粘贴的动画gif所示

<>我想看看是否有人可以帮助我确定如何防止空白行填充默认值以及当前行。在当前行中退出字段而不输入值之后,默认值填充该行是可以的,但我不希望任何填充空白行。

我已经粘贴了下面表单类中的相关代码。我试图消除课堂上与问题无关的部分:

public partial class frmCharitableContributions : Form
{
    private readonly string connectionString = ConnectionStringProvider.GetConnectionString();
    private readonly BusinessLayer bl = new BusinessLayer();


    private void FrmCharitableContributions_Load(object sender, EventArgs e)
    {

        PopulateDataGridView();
    }


    private void PopulateDataGridView()
    {
        try
        {
            DataSet dsCharitableContributions = bl.GetCharitableContributions(connectionString);

            CharitableContributionsDataGridView.DataSource = dsCharitableContributions.Tables[0];
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception Thrown in PopulateDataGridView: " + ex.Message);
        }
    }


    private void CharitableContributionsDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (CharitableContributionsDataGridView.CurrentRow != null)
        {
            DataGridViewRow dgvRow = CharitableContributionsDataGridView.CurrentRow;

            bool isValid = ValidateFields(dgvRow);
            if (isValid)
            {
                bl.InsertOrUpdateCharitableContributions(connectionString, dgvRow);
            }
            else
            {
                return;
            }

            BeginInvoke(new MethodInvoker(PopulateDataGridView));
        }
    }

    // This event will fire when a cell recognize editing
    private void CharitableContributionsDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        CharitableContributionsDataGridView.AllowUserToAddRows = false;
    }

    // This event will fire when a cell recognizes the end of editing
    // Allow or deny adding a new row here based on conditions such as required fields being filled in or selected
    private void CharitableContributionsDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int currentRowIndex = e.RowIndex;
        DataGridViewRow dgvRow = CharitableContributionsDataGridView.Rows[currentRowIndex];
        if (dgvRow.Cells["cbxCharitableOrganizationName"].Value.ToString() != "")
        {

            if (CharitableContributionsDataGridView.CurrentRow != null)
            {


                bl.InsertOrUpdateCharitableContributions(connectionString, dgvRow);

                CharitableContributionsDataGridView.AllowUserToAddRows = true;
            }
        }
    }
}

只能在begincelledit事件中关闭AllowUserToAddress。以前开过吗?如果是这样的话,为什么要关闭它?如果关闭它,会发生什么?您是否真的从
\u CellValueChanged
调用
PopulateDataGridView
,重置数据源?@TaW我尝试在构造函数中关闭AllowUserToAddress,但在执行此操作时,网格中没有可编辑的行。然后我在RowLeave事件中尝试关闭它。这使我可以填充一行,但不能添加任何其他内容。@Jimi没有从CellValueChanged事件中调用PopulateDataGridView方法,而是使用BeginInvoke方法调用PopulateDataGridView,因为我收到一个错误,该操作无效,因为它导致对SetCurrentAddressCore的重新调用功能。在PopulatedDataGridView中,每次调用该方法时,我都会设置数据源。您可以在数据源中添加新行,只需:
var dt=dataGridView1.DataSource as DataTable;添加(dt.NewRow())。如果已经存储了DataTable对象,只需使用它即可。新行将立即显示在刚刚创建的行之后。