/// <summary> /// 单元格获取焦点事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Dgw_CellEnter(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex % 2 == 0) { return; } Dgw.BeginEdit(true); }
/// <summary> /// 抓取回车消息 /// </summary> /// <param name="msg"></param> /// <param name="keyData"></param> /// <returns></returns> protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { int _RowIndex = Dgw.CurrentCell.RowIndex; int _ColIndex = Dgw.CurrentCell.ColumnIndex; string strData = ""; if (keyData == Keys.Enter) { if (_RowIndex % 2 != 0) //必须是单行(数据行) { if ((_ColIndex + 1) % 4 == 0) //必须是文本框(表位文本框) { Dgw.EndEdit(); if (Dgw.CurrentCell.Value != null && Dgw.CurrentCell.Value.ToString().Trim() != string.Empty) { bool inputResult = false; try { strData = Dgw.CurrentCell.Value.ToString().Trim(); strData = strData.Substring(0, strData.Length - 1); inputResult = this.TxtInputOver((int)Dgw.CurrentCell.Tag, strData); } catch { } if (inputResult) { SetCheckBoxValue((int)Dgw.CurrentCell.Tag, true); //设置要捡 try { this.CheckOver((int)Dgw.CurrentCell.Tag, true); // } catch { } this.SelectedBwh = (int)Dgw.CurrentCell.Tag + 1; //设置焦点到下一个表位上 } else { Dgw.CurrentCell.Value = ""; Dgw.BeginEdit(true); //this.SelectedBwh = (int)Dgw.CurrentCell.Tag +1; //设置焦点到下一个表位上 //this.SelectedBwh = (int)Dgw.CurrentCell.Tag ; //设置焦点到下一个表位上 } return(true); //退出不换行 } else { Dgw.BeginEdit(true); return(true); //退出不换行 } } } } return(base.ProcessCmdKey(ref msg, keyData)); }
/// <summary> /// 鼠标按下,只处理CheckBox类型的单元格 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Dgw_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if (Dgw.CurrentCell is DataGridViewCheckBoxCell) { if (Dgw[Dgw.CurrentCell.ColumnIndex + 2, Dgw.CurrentCell.RowIndex].Value == null || Dgw[Dgw.CurrentCell.ColumnIndex + 2, Dgw.CurrentCell.RowIndex].Value.ToString() == string.Empty) { return; } Dgw.BeginEdit(false); } }
/// <summary> /// 鼠标抬起,只处理CheckBox类型的单元格 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Dgw_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { if (Dgw.CurrentCell is DataGridViewCheckBoxCell) { Dgw.EndEdit(); try { this.CheckOver((int)Dgw.CurrentCell.Tag, (bool)Dgw.CurrentCell.Value); } catch { } } }
/// <summary> /// 重绘行距行和列距行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Dgw_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { using ( Brush gridBrush = new SolidBrush(this.Dgw.GridColor), backColorBrush = new SolidBrush(Dgw.BackgroundColor) ) if (e.RowIndex % 2 == 0 && e.ColumnIndex >= 0) //清除能被2整除的行的单元格边框 { { using (Pen gridLinePen = new Pen(gridBrush)) { // 清除单元格 e.Graphics.FillRectangle(backColorBrush, e.CellBounds); // 画 Grid 边线(仅画单元格的底边线) if (e.ColumnIndex < Dgw.Columns.Count) { if (e.ColumnIndex % 4 != 0) //同时不画作为列距的边框 { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); } } e.Handled = true; } } } else if (e.RowIndex % 2 != 0 && e.ColumnIndex >= 0 && e.ColumnIndex % 4 == 0) //如果行号不能被2整除,同时列号能被4整除的单元格边框,因为该单元格为间距列 { { using (Pen gridLinePen = new Pen(gridBrush)) { // 清除单元格 e.Graphics.FillRectangle(backColorBrush, e.CellBounds); // 画 Grid 边线(仅画单元格的右边线) if (e.ColumnIndex < Dgw.Columns.Count - 1) //如果不是最后一列才画右边线 { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); } e.Handled = true; } } } Dgw.ClearSelection(); }
/// <summary> /// 单元格离开事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Dgw_CellLeave(object sender, DataGridViewCellEventArgs e) { Dgw.EndEdit(); }