private object GetDataSourceValue( DataGridViewComboBoxExColumn oc, ComboBoxEx di, object key) { if (key != null && string.IsNullOrEmpty(di.ValueMember) == false && string.IsNullOrEmpty(di.DisplayMember) == false) { CurrencyManager cm = GetCurrencyManager(oc, di); if (cm != null) { string t = key.ToString(); for (int i = 0; i < cm.List.Count; i++) { if (cm.List[i] is DataRowView) { DataRowView drView = (DataRowView) cm.List[i]; string s = drView[di.DisplayMember].ToString(); if (t.Equals(s) == true) return (drView[di.ValueMember]); } else if (cm.List[i] is string) { if (t.Equals(cm.List[i]) == true) return (key); } else { object o = cm.List[i]; object value = o.GetType().GetProperty(di.DisplayMember).GetValue(o, null); if (value is string) { if (t.Equals(value) == true) { value = o.GetType().GetProperty(di.ValueMember).GetValue(o, null); return (value); } } } } } } return (key); }
private CurrencyManager GetCurrencyManager( DataGridViewComboBoxExColumn oc, ComboBoxEx di) { if (oc.CurrencyManager == null) { if (DataGridView != null && DataGridView.BindingContext != null && di.DataSource != null && di.DataSource != Convert.DBNull) { oc.CurrencyManager = (CurrencyManager) DataGridView.BindingContext[di.DataSource]; } } return (oc.CurrencyManager); }
/// <summary> /// DrawControl /// </summary> /// <param name="di"></param> /// <param name="r"></param> /// <param name="bm"></param> /// <param name="g"></param> /// <param name="oc"></param> /// <param name="s"></param> private void DrawControl(ComboBoxEx di, Rectangle r, Bitmap bm, Graphics g, DataGridViewComboBoxExColumn oc, string s) { Rectangle t = di.GetThumbRect(new Rectangle(0, 0, r.Width, r.Height)); if (t.IsEmpty == false) { // Work around Windows XP and Windows DropDownList // DrawToBitmap problems if (MustRenderVisibleControl(di.DropDownStyle) == true) { di.Location = oc.DataGridView.Location; if (di.Parent == null) { Form form = oc.DataGridView.FindForm(); if (form != null) di.Parent = form; } di.SendToBack(); di.Visible = true; } using (Bitmap bm2 = new Bitmap(bm)) { di.Bounds = r; di.DrawToBitmap(bm2, r); t.X += r.X; t.Y += r.Y; g.DrawImage(bm2, t, t, GraphicsUnit.Pixel); if (t.Left < r.Right) r.Width -= (r.Right - t.Left - 3); } di.Visible = false; } DrawText(di, r, g, s); }