1
我打算讓DataGridViewComboboxCell顯示類似於文本框的3D固定樣式。我管理與使用此代碼一個ComboBox做到這一點:在3d樣式中顯示DataGridViewComboBoxColumn
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);
}
}
不幸的是,我不知道如何使用的DataGridViewComboBoxCell做到這一點。芹苴我沒有在這裏找到一個解決方案:
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);
}
}
但問題與此,它只會改變的DataGridViewComboBoxCell的外觀點擊特定的小區時,當它失去焦點,它返回到正常。
我找到了CellPainting事件,但我不知道它是如何工作的這段代碼。誰能幫我?謝謝!
看起來不錯,但有什麼辦法,我可以改變ControlPaint.DrawComboButton的外觀,它看起來像是3D Fixed。 –
你希望它是3D。你不是嗎? –
我其實想要在圖像的頂部做外觀,下面的是我得到的。 Image here:https://i.imgsafe.org/6467d94258.jpg –