在datagridview中使用按钮的字段 [英] Field with a button in the datagridview

查看:79
本文介绍了在datagridview中使用按钮的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有界的Da​​taGridView。如何在一个字段中添加数据按钮?我将附上我如何看待它的屏幕截图 https://ibb.co/gjQi8H 。你有什么建议吗?



我尝试过:



它是WinForms,我认为我需要编写一个自定义列类型。



  class  TextAndButtonControl:UserControl 
{
private TextBox textbox1;
private 按钮button1;

public TextAndButtonControl()
{
this .textbox1 = new TextBox();
this .Controls.Add( this .textbox1);
this .button1 = new Button();
this .Controls.Add( this .button1);
this .RenderControl();
this .button1.Click + = new EventHandler(button1_Click);
}

void button1_Click( object sender,EventArgs e)
{
MessageBox.Show( );
}

public string 文字
{
获取 {返回 。 textbox1.Text; }
set { this .textbox1.Text = ; }
}

public string ButtonText
{
获取 {返回 .button1.Text; }
set { this .button1.Text = ; }
}

public void RenderControl()
{
this .textbox1.Location = new Point( 0 0 );
.textbox1.Width = 2 * .Width / 3 ;
.textbox1.Height = .Height;
this .button1.Location = new Point( 2 * .Width / 3 0 );
.button1.Width = .Width / 3 ;
this .button1.Height = this .Height;
}
}





然后以主要形式:



  private   void  Form1_Load( object  sender,EventArgs e)
{
TextAndButtonControl bcol = new TextAndButtonControl();
bcol.Text = 按钮列;
bcol.ButtonText = 点击我;
bcol.Name = btnClickMe;
bcol.RenderControl();
dgMainGrid.Controls.Add(bcol);
}





但我得到了: https://ibb.co/dgTaNc

解决方案

请在此处查看答案:在C#Windows应用程序中向DataGridView添加命令按钮 [ ^ ]

如果您只想让按钮出现在活动单元格中,您将不得不做一些自定义单元格绘制我害怕,请参阅: DataGridView.CellPainting事件(System.Windows.Forms) [ ^ ]

另一种方式可能是这样做ComboBox示例: DataGridView.CellPainting事件(System.Windows.Forms) [ ^

I have a bounded DataGridView. How can I add a button in one field with data? I will attach a screenshot of how I see it https://ibb.co/gjQi8H. Do you have any recommendations on this?

What I have tried:

it's WinForms and I think that I need to write a custom column type.

class TextAndButtonControl : UserControl
{
   private TextBox textbox1;
   private Button button1;

   public TextAndButtonControl()
   {
       this.textbox1 = new TextBox();
       this.Controls.Add(this.textbox1);
       this.button1 = new Button();
       this.Controls.Add(this.button1);
       this.RenderControl();
       this.button1.Click += new EventHandler(button1_Click);
   }

   void button1_Click(object sender, EventArgs e)
   {
       MessageBox.Show("Hi");
   }

   public string Text
   {
       get { return this.textbox1.Text; }
       set { this.textbox1.Text = value; }
   }

   public string ButtonText
   {
       get { return this.button1.Text; }
       set { this.button1.Text = value; }
   }

   public void RenderControl()
   {
       this.textbox1.Location = new Point(0, 0);
       this.textbox1.Width = 2 * this.Width / 3;
       this.textbox1.Height = this.Height;
       this.button1.Location = new Point(2 * this.Width / 3, 0);
       this.button1.Width = this.Width / 3;
       this.button1.Height = this.Height;
    }
}



Then in main form:

private void Form1_Load(object sender, EventArgs e)
{
    TextAndButtonControl bcol = new TextAndButtonControl();
    bcol.Text = "Button Column ";
    bcol.ButtonText = "Click Me";
    bcol.Name = "btnClickMe";
    bcol.RenderControl();
    dgMainGrid.Controls.Add(bcol);
}



But I'm getting: https://ibb.co/dgTaNc

解决方案

See the answer here: Add Command Button to DataGridView in C# Windows Application[^]
If you only want the button to appear in the active cell, you will have to do some custom cell painting I'm afraid, see: DataGridView.CellPainting Event (System.Windows.Forms)[^]
Another way might be doing it like in this ComboBox example: DataGridView.CellPainting Event (System.Windows.Forms)[^]


这篇关于在datagridview中使用按钮的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆