C# 在DataGridView中,点击单元格调出 TreeView控件 或 ListBox控件
1、调出 TreeView控件 或 ListBox控件
private void deductGrid1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) return; if (e.ColumnIndex == 19 || e.ColumnIndex == 22 || e.ColumnIndex == 25) { Rect = deductGrid1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); this.TvEmp.Size = new Size(122,300); TvEmp.Location = new Point(Rect.X, Rect.Y); TvEmp.Visible = true; } else { TvEmp.Visible = false; } if (e.ColumnIndex == 20 || e.ColumnIndex == 23 || e.ColumnIndex == 26) { Rect = deductGrid1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); this.cmb.Size = new Size(Rect.Width , 2 * Rect.Height); cmb.Location = new Point(Rect.X, Rect.Y + Rect.Height); cmb.Items.Clear(); cmb.Items.Add("是"); cmb.Items.Add("否"); //this.cmb .Size = new Size(Rect .Width ,Rect .Height); //cmb.Location = new Point(Rect.X, Rect.Y); cmb.Visible = true; } else { cmb.Visible = false; } }
2、处理TreeView控件 选择内容赋值到单元格内
private void TvEmp_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (TvEmp.GetNodeAt(e.X, e.Y) == null) return; if (TvEmp.GetNodeAt(e.X, e.Y).Level < 2) { dqlib.ShowExclamation("请选择有效的信息!"); return; } if (deductGrid1.CurrentCell .ColumnIndex-1 == 18) { deductGrid1.Rows[deductGrid1.CurrentRow.Index].Cells["EmpOne"].Value = TvEmp.GetNodeAt(e.X, e.Y).Tag; deductGrid1.CurrentCell.Value = TvEmp.GetNodeAt(e.X, e.Y).Text; } if (deductGrid1.CurrentCell.ColumnIndex - 1 == 21) { deductGrid1.Rows[deductGrid1.CurrentRow.Index].Cells["EmpTwo"].Value = TvEmp.GetNodeAt(e.X, e.Y).Tag; deductGrid1.CurrentCell.Value = TvEmp.GetNodeAt(e.X, e.Y).Text; } if (deductGrid1.CurrentCell.ColumnIndex - 1 == 24) { deductGrid1.Rows[deductGrid1.CurrentRow.Index].Cells["EmpThree"].Value = TvEmp.GetNodeAt(e.X, e.Y).Tag; deductGrid1.CurrentCell.Value = TvEmp.GetNodeAt(e.X, e.Y).Text; } //deductGrid1.CurrentCell.Value = TvEmp.GetNodeAt(e.X ,e.Y).Text; //deductGrid1.CurrentCell.Tag = TvEmp.GetNodeAt(e.X, e.Y).Tag; TvEmp.Visible = false; }
3、处理ListBox控件
private void cmb_SelectedIndexChanged(object sender, EventArgs e) { if (cmb.SelectedItem != null) { this.deductGrid1.CurrentCell.Value = this.cmb.SelectedItem.ToString(); this.cmb.Visible = false; } }
4、处理文本框
private void dgvWeight_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) return; if (e.ColumnIndex == 9) { Rectangle Rect = dgvWeight.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); this.textBox9.Size = new Size(Rect.Width, Rect.Height); textBox9.Location = new Point(Rect.X, Rect.Y); textBox9.Visible = true; textBox9.Focus(); GetRowIndex = e.RowIndex; } else { textBox9.Visible = false; } }