C# WinForms DataGridView mit BindingSource ist beim Start nicht gefülltC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 C# WinForms DataGridView mit BindingSource ist beim Start nicht gefüllt

Post by Anonymous »

Ich schreibe eine C# WinForms -Anwendung mit dem MVVM -Muster. Daten werden in EF Core gespeichert. Obwohl ich denke, dass ich dem Lehrbuch gefolgt bin.

Code: Select all

public class VmSpools : INotifyPropertyChanged, IVmSpools
{
public BindingSource MotherSpools
{
get { return motherSpools; }
set
{
if (motherSpools == value) return;
motherSpools = value;
OnPropertyChanged(nameof(MotherSpools));
}
}
BindingSource motherSpools;

public event PropertyChangedEventHandler? PropertyChanged;

void OnPropertyChanged(string? propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}

ILifetimeScope compContainer;
public VmSpools(ILifetimeScope compCon)
{
compContainer = compCon;
LoadMotherSpools();
}

private void LoadMotherSpools()
{
using (var ctx = compContainer.Resolve())
{
ctx.MotherSpools.Load();
var ms = new BindingList();
foreach (var m in ctx.MotherSpools)
{
ms.Add(new MotherspoolGridRow(m));
}
MotherSpools = new BindingSource(ms, "MspoolId");
}
}
}
Das Wrapper -Objekt soll AnzeigeNames für die Spalten -Header festlegen.

Code: Select all

public class MotherspoolGridRow : MotherSpool,INotifyPropertyChanged
{
MotherSpool baseObj;

public MotherspoolGridRow(MotherSpool obj)
{
baseObj = obj;
}
[DisplayName("Motherspool ID")]
public new long MspoolId {
get { return baseObj.MspoolId; }
set { if (baseObj.MspoolId == value) return;baseObj.MspoolId = value; OnPropertyChanged(nameof(MspoolId)); }
}

[DisplayName("Created At")]
public new DateTime CreatedAt {
get { return baseObj.CreatedAt; }
set { if (baseObj.CreatedAt == value) return;baseObj.CreatedAt = value; OnPropertyChanged(nameof(CreatedAt)); }
}

[DisplayName("FiberType")]
public new int FiberTypeId {
get { return baseObj.FiberTypeId; }
set { if (baseObj.FiberTypeId == value) return; baseObj.FiberTypeId = value;OnPropertyChanged(nameof(FiberTypeId)); }
}

[DisplayName("Country of Origin")]
public new string CountryOfOrigin {
get { return baseObj.CountryOfOrigin; }
set { if (baseObj.CountryOfOrigin == value) return; baseObj.CountryOfOrigin = value; OnPropertyChanged(nameof(CountryOfOrigin)); }
}

[DisplayName("Original Length")]
public new decimal OriginalLength {
get { return baseObj.OriginalLength; }
set { if (baseObj.OriginalLength == value) return;  baseObj.OriginalLength = value;OnPropertyChanged(nameof(OriginalLength)); }
}

[DisplayName("Remaining Length")]
public new decimal RemainingLength {
get { return baseObj.RemainingLength; }
set { if (baseObj.RemainingLength == value) return; baseObj.RemainingLength = value;OnPropertyChanged(nameof(RemainingLength)); }
}

[DisplayName("Manufacturer SN")]
public new string ManufacturerSn {
get { return baseObj.ManufacturerSn; }
set { if (baseObj.ManufacturerSn == value) return; baseObj.ManufacturerSn = value;OnPropertyChanged(nameof(ManufacturerSn)); }
}

[DisplayName("Decommissioned")]
public new bool Decommissioned {
get { return baseObj.Decommissioned; }
set { if (baseObj.Decommissioned == value) return; baseObj.Decommissioned = value;OnPropertyChanged(nameof(Decommissioned)); }
}

[DisplayName("Owner")]
public new string Owner {
get { return baseObj.Owner; }
set { if (baseObj.Owner == value) return; baseObj.Owner = value; OnPropertyChanged(nameof(Owner)); }
}

[DisplayName("Remarks")]
public new string Remarks {
get { return baseObj.Remarks; }
set { if (baseObj.Remarks == value) return; baseObj.Remarks = value;OnPropertyChanged(nameof(Remarks)); }
}

[DisplayName("Provisioned")]
public new bool? Provisioned {
get { return baseObj.Provisioned; }
set { if (baseObj.Provisioned == value) return; baseObj.Provisioned = value;OnPropertyChanged(nameof(Provisioned)); }
}

public event PropertyChangedEventHandler? PropertyChanged;
void OnPropertyChanged(string? propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}

}
In meinem Formular SpoolsadMinform verbinde ich nur die BindingsSource mit dem Raster.

Code: Select all

public SpoolAdminForm(IVmSpools vm)
{

vmSpools = vm;
InitializeComponent();
MotherSpoolGrid.DataSource = vm.MotherSpools;
// MotherSpoolGrid.Refresh();
ProductSpoolGrid.DataSource=vm.ProductionSpools;
}
Für die Vollständigkeit: Ich verwende eine ererbte DataGridView , um die Spaltenbestellung zu speichern. Ich glaube nicht, dass es mit dem Problem zu tun hat < /p>

Code: Select all

[Description("System.Windows.Forms.DataGridView with save settings")]
[ToolboxBitmap(typeof(System.Windows.Forms.DataGridView))]
public class DataGridViewPersistent : DataGridView
{
protected override void Dispose(bool disposing)
{
SaveDisplaySettings();
base.Dispose(disposing);
}

public delegate void SaveSettingsEventHandler(object sender, string setJson);
public event SaveSettingsEventHandler? SaveSettings;
public void SaveDisplaySettings()
{
List columnOrder = new List();
DataGridViewColumnCollection columns = this.Columns;
for (int i = 0; i < columns.Count; i++)
{
columnOrder.Add(new ColumnOrderItem
{
ColumnIndex = i,
DisplayIndex = columns[i].DisplayIndex,
Visible = columns[i].Visible,
Width = columns[i].Width
});
}
var json = JsonSerializer.Serialize(columnOrder);
SaveSettings?.Invoke(this, json);

//Properties.Settings.Default.dgvJobs = json;
//Properties.Settings.Default.Save();
}

public void LoadDisplaySettings(string settingsJson)
{
try
{
if (settingsJson == null) return;
var json= JsonSerializer.Deserialize(settingsJson);
if (json == null) return;
List columnOrder = json;

if (columnOrder != null)
{
var ordered=columnOrder.OrderBy(i=>i.DisplayIndex);
foreach (var item in ordered)
{
this.Columns[item.ColumnIndex].DisplayIndex = item.DisplayIndex;
this.Columns[item.ColumnIndex].Visible = item.Visible;
this.Columns[item.ColumnIndex].Width = item.Width;
}
}
}
catch { }
}
}

[Serializable]
public sealed class ColumnOrderItem
{
public int DisplayIndex { get; set; }
public int Width { get; set; }
public bool Visible { get; set; }
public int ColumnIndex { get; set; }
}

internal sealed class DataGridViewSetting : ApplicationSettingsBase
{
private static DataGridViewSetting _defaultInstace =
(DataGridViewSetting)ApplicationSettingsBase
.Synchronized(new DataGridViewSetting());
//---------------------------------------------------------------------
public static DataGridViewSetting Default
{
get { return _defaultInstace; }
}
//---------------------------------------------------------------------
// Because there can be more than one DGV in the user-application
// a dictionary is used to save the settings for this DGV.
// As key the name of the control is used.
[UserScopedSetting]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
[DefaultSettingValue("")]
public Dictionary ColumnOrder
{
get
{
return this["ColumnOrder"] as Dictionary;
}
set { this["ColumnOrder"] = value; }
}
}
Beim Debuggen der BindingList wird korrekt ausgefüllt (1 Eintrag zum Testen). Die BindingSource scheint korrekt eingestellt zu sein (z. B. count = 1). Wenn ich die BindingSource verbinde, ist es nicht.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post