DataGridView的:保持选择数据源改变后? [英] DataGridView: Keep selections after datasource is changed?

查看:139
本文介绍了DataGridView的:保持选择数据源改变后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,以保持数据源改变后选择一个DataGridView的选定单元格?

Is there a way to keep the selected cells of a DataGridView selected after the DataSource is changed?

推荐答案

您可以匹配什么应该根据具体的您的需要和刚刚成立的任何单元格的选择财产或排至真/假取决于您的匹配标准来选择。这里有一个简单的例子,你可以在一个新创建的WinForms项目,将说明这一点下降。在这个例子中的工作确保您设置的DataGridView的的SelectionMode = FullRowSelect。如果您想保留/重新选择小区的做法有异曲同工之处。注意:您也可以只保留选定行的索引列表,但这个加载另一个数据源时,它通常是unliklely会有实际的行和数据的物理位置之间的任何信件通常是没有意义

You can match what should be selected based on criteria specific to your needs and just set the Select property of either the cell or row to true/false depending on your matching. Here's a simple example you can drop in a newly created winforms project that will illustrate the point. For this example to work make sure you set the DataGridView's SelectionMode = FullRowSelect. If you want to retain/reapply cell selections the approach would be similar. Note : you could also just retain a list of selected row indexes but this typically wouldn't make sense when loading another data source as it's usually unliklely that there would be any correspondence between the actual rows and their physical position in the data.

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; }
    }

}

要实施相同的功能选定单元格像做以下。我离开原来的代码应该谁都需要它。注:我只是敲出一个匿名类型在这里,你可能会需要根据成员字段,属性等,但设置选定单元格的一般原理正确说明做一些稍微更复杂,并能。很容易进行测试和调试的理解我希望

To implement the same functionality for selected cells do something like the following. I'm leaving the previous code should anyone else need it. Note: I'm just banging out an anonymous type here, you're probably going to need to do something a little more sophisticated depending on member fields, properties, etc. but the general principal of setting the selected cells is illustrated properly and can easily be tested and debugged for understanding I hope.

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; }
    }

}

这篇关于DataGridView的:保持选择数据源改变后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆