日期:2014-05-18 浏览次数:21069 次
private void dgv_Data2_CellEndEdit(object sender, DataGridViewCellEventArgs e) { IncellEdit = true; try { ocnn = new OleDbConnection(connections); ocnn.Open(); for (int i = 0; i < dgv_Data2.Rows.Count - 1; i++) { for (int j = 0; j < dgv_Data2.Rows.Count - 1; j++) { string value1 = Convert.ToString(dgv_Data2.Rows[i].Cells[2].Value); string value2 = Convert.ToString(dgv_Data2.Rows[j].Cells[2].Value); if (i != j && value1 == value2) { MessageBox.Show("此货物已当前列表中!", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Information); [color=#FF0000]PushDelIndex(e.RowIndex); Thread delTh = new Thread(new ThreadStart(delRowThread)); delTh.Start();[/color]// return; } else { string sql = "select * from MIS_MaterielRegiser where MR_Name='" + value1 + "'"; ocmd = new OleDbCommand(sql, ocnn); OleDbDataReader reader = ocmd.ExecuteReader(); reader.Read(); dgv_Data2.Rows[i].Cells[1].Value = reader["MR_Number"].ToString(); dgv_Data2.Rows[i].Cells[3].Value = reader["MR_Model"].ToString(); dgv_Data2.Rows[i].Cells[4].Value = reader["MR_Unit"].ToString(); } } } ocnn.Close(); } catch (Exception) { MessageBox.Show("请从货物名称中选择一项!"); } }
private int[] delIndex = null;//待删行号的数组 private bool delFlag = true;//是否要不间断尝试删除数组中的行 private bool deling = false;//是否有线程正在执行删除操作的锁变量 private void PushDelIndex(int _index) { if(delIndex==null) { delIndex=new int[]{_index}; } else { Array.Resize(ref delIndex,delIndex.Length+1);//展示如何扩充数组 delIndex[delIndex.Length - 1] = _index; } } private void delRowThread() { while (delFlag) { if (delIndex != null) { if (deling == false) { delRow(); } } Thread.Sleep(100); } } //此处展现了如何在非创建控件的线程里去修改控件的方法 private delegate void delRowCallBack(); private void delRow() { if (dgv_Data2.InvokeRequired) { deling = true; delRowCallBack o = new delRowCallBack(delRow); this.Invoke(o); } else { for (int i = 0; i < delIndex.Length; i++) { dgv_Data2.Rows.RemoveAt(delIndex[i]); } deling = false;