简体   繁体   English

在DataGridView中查找按钮控件

[英]Find button control inside DataGridView

How do I find button control inside DataGridView, I would do something like this in asp.net for GridView 如何在DataGridView中找到按钮控件,我会在asp.net中为GridView做类似的事情

    foreach (GridViewRow row in gridviewname.Rows)
    {
         Button btnColor= (Button)row.FindControl("btnColor");
    }

But I'm unable to-do the same in Windows forms application.Basically I'm trying to set property of button such as color,text etc. 但是我无法在Windows窗体应用程序中做同样的事情。基本上我正在尝试设置按钮的属性,例如颜色,文本等。

  1. If you do not need to change individual buttons you can customize the entire button column through its properties : 如果您不需要更改单个按钮,则可以通过其属性自定义整个按钮列:

    You could get it during runtime like: 您可以在运行时获取它,例如:

     buttonColumn = (DataGridViewButtonColumn)dataGridView.Columns .Where(column => column is DataGridViewButtonColumn) .First(); 
  2. For individual cells you could try to change cell's Style property 对于单个单元格,您可以尝试更改单元格的Style属性

     Boolean colorFlag = false; foreach (GridViewRow row in gridviewname.Rows) { row[buttonColumn].Style.BackColor = (colorFlag) : Colors.Green? Colors.Red; colorFlag = !colorFlag; } 

    But I'm not sure however that it will work - Change Color of Button in DataGridView Cell . 但是我不确定它是否会工作- 在DataGridView Cell中更改Button的颜色

In datagridview you can get button through column. datagridview您可以通过列获取按钮。

this.mydatagridview.Columns("MybuttonColumn").DefaultCellStyle

Or if you have predefined columns then: 或者,如果您有预定义的列,则:

this.MyButtonColumn.DefaultCellStyle

Or if you use Visual Studio designer, then you can set column style values through designer. 或者,如果您使用Visual Studio设计器,则可以通过设计器设置列样式值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM