来自 威尼斯国际官方网站 2019-09-19 08:12 的文章
当前位置: 威尼斯国际官方网站 > 威尼斯国际官方网站 > 正文

DataGridView的单元格怎么着安置五个开关控件,合

  前段时间我有一个朋友面试公司的时候遇到这个面试题,他也给了份原题给我瞧瞧,并没有什么特别的要点,关于这一类问题,如何在网格上的单元格嵌入多个控件(如按钮、超链接等)问题,我在网上搜索了下这类问题,发现很多解答但是都杂乱,本篇文章帮助大家了解如何应对这类问题。

先通过在IDE中拉出这样的效果图 以学习相关属性

  微软提供的DataGirdView网格控件可通过GetCellDisplayRectangle()方法将需要的控件嵌入单元格中,如:

威尼斯国际官方网站 1

this.Load += Form1_Load;

上面效果的相关代码

void Form1_Load(object sender, EventArgs e)
{
  MulAutoBtnEdit();
}

 

private void MulAutoBtnEdit()
{
  this.dataGridView1.Columns.Add("ColBtnEdit", "嵌入操作按钮");
  this.dataGridView1.Columns["ColBtnEdit"].Width = 150;
  int index = this.dataGridView1.Columns["ColBtnEdit"].Index;
  this.dataGridView1.Columns["ColBtnEdit"].Resizable = DataGridViewTriState.False;
  Button btnAdd = GetBtnByType("BtnAdd","新增");
  Button btnEdit = GetBtnByType("BtnEdit", "修改");
  Button btnDel = GetBtnByType("BtnDel", "删除");
  this.dataGridView1.Controls.Add(btnAdd);
  this.dataGridView1.Controls.Add(btnEdit);
  this.dataGridView1.Controls.Add(btnDel);
  Rectangle rectangle = this.dataGridView1.GetCellDisplayRectangle(index, 0, true);//获取当前单元格上的矩形区域
  btnAdd.Size = btnEdit.Size = btnDel.Size = new Size(rectangle.Width / 3 + 1, rectangle.Height);
  btnAdd.Location = new Point(rectangle.Left, rectangle.Top);
  btnEdit.Location = new Point(rectangle.Left + btnAdd.Width, rectangle.Top);
  btnDel.Location = new Point(rectangle.Left + btnAdd.Width + btnDel.Width, rectangle.Top);
}

威尼斯国际官方网站 2威尼斯国际官方网站 3Code
#region Windows 窗体设计器生成的代码

private Button GetBtnByType(string strBtnName,string strBtnText)
{
  Button btn = new Button();
  btn.Name = strBtnName;
  btn.Text = strBtnText;
  btn.Click += btn_Click;
  return btn;
}

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.tableLayoutPanel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.button1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.button2, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.button3, 1, 1);
            this.tableLayoutPanel1.Controls.Add(this.button4, 0, 2);
            this.tableLayoutPanel1.Controls.Add(this.button5, 0, 3);
            this.tableLayoutPanel1.Controls.Add(this.button6, 1, 2);
            this.tableLayoutPanel1.Location = new System.Drawing.Point(46, 35);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 5;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(200, 195);
            this.tableLayoutPanel1.TabIndex = 1;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(4, 4);
            this.button1.Name = "button1";
            this.tableLayoutPanel1.SetRowSpan(this.button1, 2);
            this.button1.Size = new System.Drawing.Size(75, 79);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(103, 4);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this.button3.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.button3.Location = new System.Drawing.Point(112, 53);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            // 
            // button4
            // 
            this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.button4.Location = new System.Drawing.Point(4, 90);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(92, 36);
            this.button4.TabIndex = 3;
            this.button4.Text = "button4";
            this.button4.UseVisualStyleBackColor = true;
            // 
            // button5
            // 
            this.button5.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.button5.Location = new System.Drawing.Point(12, 139);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 4;
            this.button5.Text = "button5";
            this.button5.UseVisualStyleBackColor = true;
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(103, 90);
威尼斯国际官方网站 ,            this.button6.Name = "button6";
            this.tableLayoutPanel1.SetRowSpan(this.button6, 2);
            this.button6.Size = new System.Drawing.Size(75, 79);
            this.button6.TabIndex = 5;
            this.button6.Text = "button6";
            this.button6.UseVisualStyleBackColor = true;
            // 
            // tableLayoutPanelForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 265);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "tableLayoutPanelForm";
            this.Text = "tableLayoutPanelForm";
            this.tableLayoutPanel1.ResumeLayout(false);
            this.ResumeLayout(false);

private void btn_Click(object sender, EventArgs e)
{
  if(sender is Button)
  {
    Button btn = (Button)sender;
    MessageBox.Show(string.Format("点击按钮:{0}",btn.Text));
  }
}

        }

  效果图:

        #endregion

威尼斯国际官方网站 4

 

  通过以上的操作即可将多个按钮嵌入到单元格中,不考虑维护性、效率问题,只在于给予大家思路,对于“超链接”控件等都可用类似的方法处理,类似网址如下:

在上面属性学习的基础上 通过编码来创建我们需要的TableLayoutPanel的相关布局

 

下面想要实现的效果为:

  在网上发行另一种方式,思路差不多,地址我也贴出来:

点击一次按钮 TableLayoutPanel将新增两行

  

此两行的相关控件布局如下

  关于第三方DevExpress组件上的网格如GridView等,本人并没有找到好的解决上面问题的方法,有不足之处,知情人事可在留言板上赐教。

button1    button2

  A young idler ~ an old beggar !

               button3

再点击一次按钮 将又会新增两行

button4   button5

              button6

本文由威尼斯国际官方网站发布于威尼斯国际官方网站,转载请注明出处:DataGridView的单元格怎么着安置五个开关控件,合

关键词: