Универсальный List<T>
не полностью поддерживает привязку к DataGridView
, так как вы можете видеть, что вы можете редактировать элементы в списке, но не добавлять и не удалять.
Вам нужно использовать либо BindingList<T>
, либо BindingSource
.
BindingList<T>
позволит вам использовать пользовательский интерфейс для добавления и удаления строк из сетки - когда вы измените DataSource
на это, вы увидите пустую новую строку в нижней части сетки. Вы все еще не можете программно добавлять или удалять строки. Для этого вам понадобится BindingSource
.
Пример обоих приведен ниже (используется пример класса Users, но детали этого здесь не важны).
public partial class Form1 : Form
{
private List<User> usersList;
private BindingSource source;
public Form1()
{
InitializeComponent();
usersList = new List<User>();
usersList.Add(new User { PhoneID = 1, Name = "Fred" });
usersList.Add(new User { PhoneID = 2, Name = "Tom" });
// You can construct your BindingList<User> from the List<User>
BindingList<User> users = new BindingList<User>(usersList);
// This line binds to the BindingList<User>
dataGridView1.DataSource = users;
// We now create the BindingSource
source = new BindingSource();
// And assign the List<User> as its DataSource
source.DataSource = usersList;
// And again, set the DataSource of the DataGridView
// Note that this is just example code, and the BindingList<User>
// DataSource setting is gone. You wouldn't do this in the real world
dataGridView1.DataSource = source;
dataGridView1.AllowUserToAddRows = true;
}
// This button click event handler shows how to add a new row, and
// get at the inserted object to change its values.
private void button1_Click(object sender, EventArgs e)
{
User user = (User)source.AddNew();
user.Name = "Mary Poppins";
}
}