Winfrom DataGridView DataGridViewComboBoxCell 数据源绑定

Winfrom DataGridView DataGridViewComboBoxCell 数据源绑定解决方案:1.先给数据源赋值List<BindDataModel> ProcessStatus = new List<BindDataModel> { new BindDataModel() { _name = "生产中", _value = "生产中" }, new BindDataModel() { _name
摘要由CSDN通过智能技术生成

Winfrom DataGridView DataGridViewComboBoxCell 数据源绑定

解决方案:
1.先给数据源赋值
List<BindDataModel> ProcessStatus = new List<BindDataModel>
        {
   
            new BindDataModel() {
    _name = "生产中", _value = "生产中" },
            new BindDataModel() {
    _name = "暂停", _value = "暂停" 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 WinForm 中,可以通过双向数据来实现 DataGridView 控件与数据之间的数据同步。当数据的值发生改变时,DataGridView 控件会自动刷新显示;当用户在 DataGridView 中修改了数据时,数据的值也会自动更新。 以下是实现双向数据的步骤: 1. 义一个实体类,来表示要数据对象,该实体类的属性应该和 DataGridView 中的列一一对应。 ```C# public class Person { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } } ``` 2. 在 Form 中创建 DataGridView 控件,并设置其 DataSource 属性为 BindingSource 的实例。 ```C# BindingSource personSource = new BindingSource(); personSource.DataSource = typeof(Person); dataGridView1.DataSource = personSource; ``` 3. 创建一个 Person 对象的实例,并将其到 BindingSource 中。 ```C# Person person = new Person() { Name = "Tom", Age = 20, Gender = "Male" }; personSource.Add(person); ``` 4. 在需要进行双向数据DataGridView 列上,设置其 DataPropertyName 属性为 Person 对象的属性名。 ```C# dataGridView1.Columns["Name"].DataPropertyName = "Name"; dataGridView1.Columns["Age"].DataPropertyName = "Age"; dataGridView1.Columns["Gender"].DataPropertyName = "Gender"; ``` 5. 如果需要在 DataGridView 中修改数据,可以通过 DataGridView 中的 CurrentCellDirtyStateChanged 事件和 CellValueChanged 事件来实现数据的更新。 ```C# private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dataGridView1.IsCurrentCellDirty) { dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); } } private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { Person person = personSource.Current as Person; personSource.EndEdit(); } } ``` 以上就是在 WinForm 中实现双向数据的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值