自定义datagridview列上的数据绑定问题 [英] Problem with data binding on custom datagridview column

查看:106
本文介绍了自定义datagridview列上的数据绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个自定义的gridview列,该列在编辑模式下显示datetimepicker控件.我使用了msdn (在此处)中的文章,以达到这一阶段.直到我将其连接到数据源,该控件才能正常工作.当它绑定到数据表时,当我在新行"中的自定义编辑单元上单击和退出时,它将在数据表中生成一个新的空白行(网格未显示新行,但他在数据表中加上rowstate =).

我的问题是,如何停止添加这些新行?当编辑模式结束时,单元格应包含什么值,这样它就不会被视为数据输入(我是DBNull,它不起作用,不允许使用NULL)?或者,如果值不是问题,那么在编辑该单元格后我应该使用什么方法或事件来取消行的创建?

似乎我无法在此处添加整个代码,因此我将仅添加我认为与该问题有关的方法
PS:CalendatEditingControl是一个自定义的datetimepicker(允许通过BindableValue使用DBNull值)并实现IDataGridViewEditingControl

Hi,
I have a custom gridview column which displays a datetimepicker control when in edit mode. I used the article from msdn (here) in order to get to this stage. The control works fine until I connect it to a DataSource. When it is bound to a datatable, when I click in and out on the custom edit cell in the "new row" it generates a new blank row in datatable (the grid isn''t displaying the new row, but he is in datatable with rowstate = added).

My question for you is, how to stop adding these new rows? What value should the cell contain when edit mode finishes so it won''t be considered as data entry (I''m usign DBNull and it doesn''t works, NULL is not allowed)? Or if the value isn''t the problem, what method or event should I use to cancel the row creation on the end of editing that cell?

It seems I cannot add the entire code here so I will add just the methods I think that has something to do with this problem
PS: CalendatEditingControl is a custom datetimepicker (wich allows DBNull values through BindableValue) and implements IDataGridViewEditingControl

public class CalendarCell : DataGridViewTextBoxCell
        {
            public CalendarCell() : base()
            {
                 this.Style.Format = "dd-MMM-yyyy";
            }
            public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
            {
                // Set the value of the editing control to the current cell value.
                base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
                CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
                if (this.RowIndex < 0 || this.ColumnIndex < 0) return;              
                if (this.Value == null || this.Value == DBNull.Value )
                {
                    ctl.BindableValue = DBNull.Value;
                }
                else
                {
                    ctl.BindableValue = (DateTime)this.Value;
                }
            }
            public override void DetachEditingControl()
            {
                CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
                if (this.Value == DBNull.Value && ctl.BindableValue == DBNull.Value )
                {
                   base.DetachEditingControl();
                   return;
                }
                if (this.Value != DBNull.Value && ctl.BindableValue == DBNull.Value) this.Value = DBNull.Value;
                if (ctl.BindableValue != null) this.Value = ctl.BindableValue;
                base.DetachEditingControl();
            }





public class CalendarEditingControl : ABT_DateTimePicker , IDataGridViewEditingControl
      {
....
protected override void OnValueChanged(EventArgs eventargs)
          {
              // Notify the DataGridView that the contents of the cell have changed.
              valueChanged = true;
              this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
              base.OnValueChanged(eventargs);
          }

推荐答案

您只需将"editIndex"值设为-1,即可将编辑模式转换为普通模式.
更新/删除/插入记录后尝试此操作

gridview.EditIndex = -1;

没关系.


问候,
瓦姆西(Vamsi)
you need to convert the edit mode into normal mode by just giving the "editIndex" value as -1.

Try this after you update/delete/insert your record

gridview.EditIndex = -1;

This should be fine..


Regards,
Vamsi


这篇关于自定义datagridview列上的数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆