I was planning to make the DataGridViewComboboxCell to display similar to a 3D Fixed Style of a textbox. I manage to do it with a Combobox using this code:
public Form1()
{
cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
cmbbox.DrawItem += ComboBox_DrawItem_3DFixed;
}
private void ComboBox_DrawItem_3DFixed(object sender, DrawItemEventArgs e)
{
ComboBox cmb = sender as ComboBox;
e.DrawBackground();
if (e.State == DrawItemState.Focus)
e.DrawFocusRectangle();
var index = e.Index;
if (index < 0 || index >= cmb.Items.Count)
return;
var item = cmb.Items[index];
string text = (item == null) ? "(null)" : cmb.GetItemText(item);
using (var brush = new SolidBrush(e.ForeColor))
{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString(text, e.Font, brush, e.Bounds);
}
}
Unfortunately, I don't know how to do it with the DataGridViewComboboxCell. Tho I did found a solution here:
public void Form1()
{
dgView.CellPainting += dgView_EditingControlShowing;
}
void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
ComboBox cb = (ComboBox)e.Control;
cb.DrawMode = DrawMode.OwnerDrawFixed;
cb.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem_3DFixed);
}
}
But the problem with this, it only changes the appearance of the DataGridViewComboboxCell when the specific cell is clicked, and when it lose focus, it returns back to normal.
I did find the CellPainting Event, but I don't know how it works for this code. Can anyone help me? Thanks!