c# datagridview绑定数据源(BindingList<class>)中的现象 待查

现象1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace datagridview
{
    public partial class Form1 : Form
    {
        public Int32 count = 0;
        public string str = "test!";
        BindingList<Item> items = new BindingList<Item>();

        public Form1()
        {
            InitializeComponent();

            //禁止自动创建列
            this.dataGridView1.AutoGenerateColumns = false;

            //将左边的一栏去掉
            this.dataGridView1.RowHeadersVisible = false;

            //允许用户交换列
            this.dataGridView1.AllowUserToOrderColumns = true;

            this.dataGridView1.DataSource = items;

            //创建“序号”列
            DataGridViewTextBoxColumn serial = new DataGridViewTextBoxColumn();
            serial.HeaderText = "序号";
            serial.DataPropertyName = "Serial";
            this.dataGridView1.Columns.Add(serial);

            //创建“文本”列
            DataGridViewTextBoxColumn text = new DataGridViewTextBoxColumn();
            text.HeaderText = "文本";
            text.DataPropertyName = "Text";
            this.dataGridView1.Columns.Add(text);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            items.Add(new Item(str, count++));
            //items.Add(new Item(0) { Text = str, Serial = count++ });

            //有时候使用DataGridView难免会在最后插入一条数据,如果插入的数据超过滚动条显示的行数,
            //那么默认情况下不会显示到最后一行。增加以下代码一直将滚动条拉倒最低。
            this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;
        }
    }

    class Item
    {
        private string _text;
        private int _serial;

        public string Text
        {
            get { return _text; }
        }

        public int Serial
        {
            get { return _serial; }
        }

        public Item(string text, int serial)
        {
            this._text = text;
            this._serial = serial;
        }

        //public string Text { get; set; }

        //public int Serial { get; set; }

        //public Item(int serial)
        //{
        //    int a = 0;
        //}
    }
}

运行结果:

当在Item类中增加默认构造函数后:

    class Item
    {
        private string _text;
        private int _serial;

        public string Text
        {
            get { return _text; }
        }

        public int Serial
        {
            get { return _serial; }
        }

        public Item(string text, int serial)
        {
            this._text = text;
            this._serial = serial;
        }

        public Item()
        {
            int a = 0;
        }

运行结果:

显示最后一栏空白,原因未知待查!

 

转载于:https://www.cnblogs.com/yeshenmeng/p/9884087.html

要在 DataGridView 使用 BindingList<> 修改数据,可以按照以下步骤操作: 1. 创建一个 BindingList<> 对象,并将其设置为 DataGridView数据源。 ``` BindingList<MyData> dataList = new BindingList<MyData>(); dataGridView.DataSource = dataList; ``` 2. 在 BindingList<> 添加数据。 ``` dataList.Add(new MyData { Name = "John", Age = 25 }); dataList.Add(new MyData { Name = "Jane", Age = 30 }); ``` 3. 在 DataGridView 编辑数据。 当用户双击单元格时,DataGridView 进入编辑模式。用户可以在编辑模式下修改单元格的值。如果你需要在用户完成编辑后更新 BindingList<> 数据,可以处理 DataGridView 的 `CellEndEdit` 事件,将修改后的值保存回 BindingList<>。 ``` private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) { MyData data = dataList[e.RowIndex]; DataGridViewCell cell = dataGridView[e.ColumnIndex, e.RowIndex]; string propertyName = dataGridView.Columns[e.ColumnIndex].DataPropertyName; object value = cell.Value; PropertyInfo property = typeof(MyData).GetProperty(propertyName); property.SetValue(data, Convert.ChangeType(value, property.PropertyType)); } ``` 这段代码会获取修改后的单元格值,并将其设置回 BindingList<> 的对应对象。 4. 在 DataGridView 删除数据。 用户可以通过选一行并按下 Delete 键来删除行。如果你需要在用户删除行后更新 BindingList<> 数据,可以处理 DataGridView 的 `UserDeletingRow` 事件,将要删除的数据BindingList<> 删除。 ``` private void dataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { MyData data = (MyData)e.Row.DataBoundItem; dataList.Remove(data); } ``` 这段代码会获取要删除的对象,并将其从 BindingList<> 删除。 这样,你就可以使用 BindingList<> 在 DataGridView 修改数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值