DataGridView绑定DataTable出现大红叉

窗口名称:BoxItemRelationQuery

一、用新线程的方法打开窗口

 private void btnBoxItemRelationSelect_Click(object sender, EventArgs e)
        {
            Thread td = new Thread(new ThreadStart(BoxItemRelationQuery));
            td.Start();
            //BoxItemRelationQuery();
        }

        delegate void myDelegate_BoxItemRelation();//定义委托
        void BoxItemRelationQuery()
        {
            //if (this.InvokeRequired)
            //{
            //    this.BeginInvoke(new myDelegate_BoxItemRelation(BoxItemRelationQuery), new object[] { });
            //}
            //else
            //{

            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == "BoxItemRelationSelect")
                {
                    //frm.Close();
                    ui_set_BoxItemRelationSelect(frm);
                    break;
                }
            }
            
            BoxItemRelationSelect birs = new BoxItemRelationSelect();
            birs.ShowDialog();
            //}           
        }


1、查询事件

private void btn_getOrder_Click(object sender, EventArgs e)
        {

            Thread td = new Thread(new ParameterizedThreadStart(DoQuery));

            string boxCode = txtBoxCode.Text.Trim();
            string itemCode = txtItemCode.Text.Trim();
            td.Start(new string[] { boxCode, itemCode });
            
        }

void DoQuery(object obj)
        {
            string [] str_array=(string[])obj;
             string boxCode=str_array[0];
             string itemCode=str_array[1];
            //if (this.InvokeRequired)
            //{
            //    this.Invoke(new myDelegate(DoQuery), new object[] { });
            //}
            //else
            //{
                try
                {
                    
                    
                    if (string.IsNullOrWhiteSpace(boxCode) && string.IsNullOrWhiteSpace(itemCode))
                        return;
                    if (!string.IsNullOrWhiteSpace(boxCode))
                    {
                        if (boxCode.Length != 49 && boxCode.Length != 51)
                        {
                            MessageBox.Show("箱码长度不正确,请输入49或51位箱码");
                            return;
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(itemCode))
                    {
                        if (itemCode.Length != 16)
                        {
                            MessageBox.Show("瓶码长度不正确,请输入16位数字");
                            return;
                        }
                    }
                    string sql = "select box_barcode as BoxCode,item_barcode as ItemCode from qx_bundle_px_temp where 1=1";
                    if (boxCode != "")
                        sql += " and box_barcode='" + boxCode + "'";
                    if (itemCode != "")
                    {
                        string fwHttp = "http://fangwei.copton.com.cn:8011/identify/verify.jsp?SecCode=";
                        sql += " and item_barcode='" + fwHttp + itemCode + "'";
                    }
                    DataSet ds = DbHelperSQL.Query(sql);
                    dgvData.DataSource = ds.Tables[0];
                    lblShowAmount.Text = "共:" + ds.Tables[0].Rows.Count.ToString() + " 条数据";
                }
                catch (Exception ex)
                {
                    AddErrorLog("瓶箱关联查询", "瓶箱关联查询错误" + ex.Message + "," + ex.StackTrace, "瓶箱关联查询", "瓶箱关联查询错误" + ex.Message + "," + ex.StackTrace);
                    MessageBox.Show("数据查询错误:" + ex.Message);
                }
            //}
        }
        private void btn_getOrder_Click(object sender, EventArgs e)
        {

            Thread td = new Thread(new ParameterizedThreadStart(DoQuery));

            string boxCode = txtBoxCode.Text.Trim();
            string itemCode = txtItemCode.Text.Trim();
            td.Start(new string[] { boxCode, itemCode });
            
        }

2、使用CheckForIllegalCrossThreadCalls = false;防止跨线程交互报错

public BoxItemRelationSelect()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

查询第二次报错如下错:

DataGridView绑定DataTable出现大红叉_第1张图片

二、不用线程打开窗口

第二次查询时datagridview出现大红叉

DataGridView绑定DataTable出现大红叉_第2张图片

解决方案:

datagirdview非创建线程中绑定数据时用委托方法,“CheckForIllegalCrossThreadCalls = false”并不安全

改成下面代码就没问题了

                    DataSet ds = DbHelperSQL.Query(sql);
                    dgvData.DataSource = ds.Tables[0];
                    ui_set_datagridview(dgvData, ds);
                    //lblShowAmount.Text = "共:" + ds.Tables[0].Rows.Count.ToString() + " 条数据";
                    ui_settext_Label(lblShowAmount, "共:" + ds.Tables[0].Rows.Count.ToString() + " 条数据");

        delegate void myDelegate_ui_settext_Label(Label tb, string str);//定义委托
        void ui_settext_Label(Label tb, string str)
        {
            if (tb.InvokeRequired)
            {
                tb.Invoke(new myDelegate_ui_settext_Label(ui_settext_Label), new object[] { tb, str });
            }
            else
            {
                tb.Text = str;
            }

        }
        delegate void myDelegate_ui_set_datagridview(DataGridView dgv, DataSet  ds);//定义委托
        void ui_set_datagridview(DataGridView dgv, DataSet ds)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new myDelegate_ui_set_datagridview(ui_set_datagridview), new object[] { dgv, ds });
            }
            else
                dgv.DataSource = ds.Tables[0];

        }




你可能感兴趣的:(C#,WinForm,DataGridView,大红叉,c#)