当前位置: 代码迷 >> C# >> datagridview目录以及双击事件
  详细解决方案

datagridview目录以及双击事件

热度:17   发布时间:2016-05-05 04:33:27.0
datagridview索引以及双击事件
 #region 给dataGridview添加双击事件
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                DataGridView dgv = sender as DataGridView;
                if (dgv.Rows[e.RowIndex].Cells[1].Value != null)
                {
                    string NAME = dgv.Rows[e.RowIndex].Cells[4].Value.ToString();
                    string outNAME;
                    DialogResult myresult;
                    this.ShowDialogForm(NAME, out outNAME, out myresult);
                    if (myresult == DialogResult.OK)
                    {
                        dgv.Rows[e.RowIndex].Cells[4].Value = outNAME;
                    }
                }
            }
        }
        #endregion

#region 读取dgv数据,弹出子窗口,显示。
        private void ShowDialogForm(string NAME, out string outNAME, out DialogResult myresult)
        {
            EditForm frm = new EditForm();

            //读取输入参数,显示在子窗口中
            frm.Controls["txtPOICODE"].Text = NAME;

            frm.ShowDialog();//显示子窗口

            //给输出参数赋值默认值
            outNAME = NAME;

            //判断是否进行修改数据
            myresult = frm.DialogResult;
            if (myresult == DialogResult.OK)
            {
                outNAME = frm.Controls["txtPOICODE"].Text;
                MessageBox.Show("修改了数据");
            }
            else
            {
                MessageBox.Show("没有修改数据");
            }

            frm.Dispose();
        }
        #endregion

#region 弹出窗口,修改数据
        int selectrowindex = -1;
        private void button3_Click_1(object sender, EventArgs e)
        {
            if (selectrowindex < 0)
            {
                MessageBox.Show("请先选择一条记录");
                return;
            }
            //采用弹出,进行修改
            DataGridView dgv = dataGridView1;
            if (dgv.Rows[selectrowindex].Cells[1].Value != null)
            {
                //输入参数
                string NAME = dgv.Rows[selectrowindex].Cells[3].Value.ToString();

                //输出参数
                string outNAME;

                //返回弹出窗口关闭状态
                DialogResult myresult;
                this.ShowDialogForm(NAME, out outNAME, out myresult);
                //如果修改了数据,把当前datagridview内容的对应值进行修改
                if (myresult == DialogResult.OK)
                {
                    dgv.Rows[selectrowindex].Cells[3].Value = outNAME;
                }


            }
        }
        #endregion

        //获取当前选择的目标
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectrowindex = e.RowIndex;
        }

------解决思路----------------------
if (e.ColumnIndex == 0)//这里决定了你必须双击第一列才行,双击其他列就直接退出方法了
if (dgv.Rows[e.RowIndex].Cells[1].Value != null)这里决定了你第二列必须有值,如果是null,那么就退出方法了

这种问题,自己断点调试一下应该很容易能发现才对
------解决思路----------------------
frm.ShowDialog();//显示子窗口
 
            //给输出参数赋值默认值
            outNAME = NAME;
 
            //判断是否进行修改数据
            myresult = frm.DialogResult;
这里,应该
myresult=frm.ShowDialog();
ShowDialog方法本身有返回值,返回值类型就是DialogResult,不用费2遍事
------解决思路----------------------
if (selectrowindex < 0)
这里,你判断的是个全局变量啊,你确定它有被重新赋值过?

为什么不用gdv.SelectedRows来判断呢
------解决思路----------------------
dgv.Rows[e.RowIndex].Cells[1].Value  取的是 SHAPE的值,当然是空了。 条件根本不成立。。。。
------解决思路----------------------

楼主何必拐这么大的弯呢。DataGridView本身就有提供当前行的属性呀

dataGridView1.CurrentRow 是当前行
dataGridView1.CurrentCell  当前选中的单元格
dataGridView1.CurrentCell.OwenRow 当前单元格所有的行。。。




private void button3_Click_1(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow==null)  
            {
                MessageBox.Show("请先选择一条记录");
                return;
            }
         
            if (dataGridView1.CurrentRow.Cells[1].Value != null)
            {
                //输入参数
                string NAME =dataGridView1.CurrentRow.Cells[3].Value.ToString();
 
                //输出参数
                string outNAME;
 
                //返回弹出窗口关闭状态
                DialogResult myresult;
                this.ShowDialogForm(NAME, out outNAME, out myresult);
                //如果修改了数据,把当前datagridview内容的对应值进行修改
                if (myresult == DialogResult.OK)
                {
                   dataGridView1.CurrentRow.Cells[3].Value = outNAME;
                }
 
 
            }
        }



  相关解决方案