单元格编辑后在DataGridView中更改数据源 [英] Changing the datasource in a DataGridView after cell editing

查看:128
本文介绍了单元格编辑后在DataGridView中更改数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试更改DataGridView控件的数据源时,有人能为我指出可重入异常的正确方向.以下是我目前无法找到合适解决方案的问题的简化示例.我来自Visual Foxpro的背景,这种事情很容易实现.

我部分解决此问题的唯一方法是运行一个线程来更新数据源,但是由于这是异步的,因此很少有更新不会发生的可能性.另一种可行的方法是具有一个单独的按钮控件来对数据进行重新排序,但这需要用户按下它-一种更好的方法是在单元格更新后自动对其进行自动化.

我了解为什么在编辑另一个源的数据时更改数据源会使网格不高兴.我也知道可以将DataGridView设置为按列排序,但我要做的不只是对完整版本进行排序.

要运行下面的代码,请将DataGridView(将其命名为dgv1)添加到表单中.然后,要使异常发生,请将具有9的单元格更改为3,然后通过单击另一个单元格或使用箭头键将其移出该单元格.如果在突出显示另一个单元格之前按Enter键,则不会发生异常,但网格也不会重新排序.

Please could anyone point me in the right direction for the reentrant exceptions I''m getting when I try and change the datasource of a DataGridView control. Below is a cut down example of the problem that I am currently unable to find a suitable solution for. I come from a Visual Foxpro background where this sort of thing is easily possible.

The only way I''ve partially solved this is by running a thread to update the datasource but because this is asynchronous there is a small chance that the update will not happen. Another way that works is to have a separate button control to reorder the data but this involves the user pressing it - a much better way would be to automate it after the cell update.

I understand why the grid gets upset on changing a datasource when editing data of another source. I also know that the DataGridView can be set to sort on columns but I want to do more than just sort in my full version.

To run the code below add a DataGridView (naming it dgv1) to a form. Then to make the exception happen change the cell that has the 9 in to a 3 and then move off the cell by either clicking another cell or using the arrow keys. If enter is pressed before highlighting another cell then the exception doesn''t occur but nor does the grid get reordered.

// standard libraries added by ide
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace dgv_test
{
    public partial class Form1 : Form
    {
        private List<dv> grid;
        public Form1()
        {
            InitializeComponent();
            // this would be from a database
            List<dv> data = new List<dv>
                            {
                                new dv{ desc="t1", order=1},
                                new dv{ desc="t2", order=2},
                                new dv{ desc="t3", order=9},
                                new dv{ desc="t4", order=4},
                                new dv{ desc="t5", order=5},
                            };
            // in memory list
            grid =
                (from lu in data
                 orderby lu.order
                 select new dv
                 {
                     desc = lu.desc,
                     order = lu.order
                 }).ToList();

            // grid list
            dgv1.DataSource =
                (from g in grid
                 orderby g.order
                 select new dv
                 {
                     desc = g.desc,
                     order = g.order
                 }).ToList();
            // make description column readonly
            dgv1.Columns[0].ReadOnly = true;
        }
        private void dgv1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            // only update memory copy if order column changed
            if (dgv1.CurrentCellAddress.X == 1 && dgv1.IsCurrentCellDirty)
            {
                // grid is in memory copy of grid data
                grid.ElementAt(dgv1.CurrentCellAddress.Y).order = int.Parse(dgv1.CurrentCell.EditedFormattedValue.ToString());
                rgrid();
            }
        }
        // this is the function to update datagridview control
        private void rgrid()
        {
            // query the in memory list from the database
            var gx =
                (from g in grid
                 orderby g.order
                 select new dv
                 {
                     desc = g.desc,
                     order = g.order
                 }).ToList();
            // set the datagridview control with the newly ordered set
            dgv1.DataSource = gx.ToList();
            // do the same for the in memory list so that both are in alinement
            grid = gx.ToList();
        }
    }
    class dv
    {
        public string desc { get; set; }
        public int order { get; set; }
    }
}





Thanks in advance of any suggestions.

推荐答案

如果您将更新代码更改为CellValueChanged事件而不是CellLeave事件,则应该可以解决问题.

希望对您有帮助
If you change the update code to the CellValueChanged event instead of the CellLeave event this should cure your ills.

Hope this helps


这篇关于单元格编辑后在DataGridView中更改数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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