zoukankan      html  css  js  c++  java
  • 2015.1.3 DataGridView中嵌入其它控件

    1、按正常方法绑定待嵌入列的值,先赋值为空也行。

    2、添加combbox到datagrivdview中 dvaw.Controls.Add(cb_dir);

     

    3、添加DataGridView MouseClick事件

    private void dvaw_MouseClick(object sender, MouseEventArgs e)

     {

         int row = GetRowIndexAt(e.Y); //点击行

               

         int comcol = dvaw.Columns.Count - 1;//要出现combox的列

     

         if (row>=0 && btn_alter.Text == "确认修改")

         {

              Rectangle rect = dvaw.GetCellDisplayRectangle(comcol, row, false);//列,行,是否只要显示部分

              cb_dir.Text = dvaw.Rows[row].Cells[comcol].Value.ToString(); //原来的值作为嵌入combbox的初始值

              //将嵌入控件显示在当前cell中

              cb_dir.Left = rect.Left;

              cb_dir.Top = rect.Top;

              cb_dir.Width = rect.Width;

              cb_dir.Height = rect.Height;

              cb_dir.Visible = true;

     

              if (dvaw.CurrentCell != null && dvaw.CurrentCell.ColumnIndex == comcol)

              {

                   //当点中方向列时,combbox自动下拉************

                   cb_dir.DroppedDown = true;

               }

         }

         else

           cb_dir.Visible = false;//不满足combbox显示条件时,隐藏combbox

    }

    4. 添加判断鼠标点击时所在行函数,比CellClick的好处是点击空白处也能判断-1

    public int GetRowIndexAt(int mouseLocation_Y)

    {

        if (dvaw.FirstDisplayedScrollingRowIndex < 0)

        {

          return -1;

         }

      if (dvaw.ColumnHeadersVisible == true && mouseLocation_Y <= dvaw.ColumnHeadersHeight)

          {

              return -1;

          }

     int index = dvaw.FirstDisplayedScrollingRowIndex;

     int displayedCount = dvaw.DisplayedRowCount(true);

      for (int k = 1; k <= displayedCount; )

                {

                    if (dvaw.Rows[index].Visible == true)

                    {

                        Rectangle rect = dvaw.GetRowDisplayRectangle(index, true);  // 取该区域的显示部分区域  

                        if (rect.Top <= mouseLocation_Y && mouseLocation_Y < rect.Bottom)

                        {

                            return index;

                        }

                        k++;

                    }

                    index++;

                }

                return -1;

            }

     

    5. 添加combbox的SelectedIndexChanged 事件

    //每次选择combox值时,自动修改对应cell的显示值,虽然此时cell隐藏在combox后面,当combox隐藏时,该新值自动显示出来

     private void cb_dir_SelectedIndexChanged(object sender, EventArgs e)

    {

      int comcol = dvaw.Columns.Count - 1;//要出现combox的列

     

      //注意,下行不能用Currentcell来做判断,因为当点击第一列时,也能选择combbox,但此时的当前cell就不是第二列,会导致选择无效。所以用currentrow来判断

        if(btn_alter.Text == "确认修改")

       {

         if (dvaw.CurrentRow != null)

           dvaw.CurrentRow.Cells[comcol].Value = ((ComboBox)sender).Text;

         else

           MessageBox.Show("请先选中航路所在行,再选择方向");

        }    

    }

  • 相关阅读:
    SQLyog连接mysql8,报错1251
    Oracle日期函数
    git 回退
    git新建分支并指定拉去远程分支
    git创建分支并拉去远端分支代码
    git创建空白分支
    Maven 本地仓库明明有jar包,pom文件还是报错解决办法(Missing artifact...jar)
    SqlHelper类
    ADO.NET中的模型及对象
    MVC过滤器---异常处理过滤器
  • 原文地址:https://www.cnblogs.com/mol1995/p/5964802.html
Copyright © 2011-2022 走看看