Tato značka není monitorována společností Microsoft.
WINFORMS - Načítání záznamů
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomerEvidenceApp
{
public partial class CustomerListForm : Form
{
public CustomerListForm()
{
InitializeComponent(); // Inicializace komponent formuláře
}
private void CustomerListForm_Load(object sender, EventArgs e)
{
LoadCustomers(); // Načtení zákazníků při načtení formuláře
}
private void button_Add_Click(object sender, EventArgs e)
{
// Otevření formuláře pro přidání nového zákazníka
CustomerEditForm form = new CustomerEditForm();
DialogResult result = form.ShowDialog(); // Zobrazení formuláře jako dialog
if (result == DialogResult.OK)
{
LoadCustomers();
// Po úspěšném přidání načteme znovu seznam zákazníků
}
}
private void LoadCustomers()
{
// Vytvoření instance databázového kontextu
ApplicationDbContext dbContext = new ApplicationDbContext();
// Načtení seznamu zákazníků z databáze a přiřazení jako datový zdroj pro DataGridView this.dataGridView_Customers.DataSource = dbContext.Customers.ToList();
}
}
}
//Entita
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomerEvidenceApp.Entities
{
[Table("customers")]
public class Customer
{
[Key]
public int Id { get; set; }
public string FullName { get; set; }
public DateTime Birthday { get; set; }
public int IQ { get; set; }
public string Gender { get; set; }
public bool GdprAgreement { get; set; }
}
```}
////AppDbContext
using CustomerEvidenceApp.Entities;
using Microsoft.EntityFrameworkCore;
namespace CustomerEvidenceApp
{
```powershell
public class ApplicationDbContext: DbContext
{
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//---- Klasický přístup do MySQL databáze -----
//optionsBuilder.UseMySQL("server=;database=;user=;password=");
//---- Alternativní přístup do SQLite databáze
optionsBuilder.UseSqlite("DataSource=customer.db");
}
}
```}
//ErrorProviderExtensions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CustomerEvidenceApp
{
```go
public static class ErrorProviderExtensions
{
public static bool ReallyHasErrors(this ErrorProvider provider)
{
// ErrorProvider.HasErrors doesn't actually work; see https://github.com/dotnet/winforms/issues/10424
// If HasErrors = false then there definately are 0 errors.
// If HasErrors = true then there MAY be errors.
if (!provider.HasErrors) { return false; }
// There is no public property to get the list of Errors
var privateItems = (System.Collections.IDictionary)(typeof(ErrorProvider).GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(provider)!);
// privateItems is Dictionary<Control, ControlItem> but ControlItem is an internal class.
var controls = privateItems.Keys;
foreach (Control item in controls)
{
if(!string.IsNullOrEmpty(provider.GetError(item))) { return true; }
}
return false;
}
}
```}
////Program
namespace CustomerEvidenceApp
{
```typescript
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
InitializeDatabase();
ApplicationConfiguration.Initialize();
Application.Run(new CustomerListForm());
}
static void InitializeDatabase()
{
ApplicationDbContext dbContext = new ApplicationDbContext();
dbContext.Database.EnsureCreated();
}
}
```}