有没有办法在更改DataSource后保持选定的DataGridView单元格?
答案 0 :(得分:5)
您可以根据特定于您需要的条件匹配应选择的内容,并根据匹配情况将单元格或行的Select属性设置为true / false。这是一个简单的例子,你可以放入一个新创建的winforms项目,这将说明这一点。要使此示例正常工作,请确保设置DataGridView的SelectionMode = FullRowSelect。如果要保留/重新应用单元格选择,则方法类似。注意:您也可以保留一个选定的行索引列表,但这通常在加载另一个数据源时没有意义,因为通常不显眼的是实际行与它们在数据中的物理位置之间会有任何对应关系。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DataGridViewRetainSelection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private readonly List<Person> currentPeople = new List<Person>();
private bool dummyToggle = true;
private void Form1_Load(object sender, EventArgs e)
{
SwitchDataSource(); // will just new up the datasource
}
private void ButtonSwitchDataSourceClick(object sender, EventArgs e)
{
SwitchDataSource();
}
private void SwitchDataSource()
{
var selectedPeople = (from DataGridViewRow row in dataGridViewPeople.Rows where row.Selected select currentPeople[row.Index]).ToList();
peopleBindingSource.DataSource = null;
currentPeople.Clear();
if (dummyToggle)
{
currentPeople.Add(new Person { Name = "Joel Spolsky" });
currentPeople.Add(new Person { Name = "Jeff Atwood" });
currentPeople.Add(new Person { Name = "Jarrod Dixon" });
currentPeople.Add(new Person { Name = "Geoff Dalgas" });
currentPeople.Add(new Person { Name = "Brent Ozar" });
}
else
{
currentPeople.Add(new Person { Name = "Joel Spolsky" });
currentPeople.Add(new Person { Name = "Jeff Atwood" });
currentPeople.Add(new Person { Name = "Brent Ozar" });
}
dummyToggle = !dummyToggle;
peopleBindingSource.DataSource = currentPeople;
foreach (var person in selectedPeople)
{
foreach (DataGridViewRow row in dataGridViewPeople.Rows)
{
if (string.Equals(row.Cells[0].Value, person.Name))
{
row.Selected = true;
}
}
}
}
}
public sealed class Person
{
public string Name { get; set; }
}
}
要为选定的单元格实现相同的功能,请执行以下操作。如果其他人需要,我将离开之前的代码。注意:我只是在这里敲打一个匿名类型,你可能需要根据成员字段,属性等做一些更复杂的事情,但设置所选单元格的一般原则是正确说明的,并且可以我希望能够轻松地进行测试和调试,以便理解。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DataGridViewRetainSelection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private readonly List<Person> currentPeople = new List<Person>();
private bool dummyToggle = true;
private void Form1_Load(object sender, EventArgs e)
{
dataGridViewPeople.SelectionMode = DataGridViewSelectionMode.CellSelect;
SwitchDataSource(); // will just new up the datasource
}
private void ButtonSwitchDataSourceClick(object sender, EventArgs e)
{
SwitchDataSource();
}
private void SwitchDataSource()
{
var selectedPeopleAndCells = (from DataGridViewCell cell in dataGridViewPeople.SelectedCells where cell.Selected select new { Person = currentPeople[cell.RowIndex], Cell = cell }).ToList();
peopleBindingSource.DataSource = null;
currentPeople.Clear();
if (dummyToggle)
{
currentPeople.Add(new Person { Name = "Joel Spolsky", Id = 0 });
currentPeople.Add(new Person { Name = "Jeff Atwood", Id = 1 });
currentPeople.Add(new Person { Name = "Jarrod Dixon", Id = 2 });
currentPeople.Add(new Person { Name = "Geoff Dalgas", Id = 3 });
currentPeople.Add(new Person { Name = "Brent Ozar", Id = 4 });
}
else
{
currentPeople.Add(new Person { Name = "Joel Spolsky", Id = 0 });
currentPeople.Add(new Person { Name = "Jeff Atwood", Id = 1 });
currentPeople.Add(new Person { Name = "Brent Ozar", Id = 4 });
}
dummyToggle = !dummyToggle;
peopleBindingSource.DataSource = currentPeople;
foreach (var personAndCell in selectedPeopleAndCells)
{
foreach (DataGridViewRow row in dataGridViewPeople.Rows)
{
if (string.Equals(row.Cells[0].Value, personAndCell.Person.Id))
{
row.Cells[personAndCell.Cell.ColumnIndex].Selected = true;
}
}
}
}
}
public sealed class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
}