欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net WinForm用户控件开发--(2)带按钮DataGridView

程序员文章站 2022-04-14 10:28:16
   这一节大家共同学习下自定义的datagridview, 这个datagridview的主要功能是可以使datagridview中的某些列包含按钮,单击按钮...

   这一节大家共同学习下自定义的datagridview, 这个datagridview的主要功能是可以使datagridview中的某些列包含按钮,单击按钮可以触发相应的事件。

我们先来看下效果图吧!

  .net WinForm用户控件开发--(2)带按钮DataGridView

下面我们来说下实现步骤

  1.创建自定义控件

    创建一个自定义控件,添加新项---自定义控件

 .net WinForm用户控件开发--(2)带按钮DataGridView

2.实现自定义datagridview控件

  使刚创建的自定义控件继承datagridview,然后在自定义控件中拖放一个button按钮,然后在datagridview中的单元格获得焦点的时候,使当前单元格在当前单元格的右侧显示一个按钮.

  代码如下:

  

[csharp]
public partial class DataGridViewButton : DataGridView 
  { 
      private Array _ShowButtonColumns;   //有按钮的列名称  
 
      /// <summary>  
      /// 设置要显示按钮的列  
      /// </summary>  
      /// <param name="ShowButtonColumns"></param>  
      public void SetParam(Array ShowButtonColumns) 
      { 
          _ShowButtonColumns = ShowButtonColumns; 
      } 
 
      public DataGridViewButton() 
      { 
          InitializeComponent(); 
          this.Controls.Add(button1); 
      } 
 
      /// <summary>  
      /// 数组中是否有与指定值相等的元素  
      /// </summary>  
      /// <param name="columnName"></param>  
      /// <param name="ShowButtonColumns"></param>  
      /// <returns></returns>  
      private bool IsShowButtonColumn(string columnName, Array ShowButtonColumns) 
      { 
          if (string.IsNullOrEmpty(columnName) || ShowButtonColumns == null || ShowButtonColumns.Length < 1) return false; 
 
          foreach (string astr in ShowButtonColumns) 
              if (astr == columnName) return true; 
 
          return false; 
      } 
 
      protected override void OnPaint(PaintEventArgs pe) 
      { 
          base.OnPaint(pe); 
      } 
 
      private void DataGridViewButton_CellEnter(object sender, DataGridViewCellEventArgs e) 
      { 
          if (IsShowButtonColumn(this.Columns[this.CurrentCell.ColumnIndex].Name, _ShowButtonColumns)) 
          { 
 
              Point p = new Point(); 
 
              if (this.button1.Height != this.Rows[this.CurrentCell.RowIndex].Height) 
              { 
                  this.button1.Height = this.Rows[this.CurrentCell.RowIndex].Height; 
              } 
              #region 获取X轴的位置  
 
 
              if (this.RowHeadersVisible) 
              { 
                  //判断该类是否包含行标题,如果该列包含行标题,按钮的横坐标位置等于当前位置加上行标题  
                  p.X += this.RowHeadersWidth; 
              } 
              //FirstDisplayedCell表示左上角第一个单元格  
              for (int i = this.FirstDisplayedCell.ColumnIndex; i <= this.CurrentCell.ColumnIndex; i++) 
              { 
                  if (this.Columns[i].Visible) 
                  { 
                      //当前位置=单元格的宽度加上分隔符发宽度  
                      p.X += this.Columns[i].Width + this.Columns[i].DividerWidth; 
                  } 
              } 
 
              p.X -= this.FirstDisplayedScrollingColumnHiddenWidth; 
              p.X -= this.button1.Width; 
              #endregion 
 
              #region 获取Y轴位置  
 
              if (this.ColumnHeadersVisible) 
              { 
                  //如果列表题可见,按钮的初始纵坐标位置等于当前位置加上列标题  
                  p.Y += this.ColumnHeadersHeight; 
              } 
 
              //获取或设置某一列的索引,该列是显示在 DataGridView 上的第一列  
              for (int i = this.FirstDisplayedScrollingRowIndex; i < this.CurrentCell.RowIndex; i++) 
              { 
                  if (this.Rows[i].Visible) 
                  { 
                      p.Y += this.Rows[i].Height + this.Rows[i].DividerHeight; 
                  } 
              } 
 
              #endregion  
 
              this.button1.Location = p; 
              this.button1.Visible = true; 
          } 
          else 
          { 
              this.button1.Visible = false; 
          } 
      } 
 
      private void DataGridViewButton_Scroll(object sender, ScrollEventArgs e) 
      { 
          this.button1.Visible = false; 
      } 
 
      //定义按钮的单击事件  
      public delegate void ButtonClick(); 
      public event ButtonClick ButtonSelectClick; 
 
      private void button1_Click(object sender, EventArgs e) 
      { 
          this.ButtonSelectClick.DynamicInvoke(null); 
      } 
  } 

  public partial class DataGridViewButton : DataGridView
    {
        private Array _ShowButtonColumns;   //有按钮的列名称

        /// <summary>
        /// 设置要显示按钮的列
        /// </summary>
        /// <param name="ShowButtonColumns"></param>
        public void SetParam(Array ShowButtonColumns)
        {
            _ShowButtonColumns = ShowButtonColumns;
        }

        public DataGridViewButton()
        {
            InitializeComponent();
            this.Controls.Add(button1);
        }

        /// <summary>
        /// 数组中是否有与指定值相等的元素
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="ShowButtonColumns"></param>
        /// <returns></returns>
        private bool IsShowButtonColumn(string columnName, Array ShowButtonColumns)
        {
            if (string.IsNullOrEmpty(columnName) || ShowButtonColumns == null || ShowButtonColumns.Length < 1) return false;

            foreach (string astr in ShowButtonColumns)
                if (astr == columnName) return true;

            return false;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private void DataGridViewButton_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (IsShowButtonColumn(this.Columns[this.CurrentCell.ColumnIndex].Name, _ShowButtonColumns))
            {

                Point p = new Point();

                if (this.button1.Height != this.Rows[this.CurrentCell.RowIndex].Height)
                {
                    this.button1.Height = this.Rows[this.CurrentCell.RowIndex].Height;
                }
                #region 获取X轴的位置


                if (this.RowHeadersVisible)
                {
                    //判断该类是否包含行标题,如果该列包含行标题,按钮的横坐标位置等于当前位置加上行标题
                    p.X += this.RowHeadersWidth;
                }
                //FirstDisplayedCell表示左上角第一个单元格
                for (int i = this.FirstDisplayedCell.ColumnIndex; i <= this.CurrentCell.ColumnIndex; i++)
                {
                    if (this.Columns[i].Visible)
                    {
                        //当前位置=单元格的宽度加上分隔符发宽度
                        p.X += this.Columns[i].Width + this.Columns[i].DividerWidth;
                    }
                }

                p.X -= this.FirstDisplayedScrollingColumnHiddenWidth;
                p.X -= this.button1.Width;
                #endregion

                #region 获取Y轴位置

                if (this.ColumnHeadersVisible)
                {
                    //如果列表题可见,按钮的初始纵坐标位置等于当前位置加上列标题
                    p.Y += this.ColumnHeadersHeight;
                }

                //获取或设置某一列的索引,该列是显示在 DataGridView 上的第一列
                for (int i = this.FirstDisplayedScrollingRowIndex; i < this.CurrentCell.RowIndex; i++)
                {
                    if (this.Rows[i].Visible)
                    {
                        p.Y += this.Rows[i].Height + this.Rows[i].DividerHeight;
                    }
                }

                #endregion

                this.button1.Location = p;
                this.button1.Visible = true;
            }
            else
            {
                this.button1.Visible = false;
            }
        }

        private void DataGridViewButton_Scroll(object sender, ScrollEventArgs e)
        {
            this.button1.Visible = false;
        }

        //定义按钮的单击事件
        public delegate void ButtonClick();
        public event ButtonClick ButtonSelectClick;

        private void button1_Click(object sender, EventArgs e)
        {
            this.ButtonSelectClick.DynamicInvoke(null);
        }
    }
3.编辑该控件,将控件拖放到窗体

   现在编译该控件,然后从工具箱中将该控件拖放到窗体上,然后绑定datagridview,设置datagridview指定的列显示按钮,当单击按钮的时候弹出一个对话框,

输入内容,将输入的内容重新赋值给当前单元格。

  效果图:

   .net WinForm用户控件开发--(2)带按钮DataGridView

 

  代码:

 

[csharp]
public partial class Form1 : Form 
   { 
       public Form1() 
       { 
           InitializeComponent(); 
       } 
 
       private void Form1_Load(object sender, EventArgs e) 
       { 
           DataTable dt = new DataTable(); 
           DataColumn dc1 = new DataColumn("weibo", typeof(System.String)); 
           dt.Columns.Add(dc1); 
 
           DataColumn dc2 = new DataColumn("mail", typeof(System.String)); 
           dt.Columns.Add(dc2); 
 
           DataColumn dc3 = new DataColumn("blog", typeof(System.String)); 
           dt.Columns.Add(dc3); 
 
           for (int i = 0; i < 50; i++) 
           { 
               DataRow dr = dt.NewRow(); 
               dr[0] = "下里巴人simple"; 
               dr[1] = "524300045@qq.com"; 
               dr[2] = "msdn" + i.ToString(); 
               dt.Rows.Add(dr); 
           } 
           //设置要显示按钮的单元列  
           string[] showcell = { "weibo", "mail" }; 
           this.dataGridViewButton1.SetParam(showcell); 
           this.dataGridViewButton1.DataSource = dt; 
       } 
 
       private void dataGridViewButton1_ButtonSelectClick() 
       { 
           Form2 f2 = new Form2(); 
           if (f2.ShowDialog()==DialogResult.OK) 
           { 
               this.dataGridViewButton1.CurrentCell.Value = f2.ReturnText; 
           } 
       } 
   } 

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("weibo", typeof(System.String));
            dt.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("mail", typeof(System.String));
            dt.Columns.Add(dc2);

            DataColumn dc3 = new DataColumn("blog", typeof(System.String));
            dt.Columns.Add(dc3);

            for (int i = 0; i < 50; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "下里巴人simple";
                dr[1] = "524300045@qq.com";
                dr[2] = "msdn" + i.ToString();
                dt.Rows.Add(dr);
            }
            //设置要显示按钮的单元列
            string[] showcell = { "weibo", "mail" };
            this.dataGridViewButton1.SetParam(showcell);
            this.dataGridViewButton1.DataSource = dt;
        }

        private void dataGridViewButton1_ButtonSelectClick()
        {
            Form2 f2 = new Form2();
            if (f2.ShowDialog()==DialogResult.OK)
            {
                this.dataGridViewButton1.CurrentCell.Value = f2.ReturnText;
            }
        }
    }

 

作者:zx13525079024