基本信息
源码名称:c# DataGridView实时刷新
源码大小:0.03M
文件格式:.rar
开发语言:C#
更新时间:2019-04-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
c# DataGridView实时刷新

using System;
using System.Data;
using System.Windows.Forms;

namespace AhDung.Demos
{
    public partial class Form1 : Form
    {
        DataTable _table;

        public Form1()
        {
            InitializeComponent();

            _table = new DataTable();
            _table.Columns.Add("Col1", typeof(string));
            _table.Columns.Add("Col2", typeof(string), "Col1 '_Modified'");//计算列

            _table.Rows.Add("aaa");
            _table.Rows.Add("bbb");

            //指定哪些列使用自定的单元格类
            dataGridView1.Columns["Column1"].CellTemplate = new DataGridViewTextBoxUnSelectableCell();
            
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = _table;
        }

        //单元格键入时实时提交
        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                (dataGridView1.CurrentRow.DataBoundItem as DataRowView).EndEdit();//连带数据源一起提交
            }
        }

    }

    //定义一个单元格承载控件
    public class TextBoxUnSelectableEditingControl : DataGridViewTextBoxEditingControl
    {
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xb1)//忽略EM_SETSEL消息
            {
                return;
            }
            base.WndProc(ref m);
        }
    }

    //定义一个单元格类,重写此类的承载控件类型
    public class DataGridViewTextBoxUnSelectableCell : DataGridViewTextBoxCell
    {
        public override Type EditType
        {
            get
            {
                return typeof(TextBoxUnSelectableEditingControl);
            }
        }
    }

}