【问题标题】:DataGridView Custom control enable button in Unedited mode未编辑模式下的 DataGridView 自定义控件启用按钮
【发布时间】:2014-08-27 05:54:13
【问题描述】:

有谁知道如何让按钮一直可见?(不仅在单元格的编辑模式下) 我想请你注意这个问题的答案。 how to add ellipse button and textbox in current cell of datagridview in winforms

我可以增强此解决方案以始终查看单元格中的按钮控件。我想要的是获得第一次单击单元格的弹出框。这是在未编辑模式下绘制按钮的代码。

 // Finally paint the NumericUpDown control
                    Rectangle srcRect = new Rectangle(0, 0, valBounds.Width, valBounds.Height);
                    if (srcRect.Width > 0 && srcRect.Height > 0)
                    {
                        Bitmap renderingBitmap = new Bitmap(22, 18);
                        new TextButton().button.DrawToBitmap(renderingBitmap, srcRect);
                        graphics.DrawImage(renderingBitmap, new Rectangle(new Point(cellBounds.X+cellBounds.Width-24, valBounds.Location.Y-2), valBounds.Size),
                                           srcRect, GraphicsUnit.Pixel);
                    }

【问题讨论】:

    标签: winforms datagridview


    【解决方案1】:

    更好的选择是在 DataGridView 上嵌入一个按钮。这将使您能够更好地控制 DataGridView 的使用。看下面的sn-p:

    Button b1 = new Button();
    int cRow = 0, cCol = 0;
    private void Form1_Load(object sender, EventArgs e)
    {
      b1.Text = "...";
      b1.Visible = false;
      this.dataGridView1.Controls.Add(b1);
      b1.BringToFront();
      this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
      this.b1.Click += new EventHandler(b1_Click);
    }
    void b1_Click(object sender, EventArgs e)
    {
      //Implement your logic here
    }
    void dataGridView1_Paint(object sender, PaintEventArgs e)
    {
      if(cRow != 0 && cCol != 0)
      {
        Rectangle rect = new Rectangle();
        rect = dataGridView1.GetCellDisplayRectangle(cRow ,cCol , false);
        rect.X = rect.X + (2*dataGridView1.Columns[1].Width / 3);
        rect.Width = dataGridView1.Columns[1].Width / 3;
        b1.Bounds = rect;
        b1.Visible = true;
      }
    }
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      cRow = e.RowIndex;
      cCol = e.ColumnIndex;
    }
    

    在上面的 sn-p 中,省略号按钮的位置设置为最后点击的单元格。单击单元格后,可见性始终为真。在我看来,这样可以更好地控制按钮的功能,并且更易于维护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 2011-05-22
      相关资源
      最近更新 更多