Sn0wb0y
asked on
How to update items in a datagridview with BindingList?
In my windows forms application, I have a list of items <Container> which are populated into a datagridview. Each container has a unique ID. As follows:
In the datagridview, I have a "Modify" button column that when clicked, transfers the properties of the selected container onto the textbox controls in my UI. As follows:
Question: Given the above, how do I update the properties of a container within my list?
Background code:
Container class:
List initialization:
In the datagridview, I have a "Modify" button column that when clicked, transfers the properties of the selected container onto the textbox controls in my UI. As follows:
Question: Given the above, how do I update the properties of a container within my list?
Background code:
Container class:
public class Container
{
#region Properties
// Unique container ID
public string ContainerID { get; set; }
// Container description
public string Description { get; set; }
// Container payload capacity - weight
public double CapacityWeight { get; set; }
// Container payload capacity - cube
public double CapacityCube { get; set; }
#endregion
#region Constructor
// Default constructor
public Container()
{
ContainerID = Extension.NewID();
}
#endregion
}
List initialization:
// Create an initial arbitrary list of containers to choose from
BindingList<Container> Containers = new BindingList<Container>();
private void initContainers()
{
// Add some containers to the container list
Containers.Add(new Container() { Description = "20FT GENERAL PURPOSE", CapacityWeight = 23.4, CapacityCube = 30 });
Containers.Add(new Container() { Description = "40FT GENERAL PURPOSE", CapacityWeight = 24.5, CapacityCube = 60 });
Containers.Add(new Container() { Description = "40FT GENERAL PURPOSE - HIGH CUBE", CapacityWeight = 25.0, CapacityCube = 70 });
Containers.Add(new Container() { Description = "US CHEP TWO-WAY PALLET", CapacityWeight = 1.524, CapacityCube = 1.8 });
}
ASKER
Wow! That's completely awesome but way over the top of my head it_saige! I am just starting out with learning C# winforms here, sorry I would have to study it for days to work out what you did! Lolz
I understand that it can be a little overwhelming, at first, but you will find that this is really simple at it's core. You are more than welcome to take what I implemented and create a new solution so that you can step through what it is doing.
-saige-
-saige-
ASKER
Hey thanks for the encouragement it_saige. I can see the logic in what you have done and really do appreciate the effort you put into it. It makes sense in that my UI will become much cleaner, (I know it looks horrible as it is with all those button columns!) and windows user friendly.
However, as I am self learning, I was really looking for an easy way to update the property values on my existing binding list objects as I am wanting to learn these fundamental components first before diving into more complex things.
I will certainly try and use what you have done. Thanks again
However, as I am self learning, I was really looking for an easy way to update the property values on my existing binding list objects as I am wanting to learn these fundamental components first before diving into more complex things.
I will certainly try and use what you have done. Thanks again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi it_saige,
First off, I want to thank you again for taking the time to respond to my question. As I mentioned earlier, I am just starting out and really got myself in a tangle with your solution. In hindsight, I should of provided my existing form code, as clunky as it is, to help you further understand what I was trying to do.
From reviewing that code you provided, I managed to identify and implement the updating of my bound list as intended from my original question. I am absolutely sure it is far from perfect and would appreciate any feedback you might have. As follows:
Main.cs
ExtensionMethods.cs
Container.cs
First off, I want to thank you again for taking the time to respond to my question. As I mentioned earlier, I am just starting out and really got myself in a tangle with your solution. In hindsight, I should of provided my existing form code, as clunky as it is, to help you further understand what I was trying to do.
From reviewing that code you provided, I managed to identify and implement the updating of my bound list as intended from my original question. I am absolutely sure it is far from perfect and would appreciate any feedback you might have. As follows:
Main.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;
using System.Linq;
using System.Drawing;
namespace ContainerPackerWinForms
{
public partial class Main : Form
{
// Create an initial arbitrary list of containers to choose from
BindingList<Container> Containers = new BindingList<Container>();
Container current = default(Container);
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
initContainers();
dgvContainer.DataSource = Containers;
initContainerDGV();
initContainerSection();
}
#region Container
// Add / edit / remove container
private void DgvContainer_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// Validate row selection
if (this.dgvContainer.CurrentRow != null)
{
// Validate modify container button click
if (e.ColumnIndex == this.dgvContainer.Columns["modify_container_button_column"].Index)
{
// Perform a second validation of container selection
if (this.dgvContainer.CurrentRow != null && this.dgvContainer.SelectedRows.Count > 0)
{
// Transfer selected cell bindings to UI controls
this.txtContainerDescription.Text = this.dgvContainer.SelectedCells[1].Value.ToString();
this.txtContainerCapacityWeight.Text = this.dgvContainer.SelectedCells[2].Value.ToString();
this.txtContainerCapacityCube.Text = this.dgvContainer.SelectedCells[3].Value.ToString();
// Change button caption and set focus to description field
this.btnAddUpdateContainer.Text = "Save Changes";
this.txtContainerDescription.Focus();
}
}
// Validate remove container button click
else if (e.ColumnIndex == this.dgvContainer.Columns["remove_container_button_column"].Index)
{
// Perform a second validation of container selection
if (this.dgvContainer.CurrentRow != null && this.dgvContainer.SelectedRows.Count > 0)
{
// TO DO:
// ADD FUNCTIONALITY TO REMOVE ALL EXISTING SHIPMENT CONTAINERS AND PACKING DETAILS
// Remove the selected container from the container list
Containers.RemoveAt(this.dgvContainer.CurrentRow.Index);
// Initialize the container section
initContainerSection();
this.txtShipmentName.Focus();
}
}
}
else
{
// Prompt user to select a container
MessageBox.Show("Error! Please select a container from the list.");
}
}
// Add / update container
private void BtnAddUpdateContainer_Click(object sender, EventArgs e)
{
current = dgvContainer?.SelectedRows?.Cast<DataGridViewRow>()?.First()?.DataBoundItem as Container;
// Validate add container mode
if (this.btnAddUpdateContainer.Text == "Add New Container")
{
// Validate form fields
if (!IsEmpty(this.txtContainerDescription.Text, this.txtContainerCapacityWeight.Text, this.txtContainerCapacityCube.Text))
{
// Add container to list
Containers.Add(new Container()
{
Description = this.txtContainerDescription.Text,
CapacityWeight = Convert.ToDouble(this.txtContainerCapacityWeight.Text),
CapacityCube = Convert.ToDouble(this.txtContainerCapacityCube.Text)
});
// Clear and initialize the container section
this.dgvContainer.ClearSelection();
initContainerSection();
this.txtShipmentName.Focus();
}
else
{
// Prompt user to provide required values
MessageBox.Show("Error! Please provide values for all data entry fields.");
initContainerSection();
this.txtContainerDescription.Focus();
}
}
// Validate update container mode
else if (this.btnAddUpdateContainer.Text == "Save Changes")
{
if (this.dgvContainer.CurrentRow != null && this.dgvContainer.SelectedRows.Count > 0)
{
if (current != default(Container))
{
current.Description = txtContainerDescription.Text;
current.CapacityWeight = Convert.ToDouble(txtContainerCapacityWeight.Text);
current.CapacityCube = Convert.ToDouble(txtContainerCapacityCube.Text);
}
// Clear and initialize the container section
this.dgvContainer.ClearSelection();
initContainerSection();
}
}
}
// Initializes a list of containers
private void initContainers()
{
// Add some containers to the container list
Containers.Add(new Container() { Description = "20FT GENERAL PURPOSE", CapacityWeight = 23.4, CapacityCube = 30 });
Containers.Add(new Container() { Description = "40FT GENERAL PURPOSE", CapacityWeight = 24.5, CapacityCube = 60 });
Containers.Add(new Container() { Description = "40FT GENERAL PURPOSE - HIGH CUBE", CapacityWeight = 25.0, CapacityCube = 70 });
Containers.Add(new Container() { Description = "US CHEP TWO-WAY PALLET", CapacityWeight = 1.524, CapacityCube = 1.8 });
}
// Initializes container section
private void initContainerSection()
{
if (this.dgvContainer.Rows.Count > 0)
{
clearContainerFields();
this.dgvContainer.Enabled = true;
this.txtContainerDescription.Enabled = true;
this.txtContainerCapacityWeight.Enabled = true;
this.txtContainerCapacityCube.Enabled = true;
this.btnAddUpdateContainer.Text = "Add New Container";
this.btnAddUpdateContainer.Enabled = true;
}
else
{
clearContainerFields();
this.dgvContainer.Enabled = false;
this.txtContainerDescription.Enabled = false;
this.txtContainerCapacityWeight.Enabled = false;
this.txtContainerCapacityCube.Enabled = false;
this.btnAddUpdateContainer.Text = "Add New Container";
this.btnAddUpdateContainer.Enabled = true;
}
}
// Clears container entry fields
private void clearContainerFields()
{
this.txtContainerCapacityCube.Text = null;
this.txtContainerCapacityWeight.Text = null;
this.txtContainerDescription.Text = null;
}
// Initializes container datagrid
private void initContainerDGV()
{
// Set control properties
this.dgvContainer.AutoGenerateColumns = false;
this.dgvContainer.MultiSelect = false;
this.dgvContainer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dgvContainer.RowHeadersVisible = false;
this.dgvContainer.RowsDefaultCellStyle.BackColor = Color.White;
this.dgvContainer.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(240, 244, 244);
this.dgvContainer.ScrollBars = ScrollBars.Vertical;
// Set column definitions
this.dgvContainer.Columns[0].HeaderText = "Container ID";
this.dgvContainer.Columns[0].DataPropertyName = "ContainerID";
this.dgvContainer.Columns[0].MinimumWidth = 85;
this.dgvContainer.Columns[0].Width = 90;
this.dgvContainer.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
this.dgvContainer.Columns[1].HeaderText = "Container Description";
this.dgvContainer.Columns[1].DataPropertyName = "Description";
this.dgvContainer.Columns[1].MinimumWidth = 220;
this.dgvContainer.Columns[1].Width = 230;
this.dgvContainer.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
this.dgvContainer.Columns[2].HeaderText = "Weight Capacity (MT)";
this.dgvContainer.Columns[2].DataPropertyName = "CapacityWeight";
this.dgvContainer.Columns[2].MinimumWidth = 130;
this.dgvContainer.Columns[2].Width = 135;
this.dgvContainer.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
this.dgvContainer.Columns[2].DefaultCellStyle.Format = "0.00";
this.dgvContainer.Columns[3].HeaderText = "Cube Capacity (CBM)";
this.dgvContainer.Columns[3].DataPropertyName = "CapacityCube";
this.dgvContainer.Columns[3].MinimumWidth = 130;
this.dgvContainer.Columns[3].Width = 135;
this.dgvContainer.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
this.dgvContainer.Columns[3].DefaultCellStyle.Format = "0.00";
// Edit container button column
DataGridViewButtonColumn modifyContainerButtonColumn = new DataGridViewButtonColumn();
modifyContainerButtonColumn.Name = "modify_container_button_column";
modifyContainerButtonColumn.HeaderText = "Modify";
modifyContainerButtonColumn.Text = "Modify";
modifyContainerButtonColumn.UseColumnTextForButtonValue = true;
int columnIndex1 = 4;
if (this.dgvContainer.Columns["modify_container_button_column"] == null)
{
this.dgvContainer.Columns.Insert(columnIndex1, modifyContainerButtonColumn);
}
// Add container to shipment button column
DataGridViewButtonColumn addContainerToShipmentButtonColumn = new DataGridViewButtonColumn();
addContainerToShipmentButtonColumn.Name = "add_container_to_shipment_button_column";
addContainerToShipmentButtonColumn.HeaderText = "Add";
addContainerToShipmentButtonColumn.Text = "Add";
addContainerToShipmentButtonColumn.UseColumnTextForButtonValue = true;
int columnIndex2 = 5;
if (this.dgvContainer.Columns["add_container_to_shipment_button_column"] == null)
{
this.dgvContainer.Columns.Insert(columnIndex2, addContainerToShipmentButtonColumn);
}
// Remove container button column
DataGridViewButtonColumn removeContainerButtonColumn = new DataGridViewButtonColumn();
removeContainerButtonColumn.Name = "remove_container_button_column";
removeContainerButtonColumn.HeaderText = "Remove";
removeContainerButtonColumn.Text = "Remove";
removeContainerButtonColumn.UseColumnTextForButtonValue = true;
int columnIndex3 = 6;
if (this.dgvContainer.Columns["remove_container_button_column"] == null)
{
this.dgvContainer.Columns.Insert(columnIndex3, removeContainerButtonColumn);
}
}
#endregion
#region Form Helper Methods
// Function to validate textbox entries
private static bool IsEmpty(params string[] args)
{
if (args.Length == 0) return true;
return args.Any(p => string.IsNullOrEmpty(p));
}
#endregion
}
}
ExtensionMethods.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExtensionMethods
{
public static class Extension
{
public static string NewID()
{
return Guid.NewGuid().ToString().Replace("-", string.Empty).Replace("=", string.Empty).Replace("+", string.Empty).Substring(0, 6);
}
}
}
Container.cs
using System;
using ExtensionMethods;
namespace ContainerPackerWinForms
{
public class Container
{
#region Properties
public string ContainerID { get; private set; }
public string Description { get; set; }
public double CapacityWeight { get; set; }
public double CapacityCube { get; set; }
#endregion
#region Constructor
// Default constructor
public Container()
{
ContainerID = Extension.NewID();
}
#endregion
}
}
Form1.cs -
Open in new window
Form1.Designer.cs -Open in new window
DynamicForm.cs -Open in new window
DynamicForm.Designer.cs -Open in new window
SupportingObjects.cs -Open in new window
Produces the following output --saige-