访问EditedFormattedValue时,Datagridview单元格变黑?

时间:2010-07-08 20:36:21

标签: vb.net winforms datagridview

我有一个使用datagridview的winforms应用程序。当我在编辑模式下访问单元格的EditedFormattedValue时,有时整个单元格变为黑色。这是datagridview的一个功能,也许是我需要关闭的某种验证功能吗?

我在google搜索时发现了其他一些关于此问题的提及。没有一个线程似乎知道答案是什么:

这里没有调用Application.DoEvents解决了这个问题,但我没有调用DoEvents http://www.tek-tips.com/viewthread.cfm?qid=1277303&page=1

http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/d9bf784d-617d-4a35-8125-fe8f67987fa2
这里没有人知道它为什么会发生,但响应者建议将背景颜色设置为白色。这似乎是一个黑客,我想知道为什么会发生这种情况。此外,更改背景颜色不会删除黑色背景。

由barlop添加

enter image description here

请注意还有另一个原因,但由于这里的标题是编辑格式化的,我首先会注意到这个例子。

将EditedFormattedValue作为原因的示例。

(双击单元格时(因此进入编辑模式),它会变黑)

namespace simpleblackdgv2
{
    public partial class Form1 : Form
    {
        DataGridView dataGridView1 = new DataGridView();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            // BUG IS THAT WHEN YOU DOUBLE CLICK A CELL, IT GOES BLACK


            dataGridView1.Columns.Add("Column1", "Column1");

            dataGridView1.Rows.Add(1);

            dataGridView1.AllowUserToAddRows = false;


            this.Controls.Add(dataGridView1);


            dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;

        }

        private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

            // doesn't work
             string str = dataGridView1.CurrentCell.EditedFormattedValue.ToString();



        }


        }
    }

在相关的说明中,这是它变黑的另一个例子,虽然不是来自editedformattedvalue(此问题的先前编辑有一个更长的例子,这是一个更简单的切割它)。当常规自动调整大小设置打开时,该错误显示一个问题 - 设置为allcells,并且特定的一个设置为None,然后根据您对语句的排序方式,它可能会出错,这必定是一个错误。设置任何特定的自动调整大小时,将常规设置为关闭。

与调整大小相关的黑色示例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace blackdgvcellexample {

public partial class Form1 : Form {

    DataGridView dataGridView1;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        // autosize rows may be meaningless without that.
        //dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

        dataGridView1 = new DataGridView();
        this.Controls.Add(dataGridView1);

        dataGridView1.Columns.Add("Column1", "Column1");
        dataGridView1.Columns.Add("Column2", "Column2");
        dataGridView1.Columns.Add("Column3", "Column3");

        dataGridView1.AllowUserToAddRows = false;


         dataGridView1.Rows.Add(5);
        //dataGridView1.Rows.Add(1);

        dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;


    }


    private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {



        dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;


        //dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A
        // dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; //B
        // dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; //C

        // B-Cols, C-rows
        // A, ~B, ~C  WORKS
        // A,B, ~C  WORKS
        // A,~B,C  WORKS
        // A,B,C   WORKS
        // SO IT WAS THE ORDER OF THE LINES THAT MAKES A DIFFERENCE!!!!


        // --- trying not turning any off
        //            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A
        //           dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; //B        
        //            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; //C

        // Cols, Rows, A  BLACK FIRST CELL REST FINE
        // rows, Cols, A   BLACK FIRST CELL REST FINE
        // cols,A,rows   FINE
        // rows,A,cols  BLACK FIRST CELL REST FINE
        // A, rows, cols   BLACK FIRST CELL
        // A, cols, rows  FINE

        // perhaps unnecessary to autoaize rows. 

        dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A

        dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; 
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; 


        // this works
        /*

dataGridView1.Columns [0] .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //一个
dataGridView1.AutoSizeRowsMo​​de = DataGridViewAutoSizeRowsMo​​de.AllCells; * /

        /* 
         * this works
        dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A            
        */

        /*
          doesn't work
        dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A            
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; 
          */

        // doesn't work
        //   dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        //   dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A            

        // so don't mix them. ok

        // this doesn't work

        //            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
        //          dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A            


        // these 3 work in any order
        //  dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
        //  dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
        //  dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //A            

        // don't mix them. 
        // if you are going to mix them, then turn the generals off and do specifics.
        // this won't work
        //  dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        //  dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        //  dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; //A            

        // this works, probably any order too.
        /*
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
        dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
        dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;           
        dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;           
        dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        */


    }



}


}

related- https://connect.microsoft.com/VisualStudio/feedback/details/2878864/datagridview-cell-goes-black

2 个答案:

答案 0 :(得分:3)

好吧,虽然我无法弄清楚为什么str=dataGridView1.CurrentCell.EditedFormattedValue会发生这种情况,但我发现使用str=dataGridView1.EditingControl.Text值会得到相同的值并且不会弄乱单元格格式。所以,我想这解决了我的问题。

答案 1 :(得分:1)

现在提到这个问题

https://connect.microsoft.com/VisualStudio/feedback/details/2878864/datagridview-cell-goes-black

除了dvcolgan提到的解决方法,它仅适用于editedformattedvalue的情况。

在ms bug链接中提到的viorel有一个解决方法,我已经测试并且工作,这是用

包围代码
 BeginInvoke( new Action( ( ) =>
     {
        . . .
     } ) );

上述BeginInvoke技术适用于两种情况。