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; }
}
}
then I have a Form1
with a DataGridView
(I call it grdChoice
) like this
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);
}
}
}
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?
Close.
The Buttons, hopefully are in the Cell Values.
However you can't Click them just like that.
Instead you code the
grdChoice.CellClick
event, maybe like this: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..?
With a little Reflection Magic (found here on CodeProject) this works as well:
Here is the modified code to get at the event handler of the Buttons:
Kudos to the guys on CodeProject, Mr. Kostikov and the anonymous user #2573523 who added the changes for Controls as opposed to Components..
Edit I noticed that the Buttons Column comes up without text. To display the individual buttons' Texts you need to code the
DataGridView_CellFormatting
event like this:Note that e.Value is not the cell.Value but only the displayed text.