实现效果:
点击添加新增一行空白数据。然后就可以为所欲为了
一:得到列的索引
int ColumnIndex = this.DgvDiagnosisList.CurrentCell.ColumnIndex;//获取列的索引
二:得到当前选中行并得到该行的某一列的值
int RowIndex = this.DgvDiagnosisList.CurrentRow.Index;//获取行的索引
int RowIndex a= e.RowIndex;//这句话也可以获得行的索引,但上面那句使用更广泛。
diagnoseIDs = Convert.ToInt32(this.DgvDiagnosisList.Rows[RowIndex].Cells["diagnoseID"].Value.ToString());
实现功能:单击某一行某一列然后编辑那一列的值根据值模糊查询弹出listview使数据显示在listview中,并且可以把listview中选中的值附加到datagridview中的选中行.
需要控件
Datagridview
TextBox
ListView
事件
Datagridview CurrentCellChanged事件
TextBox KeyDowm(首次按下某个键)事件 ,TextChanged事件
ListView KeyPress事件,MouseDoubleClick(鼠标双击)事件
代码
DagaGridView
private void DgvDiagnosisList_CurrentCellChanged(object sender, EventArgs e)
{
DataGridViewCell cell = this.DgvDiagnosisList.CurrentCell;
if ((cell != null) && (cell.ColumnIndex == 1) && (cell.RowIndex != -1))
{
this.TextChangedBox.Size = cell.Size;
Rectangle rect = this.DgvDiagnosisList.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, false);
this.TextChangedBox.Top = rect.Top + this.DgvDiagnosisList.Top;
this.TextChangedBox.Left = rect.Left + this.DgvDiagnosisList.Left;
if (this.DgvDiagnosisList.Top.ToString() == this.TextChangedBox.Top.ToString())
{
this.TextChangedBox.Visible = false;
}
else
{
this.TextChangedBox.Visible = true;
}
textChangeLock = true;
if (cell.Value != null)
this.TextChangedBox.Text = cell.Value.ToString();
else
this.TextChangedBox.Text = "";
this.TextChangedBox.Focus();
textChangeLock = false;
}
else
{
this.TextChangedBox.Visible = false;
this.listView2.Visible = false;
}
}
TextBox
private void TextChangedBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 40) // 当在TextChangedBox输入下方向键时
{
this.listView2.Focus(); //listView2得到焦点
this.TextChangedBox.Visible = false;
}
}
private void TextChangedBox_TextChanged(object sender, EventArgs e)
{
if (textChangeLock)
return;
if (IsType == 0 || IsType == 1)
{
DataTable dt = new DataTable();
string ChangedBox = this.TextChangedBox.Text;
if (IsType == 1)
{
dt = fun.Se_DuoBiao15(departmentID, ChangedBox);
}
else if (IsType == 0)
{
dt = fun.Se_DuoBiao16(UserID, ChangedBox);
}
this.listView2.Visible = true;