private void RefreshData() { LogTableTableAdapter logMsgAd = new LogTableTableAdapter(); LogMessageTypesTableAdapter logMsgTypesAd = new LogMessageTypesTableAdapter(); string dbPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DB\\SampleDB.mdb"); logMsgAd.Connection.ConnectionString = string.Format(MDBConnFmt, dbPath); logMsgTypesAd.Connection.ConnectionString = string.Format(MDBConnFmt, dbPath); logMsgTypesAd.Fill(ds.LogMessageTypes); logMsgAd.Fill(ds.LogTable); dataGridView1.DataSource = ds; dataGridView1.DataMember = ds.LogTable.TableName; int position = dataGridView1.Columns.Count; if (dataGridView1.Columns.Contains(ds.LogTable.TypeColumn.ColumnName)) { position = dataGridView1.Columns[ds.LogTable.TypeColumn.ColumnName].Index; //create the multicolumncombo column DataGridViewMultiColumnComboColumn newColumn = new DataGridViewMultiColumnComboColumn(); //DataGridViewComboBoxColumn newColumn = new DataGridViewComboBoxColumn(); newColumn.CellTemplate = new DataGridViewMultiColumnComboCell(); //newColumn.CellTemplate = new DataGridViewComboBoxCell(); newColumn.DataSource = ds.LogMessageTypes; newColumn.HeaderText = ds.LogTable.TypeColumn.ColumnName; newColumn.DataPropertyName = ds.LogTable.TypeColumn.ColumnName; newColumn.DisplayMember = ds.LogMessageTypes.TypeNameColumn.ColumnName; newColumn.ValueMember = ds.LogMessageTypes.TypeIdColumn.ColumnName; newColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; dataGridView1.Columns.Remove(ds.LogTable.TypeColumn.ColumnName); dataGridView1.Columns.Insert(position, newColumn); dataGridView1.Columns[position].Width = 300; //newColumn.Items.Clear(); //foreach(SampleDBDataSet.LogTableRow row in ds.LogMessageTypes) //{ // newColumn.Items.Add(row); //} } }
/**************************************************************************************************/ protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { Rectangle rec = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height); DataGridViewMultiColumnComboColumn column = ownerCell.OwningColumn as DataGridViewMultiColumnComboColumn; DataTable valuesTbl = column.DataSource as DataTable; string joinByField = column.ValueMember; SolidBrush NormalText = new SolidBrush(System.Drawing.SystemColors.ControlText); //If there is an item if (e.Index > -1) { DataRowView currentRow = Items[e.Index] as DataRowView; if (currentRow != null) { DataRow row = currentRow.Row; string currentText = GetItemText(Items[e.Index]); //first redraw the normal while background SolidBrush normalBack = new SolidBrush(Color.White); //TODO: fix to be system edit box background e.Graphics.FillRectangle(normalBack, rec); if (DroppedDown && !(Margin.Top == rec.Top)) { int currentOffset = rec.Left; SolidBrush HightlightedBack = new SolidBrush(System.Drawing.SystemColors.Highlight); if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { //draw selected color background e.Graphics.FillRectangle(HightlightedBack, rec); } bool addBorder = false; object valueItem; foreach (object dataRowItem in row.ItemArray) { valueItem = dataRowItem; string value = dataRowItem.ToString(); //TODO: support for different types!!! if (addBorder) { //draw dividing line //currentOffset ++; SolidBrush gridBrush = new SolidBrush(Color.Gray); //TODO: make the border color configurable long linesNum = lineWidth; while (linesNum > 0) { linesNum--; Point first = new Point(rec.Left + currentOffset, rec.Top); Point last = new Point(rec.Left + currentOffset, rec.Bottom); e.Graphics.DrawLine(new Pen(gridBrush), first, last); currentOffset++; } } else { addBorder = true; } SizeF extent = e.Graphics.MeasureString(value, e.Font); decimal width = (decimal)extent.Width; //measure the string that we are goin to draw and cut it with wrapping if too large Rectangle textRec = new Rectangle(currentOffset, rec.Y, (int)decimal.Ceiling(width), rec.Height); //now draw the relevant to this column value text if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { //draw selected SolidBrush HightlightedText = new SolidBrush(System.Drawing.SystemColors.HighlightText); //now redraw the backgrond it order to wrap the previous field if was too large e.Graphics.FillRectangle(HightlightedBack, currentOffset, rec.Y, fixedAlignColumnSize, extent.Height); //draw text as is e.Graphics.DrawString(value, e.Font, HightlightedText, textRec); } else { //now redraw the backgrond it order to wrap the previous field if was too large e.Graphics.FillRectangle(normalBack, currentOffset, rec.Y, fixedAlignColumnSize, extent.Height); //draw text as is e.Graphics.DrawString(value, e.Font, NormalText, textRec); } //advance the offset to the next position currentOffset += fixedAlignColumnSize; } } else { //if happens when the combo is closed, draw single standard item view e.Graphics.DrawString(currentText, e.Font, NormalText, rec); } } } }