Winform DataGridView添加数据后,数据自动删除的问题

最近碰到一个问题,在DataGridView里添加数据后,当失去行焦点时,数据会自动删除的问题

DataGridView数据来自DataTable

一直没有找到问题所在.

  /// <summary>

        /// 添加新行到DataGridView中

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void dgv_NewRowNeeded(object sender, DataGridViewRowEventArgs e)

        {

             if (dgvContact.Focused)

            {

                e.Row.Cells["ContactSex"].Value = "男";

                e.Row.Cells["ContactDuties"].Value = CommonHelp.GetTypeName(1);

                e.Row.Cells["ContactName"].Value = "请输入姓名";

                // e.Row.Cells["Contact_CustomerId"].Value = GetCustomerId;

            }

            else if (dgvWeb.Focused)

            {

                e.Row.Cells["WebServiceType"].Value = CommonHelp.GetTypeName(4);

                e.Row.Cells["WebService"].Value = CommonHelp.GetTypeName(2);

                e.Row.Cells["WebStatus"].Value = CommonHelp.GetTypeName(5);

                e.Row.Cells["WebPrice"].Value = 0;

                e.Row.Cells["CostPrice"].Value = 0;

                e.Row.Cells["WebStartDate"].Value = DateTime.Now.Date.ToString("yyyy-MM-dd");

                e.Row.Cells["WebEndDate"].Value = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd");

                e.Row.Cells["WebAddress"].Value = "请输入地址";

             //   e.Row.Cells["Web_CustomerId"].Value = GetCustomerId;

            }

        }

经过一上午的排查终于找到原因,上面代码注释的地方

原来DataGridview添加行时,会根据绑定的数据的类型对DataGridView行进行验证,因为我在添加时没有对Contact_CustomerId和Web_CustomerId列进行 设置,所以他们默认值是Null,而绑定的数据类型是Int,所以验证没有通过

但数据还是会添加到绑定的DataTable中只是不会在DataGridView中并不会显示出来而已,.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值