Combobox mit Datenbindung

Hier werden Fragen zur Programmierung des ppsGrids...
Post Reply
deBloi
Posts: 2
Joined: 24.06.2010, 10:53
Spamschutz: 9

Combobox mit Datenbindung

Post by deBloi »

Hallo,

meine Aufgabenstellung ist folgende:
Ich fülle ein Grid mit x zeilen und binde in jede Zeile der Spalte 2 eine Combobox ein.
Diese werden per Datenbindung gefüllt

Code: Select all


mycbo = New ComboBox
With mycbo
                   .DataSource = dt
                   .DisplayMember = "Pk_Name"
                   .ValueMember = "Pk_ID"
                   .DropDownStyle = ComboBoxStyle.DropDownList
End With

myGrid.Cells(2, i).ControlDisplayInfo.Control = mycbo
myGrid.Cells(2, i).ControlDisplayInfo.PropertyName = "SelectedItem"

mycbo = New ComboBox
                With mycbo
                    .DataSource = dt
                    .DisplayMember = "Pk_Name"
                    .ValueMember = "Pk_ID"
                    .DropDownStyle = ComboBoxStyle.DropDownList
                End With

myGrid.Cells(2, i).ControlEditInfo.Control = mycbo
myGrid.Cells(2, i).ControlEditInfo.PropertyName = "SelectedItem"
Dies funktioniert auch tadellos und ich kann sowohl gewählte Values, Items etc. abfragen

Jedoch will es mir nicht gelingen, nach dem Füllen der Comboboxen die SelectedValue Eigenschaft zu setzen um so einen Eintrag per Value vorzuwählen.

Wenn ich z.B.

Code: Select all

myCbo.SelectedValue = 2
festlege, bekomme ich zwar keine Exception, aber es wird auch kein Eintrag aus der Combobox gewählt

Wie kann ich dies erreichen?

Ich benutze Visual Studio 2005 und ppsGrid Version 1.3.4.1

Danke schon mal im vorraus
Mirko

deBloi
Posts: 2
Joined: 24.06.2010, 10:53
Spamschutz: 9

Re: Combobox mit Datenbindung

Post by deBloi »

Hallo Torsten,

habe das Problem leider noch immer nicht in den Griff bekommen und schon etliche Szenarien durchprobiert.
Wenn ich z.B. in den Zeilen

Code: Select all

myGrid.Cells(2, i).ControlDisplayInfo.PropertyName = "SelectedItem"


und

Code: Select all

myGrid.Cells(2, i).ControlEditInfo.PropertyName = "SelectedItem"
den Wert auf SelectedValue ändere wirft er mir folgende Exception

Code: Select all

System.Reflection.TargetInvocationException wurde nicht behandelt.
  Message="Ein Aufrufziel hat einen Ausnahmefehler verursacht."
  Source="mscorlib"
  StackTrace:
       bei System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       bei System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       bei System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
       bei System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
       bei ppsGrid.ControlDisplayInfo.SetValue(Object value)
       bei ppsGrid.GridCell.PaintControl(Graphics gfx, ControlDisplayInfo ctrlInfo, SolidBrush brsh, Rectangle clpRct, Int32 x, Int32 y, Int32 w, Int32 h)
       bei ppsGrid.GridCell._Paint(Graphics gfx, Int32 x, Int32 y, Rectangle clpRct, Boolean alpha)
       bei ppsGrid.Grid.PaintCells(Graphics gfx, Int32 lft, Int32 top, Int32 pX, Int32 pY, Rectangle clpRct)
       bei ppsGrid.Grid.OnPaint(PaintEventArgs e)
       bei System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
       bei System.Windows.Forms.Control.WmPaint(Message& m)
       bei System.Windows.Forms.Control.WndProc(Message& m)
       bei System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       bei System.Windows.Forms.ContainerControl.WndProc(Message& m)
       bei System.Windows.Forms.UserControl.WndProc(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.Run(ApplicationContext context)
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       bei CaterKing_Kundenverwaltung.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
       bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
Irgendwie trete ich auf der Stelle.
Kannst Du mir dabei helfen? Ist dieses Forum noch aktiv?
Bin für jede Hilfe Dankbar und hoffe sehr, dass mir trotz Fußball WM jemand helfen kann. ;-)

Danke und Lg Mirko

torsten
Site Admin
Posts: 92
Joined: 02.05.2006, 09:04
Spamschutz: 10
Location: Bremen

Re: Combobox mit Datenbindung

Post by torsten »

Hallo Mirko
Entschuldige, dass ich mich erst jetzt melde...

Infos zu den "Embedded Controls" findest du in der Hilfe unter
"ppsGrid.ControlDisplayInfo.Control" oder "ppsGrid.ControlEditInfo.Control"

Wenn alle Zeilen( (außer die FixedCells) die Combobox anzeigen sollen,
solltest du nur eine einzige Combobox erzeugen und diese der Spalte
(und nicht den Zellen) zuordnen:

Code: Select all

string[]  items = new String[]{
  "Gabi", "Thomas","Andreas","Torsten","Claus",          // Diese Werte in der ComboBox
  "Martina","Bettina","Manfred","Joachim","Peter"};      // ..zur Auswahl anbieten
ComboBox  cbo;                                           // ComboBox zur Darstellung der Items
GridCol   col=grid.Cols[2];                              // erste (und einzige) Spalte 

cbo = new ComboBox();                                    // ComboBox zur Darstellung erzeugen
cbo.Dock = DockStyle.Top;                                // ..am oberen Rand der Zelle zeigen
cbo.Items.AddRange(items);                               // ..Liste der Namen setzen
cbo.DropDownStyle = ComboBoxStyle.DropDownList;          // ..Modus: Nur Auswahl
col.ControlDisplayInfo.Control = cbo;                    // Spalte 0: Daten zeigen durch ComboBox
col.ControlDisplayInfo.PropertyName = "SelectedItem";
Das obige ist die reine Anzeige der Daten in der Combobox.
Wenn du Änderungen ermöglichen willst, musst du zusätzlich "ControlEditInfo" verwenden:

Code: Select all

cbo = new ComboBox();                                    // ComboBox zum editieren der Daten erzeugen
cbo.Dock = DockStyle.Top;                                // ..am oberen Rand der Zelle zeigen
cbo.Items.AddRange(items);                               // ..Liste der namen setzen
cbo.DropDownStyle = ComboBoxStyle.DropDownList;          // ..Modus: Nur Auswahl
col.ControlEditInfo.Control = cbo;                       // Daten ändern durch ComboBox
col.ControlEditInfo.PropertyName = "SelectedItem";
Auch hier gilt die Zuordnung an die Spalte.

Wenn du bestimmte Werte anzeigen möchtest, musst du die
Zellenwerte entspr. belegen:

Code: Select all

foreach (GridRow r in grid1.Rows)
    if (r.Fixed==false)
        r[2].Value=items[rnd.Next(items.Length)];
Den Zellenwert "Value" setzen: grid1[col, row].Value = "Andreas"
Bedenke: Die Zuordnung zur Combobox erfolgt in deinem Beispiel
über die Eigenschaft "SelectedItem", deshalb "Andreas" und nicht der Index

Ein Demo findest du auch im Demo-Programm unter
"http://www.ppsgrid.de/download/ppsGrid_Demo_vs.zip"
in der Datei "winDemo/frm/frmCtrlCombobox.cs"

Gruß Torsten

Post Reply