I have a DataGridView object:
dataGridView1.DataSource = an.peaks;
(an.peaks is a List<Point> object. Point type has 3 properties: x,y,z)
witch generates the next table on run-time: (Apparently I cant upload an Image yet because I’m a new user so I’ll try to draw it 🙂
____|_x__|_y__|_z__|[new column ]
____|_11_|_12_|_13_|[text/button] <==\
____|_20_|_30_|_40_|[text/button] <== } Add text if something or button if something else.
____|_50_|_60_|_70_|[text/button] <==/
I would like to add buttons (as shown in the image/drawing) in a new column to each row that satisfying some condition. If the condition is not satisfied add some text instead.
Example: If the point already exist in the database show it’s substance name (each point represent a substance). If not add a button “ADD” to the corresponding row that will add the new point to the database.
The conditions are not the problems – they are only for examples. The problem is adding the buttons/text to each row and the clicking event for the new button/s.
This is actually quite simple to do using the
DataGridView. What you need to do is:Add column of type DataGridViewButtonColumn
DataGridViewButtonColumnis a standardDataGridViewcolumn type. It can be added through the designer but I generally prefer using code (usually in the form constructor).Setting
UseColumnTextForButtonValuetrue means that theTextproperty gets applied to all buttons giving them the “ADD” button text. You can also useDataPropertyNameto point at a column in the grid’s datasource to provide the button text, or you can even set each cell’s value directly.Change buttons to text
Once you have your button column you then want to turn particular buttons to text. You do this by replacing a cell of button type with one of text type. You can do this many places but one of the best is in the
DataBindingCompleteevent handler – this event fires once the grid is bound and ready to display but before it is painted.Below I simply grab the row with index 1 but you can also inspect each rows
Valueproperty.Respond to button clicks
The final part of the problem is responding to button clicks. This is a little bit cludgy – you need to either use the
CellClickevent or theEditingControlShowingevent for the entire grid.CellClick
EditingControlShowing
In your case the editing control approach is probably best since it won’t respond to clicking on buttons that have been replaced with text. Also this way is more like how you respond to any other button on a form.