A set of .NET Framework managed libraries for developing graphical user interfaces.
Use EF Core, for example in the following I take the liberty to change column names. Not sure why CurrentQuantity is a decimal and not an int but left it as is.
CREATE TABLE [dbo].[Products](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[ProductName] [NVARCHAR](MAX) NULL,
[CurrentQuantity] [DECIMAL](10, 2) NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
EF Core code generated by EF Power Tools.
Find full source here
EF Core model
public partial class Products : INotifyPropertyChanged
{
private string _productName;
private decimal? _currentQuantity;
public int Id { get; set; }
public string ProductName
{
get => _productName;
set
{
if (value == _productName) return;
_productName = value;
OnPropertyChanged();
}
}
public decimal? CurrentQuantity
{
get => _currentQuantity;
set
{
if (value == _currentQuantity) return;
_currentQuantity = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
DContext
public partial class Context : DbContext
{
public Context()
{
}
public Context(DbContextOptions<Context> options)
: base(options)
{
}
public virtual DbSet<Products> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(
"""
Data Source=(localdb)\MSSQLLocalDB;
Initial Catalog=Demo;
Integrated Security=True
""");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Products>(entity =>
{
entity.Property(e => e.CurrentQuantity).HasColumnType("decimal(10, 2)");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Form code
public partial class Form1 : Form
{
private Context _context = new();
private BindingSource _bindingSource = new();
private SortableBindingList<Products> _bindingList;
public Form1()
{
InitializeComponent();
_bindingList = new SortableBindingList<Products>(
_context.Products.OrderBy(p => p.ProductName).ToList());
_bindingSource.DataSource = _bindingList;
dataGridView1.DataSource = _bindingSource;
dataGridView1.Columns["Id"]!.ReadOnly = true;
dataGridView1.ExpandColumns();
dataGridView1.Spread();
}
private void SaveButton_Click(object sender, EventArgs e)
{
var affected = _context.SaveChanges();
MessageBox.Show(affected == 0 ? "Nothing saved" : "Saved");
}
}
Another option if this is only for simple updates is to look at a TableAdapter which is easy for what is being asked but getting into more advance coding without understanding TableAdapters can become frustrating. Best to use EF Core.