3

I'm a beginner in C# and I'm trying to style a DataGridView button in Windows Forms.

I don't know how to, as example, remove the border from a button or change the hover color. Many configurations that are on a normal button are missing in the DataGridView settings.

How can I achieve a full editable button inside DataGridView?

Steven
  • 1,996
  • 3
  • 22
  • 33

1 Answers1

0

Datagridview works a little different than normal buttons, but you can still edit several things in it if you search for the subproperties inside the properties. Let's go in a bit of detail:

DefaultCellStyle

Once you've selected your dataGridView, go to properties > RowTemplate. In there, you find something called DefaultCellStyle. if you press on the '...' at the right. then it'll open a popup that allows you to change some of the standard design of the Cells. The same can also be applied to ColumnHeadersDefaultCellStyle, which is almost the same as DefaultCellStyle.

Columns

You can also go to Columns and add a new Column. After you've added a new column, you're also able to set several properties unique to that Column as well. You can even set all the cells in a Column to act as buttons! Datagridview has certainely the access to customise, but most of them are divided in the cellstyles and column collections.

Changing the backcolor on hover

This in't as easily done as on a normal button, I did a search and came with this solution:

In properties, at the lightning button, you can see the events, there you can doubleclick on 'CellMouseMove' and add this. (Change the datagridview1 name eventually to be equall to yours)

private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Blue;
}

Then doubleclick the 'CellMouseLeave' event so it can revert the color.

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
}

I hope this has helped you further.

Steven
  • 1,996
  • 3
  • 22
  • 33
  • Thanks but I still cannot find way ho to change for example hover color on DataGridView Button. –  Oct 20 '17 at 13:00
  • I've updated the answer with a way to help you with specifically the hover function, but that's all I can do now. – Steven Oct 20 '17 at 13:26
  • I cannot get rid out of gray hover effect when i hover button. I am changing color by CellMouseEnter event but still getting gray. –  Oct 20 '17 at 13:43
  • Btw. I am using Flat style. https://imgur.com/a/rNCjN <- Example. Green is normal and gray is hovered even my function to MouseEnter red color works. –  Oct 20 '17 at 13:44
  • I'm trying to understand, so the MouseMove and MouseEnter events works, but the MouseLeave doesn't? weird...Can you show me what you got in the code now? – Steven Oct 20 '17 at 14:00
  • Everythong works well, but I am getting still gray button even I have red set. –  Oct 20 '17 at 15:21