0

我在与 DataGridView 作斗争:我确实有一些实现 INotifyPropertyChanged 的​​简单对象的 BindingList。DataGridView 的数据源设置为此 BindingList。现在我需要通过点击“+”键将一个对象添加到列表中。添加对象时,它应显示为新行并成为当前行。由于 CurrentRow 属性是只读的,我遍历所有行,检查它的绑定项是否是新创建的对象,如果是,我将此行设置为“ Selected = true;

问题:虽然在 DataGridView 中插入并选择了新对象和新行,但它仍然不是 CurrentRow!除非我在新行中单击鼠标,否则它不会成为 CurrentRow。

在这个测试程序中,您可以使用“+”键添加新对象(从而添加行),并使用“i”键将 CurrentRow 的数据绑定对象显示在 MessageBox 中。

如何使新添加的对象成为 CurrentObject?谢谢你的帮助!

这是示例:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        BindingList<item> myItems;

        public Form1()
        {
            InitializeComponent();
            myItems = new BindingList<item>();

            for (int i = 1; i <= 10; i++)
            {
                myItems.Add(new item(i));
            }

            dataGridView1.DataSource = myItems;
        }

        public void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Add)
            {
                addItem();
            }
        }

        public void addItem()
        {
            item i = new item(myItems.Count + 1);
            myItems.Add(i);
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                if (dr.DataBoundItem == i)
                {
                    dr.Selected = true;
                }
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            addItem();
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Add)
            {
                addItem();
            }
            if (e.KeyCode == Keys.I)
            {
                MessageBox.Show(((item)dataGridView1.CurrentRow.DataBoundItem).title);
            }
        }
    }

    public class item : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private int _id;
        public int id {
            get
            {
                return _id;
            }
            set
            {
                this.title = "This is item number " + value.ToString();
                _id = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("id"));
            }
        }

        private string _title;
        public string title {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("title"));
            }
        }

        public item(int id)
        {
            this.id = id;
        }

        #region Implementation of INotifyPropertyChanged
        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }

        #endregion
    }
}
4

1 回答 1

1

我在这篇文章中找到了答案:http ://www.c-sharpcorner.com/Forums/Thread/43898/

选择行是不够的,你必须设置 CurrentCell 使行成为 CurrentRow。所以要使上面的代码工作,你必须改变这个:

    public void addItem()
    {
        item i = new item(myItems.Count + 1);
        myItems.Add(i);
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            if (dr.DataBoundItem == i)
            {
                dr.Selected = true;
            }
        }
    }

对此:

    public void addItem()
    {
        item i = new item(myItems.Count + 1);
        myItems.Add(i);
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            if (dr.DataBoundItem == i)
            {
                dr.Selected = true;
                dataGridView1.CurrentCell = dr.Cells[0];
            }
        }
    }
于 2013-10-28T10:04:11.533 回答