1. 程式人生 > >C# DataGridView單元格中動態新增多個控制元件

C# DataGridView單元格中動態新增多個控制元件

簡介:

         在DataGridView的單元格中動態新增多個控制元件。例如在DataGridViewTextBox單元格中,新增CheckBox和Button控制元件。主題思路就是一個動態控制元件的大小,位置,顯示,事件設定,和平常控制元件一樣使用。

         程式碼下載連結:https://download.csdn.net/download/c_gyl/10861487

         如果不熟悉此控制元件的使用,參考連結:

https://blog.csdn.net/C_gyl/article/details/85067599


效果:

         如下圖:

                      


新增

                 AddControl()動態新增控制元件,繫結事件。

        //新增按鈕
        private void AddControl()
        {
            
            dataGridView1.Columns[InfoColumnName].Width = 70;

            for (int i = 0; i < Data.Length; i++)
            {
                TextBox tbx = new TextBox();  
                CheckBox chx = new CheckBox();  //新增CheckBox
                Button btn = new Button();      //新增Button

                //新增
                dataGridView1.Controls.Add(chx);
                dataGridView1.Controls.Add(btn);

                //獲取大小
                Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);
                //大小設定
                tbx.Size = new Size((rect.Width / 3), rect.Height);
                chx.Size = new Size((rect.Width / 3), rect.Height);
                btn.Size = new Size((rect.Width / 3), rect.Height);

                //位置設定
                tbx.Location = new Point(rect.Left, rect.Top );
                chx.Location = new Point(rect.Left + tbx.Width, rect.Top);
                btn.Location = new Point(rect.Left + tbx.Width + chx.Width, rect.Top);

                //繫結事件
                chx.CheckedChanged += new EventHandler(chx_CheckedChanged);
                btn.Click += new EventHandler(btn_Click);

                //屬性設定
                chx.Tag = i;
                Chx[i] = chx;
                btn.Tag = i;
                Btn[i] = btn;

            }
        }

1.變數

  1. InfoColumnName是屬性DataPropertyName設定的。
  2. ColumnIndex是要新增的列索引。
        DataTable Table = new DataTable();                         //資料,繫結DataGridView
        structData[] Data = new structData[10];                    //資料,長度自行更改
        string NoColumnName = Enum.GetName(typeof(enumData), 0);   //資料來源列名
        string InfoColumnName = Enum.GetName(typeof(enumData), 1); //資料來源列名
        CheckBox[] Chx = new CheckBox[10];                         //按鈕CheckBox
        Button[] Btn = new Button[10];                             //按鈕Button
        int ColumnIndex = 1;                                       //要新增在哪一個列的索引

 

2.新增控制元件

dataGridView1.Controls.Add(chx);

 

3.獲取大小

Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);

 

4.大小設定

  1. 控制元件的寬,要根據實際需要設定寬。
  2. 控制元件的高,一般預設原高。
chx.Size = new Size((rect.Width / 3), rect.Height);

 

5.位置設定

  1. 控制元件的起點X,要根據相鄰的起點位置加上相鄰的寬。
  2. 控制元件的起點Y,一般預設原Y。
chx.Location = new Point(rect.Left + tbx.Width, rect.Top);

 

6.事件

  1. 先繫結,和正常控制元件事件一樣,根據實際需要自行更改。
  2. 新增事件。
chx.CheckedChanged += new EventHandler(chx_CheckedChanged);
        private void chx_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ch = sender as CheckBox;
            int index = int.Parse(ch.Tag.ToString());
            bool value = ch.Checked;
            //Do Something
        }

重新整理

            當用到滾動時,還需要把對應的控制元件顯示出來。

        //Scroll事件
        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            ControlRefresh();
        }

        //更新顯示
        //1.滾動時需要啟用,否則按鈕會看不到
        private void ControlRefresh()
        {
            int curFirstIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
            int disCount = dataGridView1.DisplayedRowCount(true);

            for (int j = 0; j < Data.Length; j++)
            {
                Chx[j].Visible = false;
                Btn[j].Visible = false;
            }
            for (int i = curFirstIndex; i < curFirstIndex + disCount; i++)
            {
                Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);

                Chx[i].Size = new Size((rect.Width / 3), rect.Height);
                Chx[i].Location = new Point(rect.Left + (rect.Width / 3), rect.Top);
                Chx[i].Visible = true;

                Btn[i].Size = new Size((rect.Width / 3), rect.Height);
                Btn[i].Location = new Point(rect.Left + (rect.Width * 2 / 3), rect.Top);
                Btn[i].Visible = true;
            }
        }

 

1.Scroll事件

呼叫ControlRefresh()函式。

 

2.獲取當前首行的索引

int curFirstIndex = dataGridView1.FirstDisplayedScrollingRowIndex;

 

3.獲取當前顯示的行數

 int disCount = dataGridView1.DisplayedRowCount(true);

 

4.大小和位置設定

  1. Size要和AddControl()保持一致。
  2. Location要和AddControl()保持一致。
                Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);

                Chx[i].Size = new Size((rect.Width / 3), rect.Height);
                Chx[i].Location = new Point(rect.Left + (rect.Width / 3), rect.Top);
                Chx[i].Visible = true;

                Btn[i].Size = new Size((rect.Width / 3), rect.Height);
                Btn[i].Location = new Point(rect.Left + (rect.Width * 2 / 3), rect.Top);
                Btn[i].Visible = true;