DataGridView的DataGridViewButtonColumn不会注意到一个真正的按钮 [英] DataGridView DataGridViewButtonColumn doesn't notice a real Button

查看:183
本文介绍了DataGridView的DataGridViewButtonColumn不会注意到一个真正的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢看着我的问题。我有一个名为对象 UIChoice

thanks for looking at my question. I have a Object called UIChoice

namespace uitest
{
  public class UIChoice
  {
      public String name {get; set;}
      public Button yes { get; set; }      
  }
}

然后我有一个 Form1中 DataGridView的(我称之为 grdChoice )这样的

namespace uitest
{
  public class Form1 : Form
  {
    public BindingList<UIChoice> Choices = new BindingList<UIChoice>();
    public Form1 ()
    {
        InitializeComponent();

        DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
        colName.HeaderText = "Name:";
        colName.Name = "yestext";
        colName.DataPropertyName = "name";
        grdChoice.Columns.Add(colName);

        DataGridViewButtonColumn colYes = new DataGridViewButtonColumn();
        colYes.HeaderText = "Yes:";
        colYes.Name = "yesbutton";
        colYes.DataPropertyName = "yes";
        colYes.FlatStyle = FlatStyle.Popup;
        grdChoice.Columns.Add(colYes);

        grdChoice.DataSource = Choices;
        grdChoice.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        grdChoice.Show();  

        FillData();
    }

    private void FillData()
    {
        UIChoice myChoice = new UIChoice();
        Button Yes = new Button();
        Yes.Click += ((sender, args) =>
        {
                MessageBox.Show("try this yes works");
        });
        Yes.Text = "yes";
        myChoice.name = "try this";
        myChoice.yes = Yes;
        Choices.Add(myChoice);

        UIChoice myChoiceA = new UIChoice();
        Button YesA = new Button();
        YesA.Click += ((sender, args) =>
        {
                MessageBox.Show("try A yes works");
        });
        YesA.Text = "yes";
        myChoiceA.name = "try A";
        myChoiceA.yes = YesA;
        Choices.Add(myChoiceA);
    }
  }
}

应该发生什么(在我的想象)是, DataGridView的应该注意到,这是一个 DataGridViewButtonColumn 及注意事项它被赋予一个按钮,并使用它。但事实并非如此。对我来说,问题是,这是一个替代code。在现实生活中的 UIChoice 对象在很大程度上与方法强化和 DataGridView的是(最初)仅仅意味着取代手手摇水平递增按钮(这成为太庞大),不幸的是,我写它的面板这个 DataGridView的当我构建一个对象的BindingList它不工作,即似乎没有通知或照顾,我给它一个按钮..我怎么做,我已经发送到 DataGridView的照顾按钮需要?

what should happen (in my imagination) is that the DataGridView should notice that it is a DataGridViewButtonColumn and notice it has been given a Button and use it. But it doesn't. The problem for me is this is a replacement code. In real life the UIChoice object is heavily fortified with methods and the DataGridView was (originally) merely meant to replace a hand cranked panel of horizontally incrementing buttons (which became too "massive") the sad part is as I have written it this DataGridView doesn't work i.e. when I construct a BindingList object it doesn't seem to "notice" or "care" that I am giving it a button .. how do I make the DataGridView care that I already sent it the Buttons it needs?

推荐答案

关闭。

按钮,希望在单元格值。

The Buttons, hopefully are in the Cell Values.

然而,你不能按他们就这样。

However you can't Click them just like that.

相反,你code中的 grdChoice.CellClick 事件,也许是这样的:

Instead you code the grdChoice.CellClick event, maybe like this:

if (e.ColumnIndex == yourButtonColumnIndex )
{
   Button btn = gradesDataGridView[yourButtonColumnIndex , e.RowIndex].Value as Button;
   if (btn != null) buttons_Click(btn, null);
}

这会触发一个公共事件 buttons_Click 在这里您可以用sender参数辨别按钮和调用适当的code。

This will trigger a common event buttons_Click where you can use the sender parameter to discern the buttons and to call the appropriate code.

这可能是确定的,但也许你宁愿看到右边点击都会自动触发..?

This may be OK but maybe you'd rather see the right Clicks get triggered automatically..?

随着一点点的反思魔术(发现这里在$ C $的CProject)这个工程,以及:

With a little Reflection Magic (found here on CodeProject) this works as well:

if (e.ColumnIndex == yourButtonColumnIndex )
{
   Button btn = gradesDataGridView[yourButtonColumnIndex , e.RowIndex].Value as Button;
   if (btn != null) 
   {
     var handler = (EventHandler)GetDelegate(btn, "EventClick");
     if (handler != null) btn.Invoke(handler);
   }

}

下面是修改code来获得在按钮的事件处理程序:

Here is the modified code to get at the event handler of the Buttons:

private static object GetDelegate(Control issuer, string keyName)
{
    // Get key value for a Click Event
    var key = typeof(Control)
        .GetField(keyName, BindingFlags.Static | BindingFlags.NonPublic | 
                               BindingFlags.FlattenHierarchy)
        .GetValue(null);
    var events = typeof(Component)
        .GetField("events", BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(issuer);
    // Find the Find method and use it to search up listEntry for corresponding key
    var listEntry = typeof(EventHandlerList)
        .GetMethod("Find", BindingFlags.NonPublic | BindingFlags.Instance)
        .Invoke(events, new object[] { key });
    // Get handler value from listEntry 
    var handler = listEntry
        .GetType()
        .GetField("handler", BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(listEntry);
    return handler;
}

荣誉在$ C $的CProject的家伙,Kostikov先生和匿名用户#2573523谁增加了控制的变化,而不是组件。

Kudos to the guys on CodeProject, Mr. Kostikov and the anonymous user #2573523 who added the changes for Controls as opposed to Components..

修改我注意到,按钮栏出现没有文字。要显示你需要code这样的 DataGridView_CellFormatting 事件中的各个按钮文本:

Edit I noticed that the Buttons Column comes up without text. To display the individual buttons' Texts you need to code the DataGridView_CellFormattingevent like this:

private void gradesDataGridView_CellFormatting(
                                 object sender, DataGridViewCellFormattingEventArgs e)
{
   if (e.ColumnIndex == yourBottonColumnIndex )
   {
     Button btn = gradesDataGridView[yourButtonColumnIndex , e.RowIndex].Value as Button;
     if (btn != null) e.Value = btn.Text;
   }
}

注意e.Value不是cell.Value但只显示文本。

Note that e.Value is not the cell.Value but only the displayed text.

这篇关于DataGridView的DataGridViewButtonColumn不会注意到一个真正的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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