DataGridView 编辑 出现 ListView

一:得到列的索引

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;
                this.listView2.Left = this.TextChangedBox.Left;
                this.listView2.Top = this.TextChangedBox.Top + 20;
                this.listView2.Width = 700;
                this.listView2.Items.Clear();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem ite = new ListViewItem();
                    ite.SubItems[0].Text = dr["diagnoseID"].ToString();
                    ite.SubItems.Add(dr["icdCode"].ToString());
                    ite.SubItems.Add(dr["助记符"].ToString());
                    ite.SubItems.Add(dr["科室"].ToString());
                    ite.SubItems.Add(dr["诊断名称"].ToString());

                    ite.SubItems.Add(dr["specificDiseases"].ToString());
                    ite.SubItems.Add(dr["isValid"].ToString());
                    listView2.Items.Add(ite);

                }
            }

        }

listview

private void listView2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                evaluate();

            }
        }

private void listView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.evaluate();
        }

方法

/// <summary>
        /// 在dgv中显现listview2选中的数据
        /// </summary>
        private void evaluate()
        {
            int Rowindex = this.DgvDiagnosisList.CurrentRow.Index;

            foreach (ListViewItem lv in this.listView2.SelectedItems)
            {
                DgvDiagnosisList.Rows[Rowindex].Cells["diagnoseEditID"].Value = "0";
                DgvDiagnosisList.Rows[Rowindex].Cells["diagnoseID"].Value = lv.SubItems[0].Text;
                DgvDiagnosisList.Rows[Rowindex].Cells["icdCode"].Value = lv.SubItems[1].Text;
                DgvDiagnosisList.Rows[Rowindex].Cells["diagnosecode"].Value = lv.SubItems[2].Text;
                DgvDiagnosisList.Rows[Rowindex].Cells["departmentName"].Value = lv.SubItems[3].Text;
                DgvDiagnosisList.Rows[Rowindex].Cells["diagnoseName"].Value = lv.SubItems[4].Text;
                DgvDiagnosisList.Rows[Rowindex].Cells["specificDiseases"].Value = lv.SubItems[5].Text;//特殊疾病
                DgvDiagnosisList.Rows[Rowindex].Cells["isValid"].Value = lv.SubItems[6].Text;
                diagnoseIDs = Convert.ToInt32(lv.SubItems[0].Text);

            }
            this.TextChangedBox.Visible = false;
            this.listView2.Visible = false;

        }

在winform中datagridview添加的时候增加空白行的时候,有两个办法

1:定义一个datatable,然后将数据绑定到这个datatable,然后实例化一个datarow,新增一行,最后将这一行增加到datatable里面,最后将datatable复制给datagridview

          DataTable dtabel  = datatable数据源

          DataRow drow = dtabel .NewRow();

         dtabel .Rows.Add(drow);

          datagridview1.DataSource = dtabel ;

2:直接将datagridview的属性设置为:AllowUserToAddRows = true

    但是这样有个问题,在增加新的一行的时候,输入文字的时候,又会出现新的一行

posted @ 2012-05-27 10:50  风与雨无阻  阅读(452)  评论(0编辑  收藏  举报