//7.文本过滤 void editTextBox_TextChanged(object sender, EventArgs e) { if (CurrentCell != null) { if (ColumnIsBindSelectionCard(CurrentCell.ColumnIndex)) { if (textpanel.Visible == false) { return; } } else { return; } } DataGridViewSelectionCard selectionCardInfo = (DataGridViewSelectionCard)textpanel.Tag; //textpager.IsPage = selectionCardInfo.IsPage; if (textpager.IsPage == false) { textpager.totalRecord = selectionCardInfo.PageTotalRecord; } if (textpanel.Visible == true && selectionCardInfo.IsPage == true) { //过滤选项卡数据 string filterString = ""; string s1 = "%"; string s2 = "%"; string keyWord = FormatKeyword(editTextBox.Text); if (!pressSpaceKey) { if (selectionCardInfo.SelectCardFilterType == MatchModes.ByFirstChar) { s1 = ""; } filterString = ""; if (selectionCardInfo.QueryFields != null) { for (int i = 0; i < selectionCardInfo.QueryFields.Length - 1; i++) { filterString += selectionCardInfo.QueryFields[i] + " like '" + s1 + keyWord + s2 + "' or "; } filterString += selectionCardInfo.QueryFields[selectionCardInfo.QueryFields.Length - 1] + " like '" + s1 + keyWord + s2 + "'"; } } else { filterString = ""; if (selectionCardInfo.QueryFields != null) { for (int i = 0; i < selectionCardInfo.QueryFields.Length - 1; i++) { filterString += selectionCardInfo.QueryFields[i] + " = '" + keyWord + "' or "; } filterString += selectionCardInfo.QueryFields[selectionCardInfo.QueryFields.Length - 1] + " = '" + keyWord + "'"; } } if (_source != null) { DataRow[] drs = _source.Select(filterString); DataTable dt = _source.Clone(); for (int i = 0; i < drs.Length; i++) { dt.Rows.Add(drs[i].ItemArray); } this.textpager.DataSource = dt; if (dt.Rows.Count > 0) { textdataGrid.CurrentCell = textdataGrid[GetSelectCardVisableColumnIndex(textdataGrid), 0]; } } } if (selectionCardInfo.IsPage == false) { textpager.pageNo = 1; } }
/// <summary> /// 网格单元格编辑 /// </summary> /// <param name="e"></param> //1.显示ShowCard protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) { int columnIndex = this.CurrentCell.ColumnIndex; if (e.Control.GetType() == typeof(System.Windows.Forms.DataGridViewTextBoxEditingControl)) { editTextBox = (TextBox)e.Control; if (editTextBoxKeyPressEventHandler == null) { editTextBoxKeyPressEventHandler = new KeyPressEventHandler(editTextBox_KeyPress); editTextBox.KeyPress += editTextBoxKeyPressEventHandler; } if (editTextBoxTextChangeEventHandler == null) { editTextBoxTextChangeEventHandler = new EventHandler(editTextBox_TextChanged); editTextBox.TextChanged += editTextBoxTextChangeEventHandler; } if (Columns[CurrentCell.ColumnIndex].GetType() == typeof(DataGridViewTextBoxColumn)) { editTextBox.MaxLength = ((DataGridViewTextBoxColumn)Columns[CurrentCell.ColumnIndex]).MaxInputLength; } //设定绑定的对应的选项卡的位置 if (ColumnIsBindSelectionCard(columnIndex)) { DataGridViewSelectionCard selectionCardInfo = (DataGridViewSelectionCard)textpanel.Tag; textpager.IsPage = selectionCardInfo.IsPage; this.textpager.DataSource = _source; if (textpager.IsPage == false) { textpager.totalRecord = selectionCardInfo.PageTotalRecord; } if (PageNoChanged != null) { textpager.PageNoChanged -= new PagerEventHandler(textpager_PageNoChanged); textpager.PageNoChanged += new PagerEventHandler(textpager_PageNoChanged); textpager.pageNo = 1; } SetSelectCardLocation(columnIndex); string oldString = editTextBox.Text; editTextBox.Text = oldString; if (hideSelectionCardWhenCustomInput == false) { textpanel.Show(); } else { textpanel.Hide(); } } else { textpanel.Hide(); } } }
//4.按键控制 protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) { if (textpanel.Visible == true) { DataGridViewSelectionCard selectCardInfo = ((DataGridViewSelectionCard)textpanel.Tag); DataGridViewColumn currentColumn = (DataGridViewColumn)Columns[CurrentCell.ColumnIndex]; #region up and down key if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right) { if (keyData == Keys.Left || keyData == Keys.Right) { return(true); } if (textdataGrid.Rows.Count == 0) { return(true); } int selectionCardColIndex = textdataGrid.CurrentCell.ColumnIndex; int selectionCardRowIndex = textdataGrid.CurrentCell.RowIndex; if (keyData == Keys.Up) { if (selectionCardRowIndex > 0) { selectionCardRowIndex = selectionCardRowIndex - 1; } else { selectionCardRowIndex = 0; } textdataGrid.CurrentCell = textdataGrid[selectionCardColIndex, selectionCardRowIndex]; } else { if (selectionCardRowIndex == textdataGrid.Rows.Count - 1) { selectionCardRowIndex = textdataGrid.Rows.Count - 1; } else { selectionCardRowIndex = selectionCardRowIndex + 1; } textdataGrid.CurrentCell = textdataGrid[selectionCardColIndex, selectionCardRowIndex]; } return(true);//返回true,控件不响应键盘事件 } #endregion #region enter key if (keyData == Keys.Enter) { //触发选择事件并跳转 if (textdataGrid.Rows.Count == 0) { return(true); } else { //判断是否是空行 bool isEmptyRow = true; for (int i = 0; i < textdataGrid.Columns.Count; i++) { int keyRow = textdataGrid.CurrentRow.Index; if (textdataGrid[i, keyRow].Value != null && textdataGrid[i, keyRow].Value.ToString().Trim() != "") { isEmptyRow = false; break; } } if (isEmptyRow) { return(true); } } bool stopJump = false; //辅助参数,用户返回用户是否决定跳转 int customNextColumnIndex = CurrentCell.ColumnIndex; //设置用户可能定义的下一个跳转列 if (SelectCardRowSelected != null) { SelectCardRowSelected(GetReturnValue(), ref stopJump, ref customNextColumnIndex); } textpanel.Hide(); this.EndEdit(); if (!stopJump) { if (customNextColumnIndex == CurrentCell.ColumnIndex) { //如果用户没有指定自定义的列,按可编辑列顺序跳转 ColumnJumpControl(CurrentCell.ColumnIndex, CurrentCell.RowIndex); } else { //跳转到指定的列 this.CurrentCell = this[customNextColumnIndex, CurrentCell.RowIndex]; } } return(true); } #endregion #region page key if (keyData == Keys.PageUp || keyData == Keys.PageDown) { if (keyData == Keys.PageDown) { textpager.pageNo += 1; } if (keyData == Keys.PageUp) { textpager.pageNo -= 1; } return(true); } #endregion #region space if (keyData == Keys.Space) { pressSpaceKey = true; editTextBox_TextChanged(null, null); pressSpaceKey = false; return(true); } #endregion } else { if (keyData == Keys.Enter && CurrentCell != null) { ColumnJumpControl(CurrentCell.ColumnIndex, CurrentCell.RowIndex); return(true); } if ((int)keyData >= 48 && (int)keyData <= 57 && CurrentCell != null) { if (ColumnIsBindSelectionCard(CurrentCell.ColumnIndex)) { if (textpanel.Visible == false) { return(true); } } } } return(base.ProcessCmdKey(ref msg, keyData)); }
void editTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (CurrentCell.IsInEditMode) { if (textpanel.Visible == true && ColumnIsBindSelectionCard(CurrentCell.ColumnIndex)) { bool showCardVisable = true; DataGridViewSelectionCard selectCardInfo = ((DataGridViewSelectionCard)textpanel.Tag); if (((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57) && showCardVisable == true) { bool isEmptyRow = true; for (int i = 0; i < textdataGrid.Columns.Count; i++) { int keyRow = Convert.ToInt32(e.KeyChar.ToString()) - 1; if (keyRow < 0) { keyRow = 9; } if (keyRow > textdataGrid.Rows.Count - 1) { isEmptyRow = true; break; } if (textdataGrid[i, keyRow].Value != null && textdataGrid[i, keyRow].Value.ToString() != "") { isEmptyRow = false; break; } } if (!isEmptyRow) { int rowIndex = Convert.ToInt32(e.KeyChar.ToString()) - 1; if (rowIndex == -1) { rowIndex = 9; } if (rowIndex > textdataGrid.Rows.Count - 1) { e.Handled = true; return; } textdataGrid.CurrentCell = textdataGrid[GetSelectCardVisableColumnIndex(textdataGrid), rowIndex]; this.Focus(); e.Handled = true; this.EndEdit(); System.Windows.Forms.Message msg = new System.Windows.Forms.Message(); ProcessCmdKey(ref msg, System.Windows.Forms.Keys.Enter); } else { e.Handled = true; return; } } } else { int keyAsc = (int)e.KeyChar; if (Columns[CurrentCell.ColumnIndex].GetType() != typeof(DataGridViewTextBoxColumn)) { e.Handled = false; return; } if (keyAsc == 8 || keyAsc == 13) { e.Handled = false; } } } else { if (textpanel.Visible) { textpanel.Hide(); } } }