c# datagridview 有时出现大红叉,同时提示错误,如何解决?

时间:2022-12-09 07:54:51
图贴不会,请见谅!
出现红叉时,提示的错误框:

错误提示框: 尝试读取或写入受保护的内存,通常指示其他内存已损坏。报错的详细信息是:

clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
   在 System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
   在 System.Windows.Forms.DataGridViewTextBoxCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
   在 System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
   在 System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
   在 System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
   在 System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
   在 System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
   在 System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   在 System.Windows.Forms.Control.WmPaint(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.DataGridView.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 已加载的程序集 **************
mscorlib
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
EXCELINTOSQL
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0

12 个解决方案

#1


感觉是你的控件在建时就出错了

说实在还是不知道你的大红叉是什么样的

#2


我只在dev gridcontrol中遇到这个问题,
原来dataGridView也会出现这问题,,
我当初出现这问题好像是字段格式有问题就报那错。。

#3


请你检查在DGV上绑定的数据源,包括字段

#5


能看看代码么?没有提示在哪里出的错误么

#6


引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉

#7


引用 6 楼 yangliyang 的回复:
引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉



/**************************************************查询数据 **************************   */
        private void button2_Click(object sender, EventArgs e)
        {

           string var1 = "select 制造令号,机型,箱号,产品编号,状态,发货单号,发货日期 from kd" +textBox1.Text+ " where ";
           // string var1 = "select * from kd"+textBox1.Text+ " where ";

            if (textBox1.Text != "")
            {
                if (checkBox1.Checked)
                {
                    var1 += " 制造令号 = '" + textBox1.Text + "' and ";
                }
                if (checkBox2.Checked)
                {
                    var1 += " 箱号 = '" + textBox3.Text + "' and ";
                }
                else
                {
                    textBox3.Text = "";
                }
                if (checkBox3.Checked)
                {

                    var1 += " 产品编号 = '" + textBox4.Text + "' and ";
                }
                else
                {
                    textBox4.Text = "";
                }

                int a;
                a = var1.Length;
                var1 = var1.Substring(0, a - 5);
               // MessageBox.Show(var1);
                try
                {
                    sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
                    sqlda = new SqlDataAdapter(var1, sqlcon);//实例化数据库桥接器对象
                    myds = new DataSet();//实例化数据集
                    sqlda.Fill(myds);//填充数据集
                    dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());//显示错误信息
                }

            }
            else
            {
                MessageBox.Show(this, "制造令号为必添项,不能为空值!!", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }




        }

#8


引用 6 楼 yangliyang 的回复:
引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉

你是不是执行了不安全代码??

#9


引用 7 楼 yangliyang 的回复:
引用 6 楼 yangliyang 的回复:
引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉



/**************************************************查询数据 ******************……




代码如何改呢?

#10


如果之前是可以话,应该说程序本身是没有问题的!
是不是加载的数据量太大了!!

#11


这个我们也偶尔会碰到;是数据绑定到Grid时出的错;主要原因索引或者什么越界导致;可以查一查这方面的问题;

#12


引用 11 楼 windchenhonglong 的回复:
这个我们也偶尔会碰到;是数据绑定到Grid时出的错;主要原因索引或者什么越界导致;可以查一查这方面的问题;

小弟才疏学浅,越界?是什么意思

#1


感觉是你的控件在建时就出错了

说实在还是不知道你的大红叉是什么样的

#2


我只在dev gridcontrol中遇到这个问题,
原来dataGridView也会出现这问题,,
我当初出现这问题好像是字段格式有问题就报那错。。

#3


请你检查在DGV上绑定的数据源,包括字段

#4


#5


能看看代码么?没有提示在哪里出的错误么

#6


引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉

#7


引用 6 楼 yangliyang 的回复:
引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉



/**************************************************查询数据 **************************   */
        private void button2_Click(object sender, EventArgs e)
        {

           string var1 = "select 制造令号,机型,箱号,产品编号,状态,发货单号,发货日期 from kd" +textBox1.Text+ " where ";
           // string var1 = "select * from kd"+textBox1.Text+ " where ";

            if (textBox1.Text != "")
            {
                if (checkBox1.Checked)
                {
                    var1 += " 制造令号 = '" + textBox1.Text + "' and ";
                }
                if (checkBox2.Checked)
                {
                    var1 += " 箱号 = '" + textBox3.Text + "' and ";
                }
                else
                {
                    textBox3.Text = "";
                }
                if (checkBox3.Checked)
                {

                    var1 += " 产品编号 = '" + textBox4.Text + "' and ";
                }
                else
                {
                    textBox4.Text = "";
                }

                int a;
                a = var1.Length;
                var1 = var1.Substring(0, a - 5);
               // MessageBox.Show(var1);
                try
                {
                    sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
                    sqlda = new SqlDataAdapter(var1, sqlcon);//实例化数据库桥接器对象
                    myds = new DataSet();//实例化数据集
                    sqlda.Fill(myds);//填充数据集
                    dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());//显示错误信息
                }

            }
            else
            {
                MessageBox.Show(this, "制造令号为必添项,不能为空值!!", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }




        }

#8


引用 6 楼 yangliyang 的回复:
引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉

你是不是执行了不安全代码??

#9


引用 7 楼 yangliyang 的回复:
引用 6 楼 yangliyang 的回复:
引用 5 楼 xiaxin_xx 的回复:
能看看代码么?没有提示在哪里出的错误么

前段时间没事,就这几天 开始的,就是数据查询完后,在DGV显示完后,过一会,整个显示区一个大红叉



/**************************************************查询数据 ******************……




代码如何改呢?

#10


如果之前是可以话,应该说程序本身是没有问题的!
是不是加载的数据量太大了!!

#11


这个我们也偶尔会碰到;是数据绑定到Grid时出的错;主要原因索引或者什么越界导致;可以查一查这方面的问题;

#12


引用 11 楼 windchenhonglong 的回复:
这个我们也偶尔会碰到;是数据绑定到Grid时出的错;主要原因索引或者什么越界导致;可以查一查这方面的问题;

小弟才疏学浅,越界?是什么意思