Hallo
ich versuche grade eine ComboBox in ein DataGrid zubekommen
Ich habe im Netz diesen Code dazu gefunden.
Die Class
Und so spreche ich die ComboBox an
Und dann noch die Schleife
Nur er zeigt mir nix im DataGrid an.
Weiß jemand Rat?
ich versuche grade eine ComboBox in ein DataGrid zubekommen
Ich habe im Netz diesen Code dazu gefunden.
Die Class
Code:
unit gDataGridComboBoxColumnU; interface uses System.Windows.Forms, System.Drawing, System.Data; type DataGridComboBoxColumn = class(System.Windows.Forms.DataGridTextBoxColumn) private { Private-Deklarationen } cm: CurrencyManager; intCurrentRow: Integer; cbb: Combobox; // Hosted combobox control procedure comboBox_Leave(sender: System.Object; e: EventArgs ); procedure DataGrid_Scroll(sender: System.Object; e: EventArgs); protected procedure Edit(source: System.Windows.Forms.CurrencyManager; rowNum: Integer; bounds: System.Drawing.Rectangle; readOnly: Boolean; instantText: string; cellIsVisible: Boolean); override; function GetColumnValueAtRow(source: System.Windows.Forms.CurrencyManager; rowNum: Integer): System.Object; override; procedure SetColumnValueAtRow(source: System.Windows.Forms.CurrencyManager; rowNum: Integer; value: System.Object); override; public constructor Create; function Combobx: Combobox; end; implementation constructor DataGridComboBoxColumn.Create; {Constructor - create combobox, register selection change event handler, register lose focus event handler} begin inherited Create; // Create combobox and force DropDownList style: cbb := Combobox.Create; cbb.DropDownStyle := ComboBoxStyle.DropDownList; // Add event handler for notification when combobox loses focus: Include(Self.cbb.Leave, Self.comboBox_Leave); end; function DataGridComboBoxColumn.Combobx: Combobox; begin Result := cbb; end; procedure DataGridComboBoxColumn.comboBox_Leave(sender: System.Object; e: EventArgs ); {On combobox losing focus, set the column value, hide the combobox, and unregister scroll event handler} var str: string; rowView: DataRowView; begin rowView := DataRowView(cbb.SelectedItem); str := string(rowView.Row[cbb.DisplayMember]); SetColumnValueAtRow(cm, intCurrentRow, str); Invalidate; cbb.Hide; Exclude(DataGridTableStyle.DataGrid.Scroll, DataGrid_Scroll); end; procedure DataGridComboBoxColumn.Edit(source: System.Windows.Forms.CurrencyManager; rowNum: Integer; bounds: System.Drawing.Rectangle; readOnly: Boolean; instantText: string; cellIsVisible: Boolean); // On edit, add scroll event handler, and display combobox var rect: Rectangle; begin inherited Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible); if (not readOnly and cellIsVisible) then begin // Save current row in the DataGrid and currency manager // associated with the data source for the DataGrid intCurrentRow := rowNum; cm := source; // Add event handler for DataGrid scroll notification include(DataGridTableStyle.DataGrid.Scroll, DataGrid_Scroll); // Site the combobox control within the current cell cbb.Parent := TextBox.Parent; rect := DataGridTableStyle.DataGrid.GetCurrentCellBounds; cbb.Location := rect.Location; cbb.Size := Size.Create(TextBox.Size.Width, cbb.Size.Height); // Set combobox selection to given text cbb.SelectedIndex := cbb.FindStringExact(TextBox.Text); // Make the combobox visible and place on top textbox control cbb.Show; cbb.BringToFront; cbb.Focus; end; end; function DataGridComboBoxColumn.GetColumnValueAtRow( source: System.Windows.Forms.CurrencyManager; rowNum: Integer): System.Object; {Given a row, get the value member associated with a row. Use the value member to find the associated display member by iterating over bound data source} var obj: System.&Object; dv: DataView; i: Integer; begin // Given a row number in the DataGrid, get the display member obj := inherited GetColumnValueAtRow(source, rowNum); // Iterate through the data source bound to the ColumnComboBox cm := CurrencyManager(DataGridTableStyle.DataGrid.BindingContext[cbb.DataSource]); // Assumes the associated DataGrid is bound to a DataView or // DataTable dv := DataView(cm.List); for i := 0 to dv.Count - 1 do if (obj.Equals(dv[i][cbb.ValueMember])) then break; // If set item was found return corresponding value, // otherwise return DbNull.Value if (i < dv.Count) then Result := dv[i][cbb.DisplayMember]; Result := DBNull.Value; end; procedure DataGridComboBoxColumn.SetColumnValueAtRow( source: System.Windows.Forms.CurrencyManager; rowNum: Integer; value: System.Object); {Given a row and a display member, iterate over bound data source to find the associated value member. Set this value member.} var s: System.&Object; dv: DataView; i: Integer; begin s := value; // Iterate through the data source bound to the ColumnComboBox: cm := CurrencyManager( DataGridTableStyle.DataGrid.BindingContext[cbb.DataSource]); // Assumes the associated DataGrid is bound to a DataView or // DataTable dv := DataView(cm.List); for i := 0 to dv.Count - 1do if (s.Equals(dv[i][cbb.DisplayMember])) then break; if i < dv.Count then s := dv[i][cbb.ValueMember] else s := DBNull.Value; inherited SetColumnValueAtRow(source, rowNum, s); end; procedure DataGridComboBoxColumn.DataGrid_Scroll(sender: System.Object; e: EventArgs); // On DataGrid scroll, hide the combobox begin cbb.Hide; end; end.
Und so spreche ich die ComboBox an
Code:
... DGStyle : DataGridTableStyle; ComboBoxCol1 : DataGridComboBoxColumn; begin //DataGridStyle erzeugen DGStyle := System.Windows.Forms.DataGridTableStyle.Create; DGStyle.MappingName := FDbPKMView.Table.TableName; DGStyle.RowHeaderWidth := 25; //Style Farbe DGStyle.AlternatingBackColor := System.Drawing.Color.LightSteelBlue; //DGStyle.SelectionBackColor := Color.DarkOrange; ComboBoxCol1 := DataGridComboBoxColumn.Create; SetLookupBinding(ComboBoxCol1.Combobx, 'KFZ_NAME', 'KFZ_ID', FDbKFZView); ComboBoxCol1.HeaderText := 'KFZ'; ComboBoxCol1.Alignment := horizontalalignment.Left; ComboBoxCol1.NullText := ''; ComboBoxCol1.Width := 150; //Zum Style hinzufügen DGStyle.GridColumnStyles.Clear; DGStyle.PreferredRowHeight := ComboBoxCol1.Combobx.Height + 1; DGStyle.GridColumnStyles.Add(ComboBoxCol1); //Ausfuehrung im Datagrid anzeigen DataGrid1.TableStyles.Clear; DataGrid1.DataSource := FDbPKMView; DataGrid1.TableStyles.Add(DGStyle);
Code:
procedure SetLookupBinding(combobx: System.Windows.Forms.Combobox; DisplayColumn, BoundColumn: string; dv: DataView); begin with combobx do begin DisplayMember := DisplayColumn; end; ValueMember := BoundColumn; DataSource := dv; end;
Nur er zeigt mir nix im DataGrid an.
Weiß jemand Rat?