C# DataGridView使用特殊情况处理

6 篇文章 0 订阅

 

DataGridView CellFormatting单元格格式化

private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id"));
            dt.Columns.Add(new DataColumn("Name"));
            dt.Columns.Add(new DataColumn("Bit"));
            dt.Rows.Add(new object[] { "1", "abcd", "0" });
            dt.Rows.Add(new object[] { "2", "abcd", "1" });
            dt.Rows.Add(new object[] { "3", "abcd", "0" });
            dt.Rows.Add(new object[] { "4", "abcd", "1" });
            dt.Rows.Add(new object[] { "5", "abcd", "1" });
            dt.Rows.Add(new object[] { "6", "abcd", "0" });
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;
            dataGridView1.CellFormatting += dataGridView1_CellFormatting;
        }


        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (Column_bit.Index == e.ColumnIndex)
            {
                if (e.Value == null)
                {
                    return;
                } if (e.Value.Equals("0"))
                {
                    e.Value = "否";
                }
                else
                {
                    e.Value = "是";
                }
            }
        }

DataGridView 在多线程中使用可能出现大红叉

老外的解释:

The DataGridView is a common .Net control used to display and permit editing of tabular data. It can be filled in via code or by attaching a data source to it. 
DataGridViews, like most controls, are not thread-safe. That is, you need to perform operations on them using the same thread as they were created on. However, sometimes OpenSpan developers will accidently modify the DataGridView from another thread because the operation is coming from an event in another application. 
A safe way to correct this problem and to ensure it doesn't accidently happen is to postpone updating the control during a OnPaint() event if it happens from another thread. This is done by handling an exception and then flagging the control to be redrawn when the MessagePump is run. You can do this by creating a .Net class that inherits from DataGridView and overrides the OnPaint() event. You will need access to a C# compiler to build this. Once built, you add it to the OpenSpan Studio Toolbox and replace your DataGridView controls with it.

处理的方法,重新封装DataGridView 控件,重新OnPaint 方法,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;



namespace wstest
{
    class DataGridViewForWs : DataGridView
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
            }
            catch
            {

                Invalidate();
            }
        }

    }
}


--------------------- 
作者:夜游神一郎 
来源:CSDN 
原文:https://blog.csdn.net/yeyoushengyilang/article/details/22979665 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@David Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值