internal void TextBoxDplDgv_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Down) { DgvEx.Focus(); //DgvExShow(); } if (e.KeyCode == Keys.Enter) { if (DgvEx.Visible) { DgvEx.Focus(); if (DgvEx.Rows.Count > 0 && DgvEx.CurrentRow.Index >= 0) { TextBoxBindValue(); } } else { if (this.SelectNextControlFlag) { this.Parent.SelectNextControl(this, true, true, true, true); } } } }
/// <summary> /// 给TextBox赋值 /// </summary> internal void TextBoxBindValue() { this.dataGridViewRow = DgvEx.CurrentRow; if (!string.IsNullOrEmpty(DisplayMember) && !string.IsNullOrEmpty(ValueMember)) { this.Tag = DgvEx.CurrentRow.Cells[ValueMember].Value.ToString().Trim(); this.Text = DgvEx.CurrentRow.Cells[DisplayMember].Value.ToString().Trim(); } else { foreach (Control ctl in DgvForm.Controls) { ControlSetValue(ctl); } //DgvEx.Hide(); this.Text = thisText; } DgvEx.Hide(); this.Focus(); if (textBoxDplDgv_Selected != null) { textBoxDplDgv_Selected(this, new EventArgs()); } if (SelectNextControlFlag) { this.Parent.SelectNextControl(this, true, true, true, true); } }
/// <summary> /// parentControl离开DataGridView以外按鼠标左右键DataGridView隐藏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> internal void parentControl_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right || e.Button == MouseButtons.Left) { DgvEx.Hide(); } }
/// <summary> /// DataGridView回车事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> internal void DgvEx_KeyDown(object sender, KeyEventArgs e) { if (DgvEx.Rows.Count > 0 && DgvEx.CurrentRow.Index >= 0) { if (e.KeyCode == Keys.Enter && DgvEx.Focus()) { TextBoxBindValue(); } } }
private void TxtExistDt() { if (!this.Focused && !DgvEx.Focused) { DgvEx.Hide(); if (this.Text.Trim().Length > 0 && thisText.Length > 0) { string strWhere = DgvDataSourceWhere.Replace("@keyword", thisText); strWhere = strWhere.Replace("like", "=").Replace("%", ""); DataRow[] drs = ((DataTable)DgvDataSource).Select(strWhere); if (drs.Length <= 0) { MessageBox.Show("输入不正确!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Focus(); this.SelectAll(); } } } }
/// <summary> /// tab按下验证 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> internal void TextBoxDplDgv_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Tab) { DgvEx.Hide(); if (this.Text.Trim().Length > 0) { string strWhere = DgvDataSourceWhere.Replace("@keyword", this.Text.Trim()); strWhere = strWhere.Replace("like", "=").Replace("%", ""); DataRow[] drs = ((DataTable)DgvDataSource).Select(strWhere); if (drs.Length <= 0) { MessageBox.Show("输入不正确!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Focus(); this.SelectAll(); } } } }
/// <summary> /// 初始化 /// </summary> internal void Init() { this._DgvEx = new DataGridViewEx(); DgvEx.Hide(); DgvEx.AllowUserToAddRows = false; DgvEx.AllowUserToDeleteRows = false; DgvEx.ColumnVisibleEnableSave = false; DgvEx.ColumnWidthSave = false; DgvEx.SelectionMode = DataGridViewSelectionMode.FullRowSelect; DgvEx.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; DgvEx.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells; DgvEx.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; DgvEx.BackgroundColor = System.Drawing.SystemColors.Window; DgvEx.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; DgvEx.RowHeadersVisible = false; DgvEx.EditMode = DataGridViewEditMode.EditProgrammatically; //行字体颜色 System.Windows.Forms.DataGridViewCellStyle DGVCSRows = new System.Windows.Forms.DataGridViewCellStyle(); DGVCSRows.Font = new System.Drawing.Font("宋体", 10F); DGVCSRows.ForeColor = Color.FromArgb(51, 51, 51); DgvEx.RowsDefaultCellStyle = DGVCSRows; this.BringToFront(); ((System.ComponentModel.ISupportInitialize)(DgvEx)).EndInit(); this.Click += new EventHandler(TextBoxDplDgv_Click); this.KeyDown += new KeyEventHandler(TextBoxDplDgv_KeyDown); this.Enter += new EventHandler(TextBoxDplDgv_Enter); this.Leave += new EventHandler(TextBoxDplDgv_Leave); this.PreviewKeyDown += new PreviewKeyDownEventHandler(TextBoxDplDgv_PreviewKeyDown); DgvEx.KeyDown += new KeyEventHandler(DgvEx_KeyDown); DgvEx.CellDoubleClick += new DataGridViewCellEventHandler(DgvEx_CellDoubleClick); DgvEx.LostFocus += new EventHandler(DgvEx_LostFocus); DgvEx.CellFormatting += new DataGridViewCellFormattingEventHandler(DgvEx_CellFormatting); }
/// <summary> /// 清空TextBox赋值 /// </summary> internal void TextBoxClearValue() { this.dataGridViewRow = DgvEx.CurrentRow; if (!string.IsNullOrEmpty(DisplayMember) && !string.IsNullOrEmpty(ValueMember)) { this.Tag = ""; } else { if (DgvForm != null) { foreach (Control ctl in DgvForm.Controls) { ControlClearValue(ctl); } } //DgvEx.Hide(); } DgvEx.Hide(); //this.Parent.SelectNextControl(this, true, true, true, true); }
/// <summary> /// parentControl按下键选择DataGridView中的数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> internal void parentControl_KeyDown(object sender, KeyEventArgs e) { if (DgvEx == null) { return; } if (DgvEx.Visible == true && e.KeyCode == Keys.Down) { if (DgvEx.Rows.Count > 0) { DgvEx.Focus(); DgvEx.Rows[0].Selected = true; } } if (e.KeyCode == Keys.Escape) { DgvEx.Hide(); } }
/// <summary> /// 绑定显示 /// </summary> internal void DgvExShow(object sender, EventArgs e) { //如果没绑数据则不显示 if (DgvDataSource == null) { if (txtdgv_dataBinds != null) { txtdgv_dataBinds(sender, e); } else { return; } } try { if (DgvDataSource == null) { return; } this.dgvForm = this.FindForm(); DgvForm.KeyPreview = true; DataTable dt = new DataTable(); dt = ((DataTable)DgvDataSource).Clone(); ////添加对文本框为焦点时过滤% pwg 20140506 string txtwhere = ""; if (this.Text.Trim().Contains("%")) { int indesif = this.Text.Trim().IndexOf("%"); int length = this.Text.Trim().Length; if (indesif != 0 && length > 1 && indesif + 1 < length) { txtwhere = this.Text.Trim().Replace("%", ""); } } else { txtwhere = this.Text.Trim(); } string strWhere = DgvDataSourceWhere.Replace("@keyword", txtwhere); DataRow[] drs = ((DataTable)DgvDataSource).Select(strWhere); int row = 0; foreach (DataRow dr in drs) { row++; if (row == ShowRowCount) { break; } dt.ImportRow(dr); } DgvEx.TabIndex = this.TabIndex; DgvX = 0; DgvY = 0; controlParent(this); //DgvX = this.Location.X; //DgvY=this.Location.Y; DgvEx.Location = new Point(DgvX, DgvY + this.Height); //DgvEx.Location = this.FindForm().PointToClient(this.PointToScreen(new Point(-2, this.Height))); Size size = DgvForm.Size; if (wValue >= (size.Width - DgvX)) { wValue = (size.Width - DgvX) - 15; } if (hValue >= (size.Height - (DgvY + this.Height))) { hValue = (size.Height - (DgvY + this.Height)) - 15; } DgvEx.Size = new System.Drawing.Size(WValue, HValue); DgvEx.Parent = this.Parent; DgvEx.BringToFront(); DgvEx.DataSource = null; DgvEx.DataSource = dt; string[] indexs = VisibleIndexs.Split(','); foreach (string index in indexs) { int a; if (int.TryParse(index, out a)) { DgvEx.Columns[a].Visible = false; } } float widths = 0; DgvEx.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; for (int i = 0; i < DgvEx.Columns.Count; i++) { if (DgvEx.Columns[i].Visible) { widths += DgvEx.Columns[i].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true); // 计算调整列后单元列的宽度和 } } if (widths >= DgvEx.Size.Width - 7) // 如果调整列的宽度大于设定列宽 { DgvEx.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; // 调整列的模式 自动 } //else // DgvEx.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 如果小于 则填充 Cursor.Current = Cursors.Default; DgvEx.Show(); this.Focus(); BindTextBoxDplDgvToForm(); } catch (Exception ex) { //throw new Exception(ex.Message); MessageBox.Show(ex.Message); } }