How to pass ListView row data into another Form in C#
By FoxLearn 12/1/2024 2:29:02 PM 8.05K
In this article, we will walk through an example of how to link a ListView
control to a database in a Windows Forms application using Entity Framework.
The example will also demonstrate how to open a detailed view of a selected product when a user clicks on an item in the ListView
. We will create two forms: one for displaying the list of products and another for displaying product details.
How to pass ListView row data into another Form in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "LinkListViewToDatabase" and then click OK
Design your form as shown below.
Form1
frmDetail
Create an Entity Framework Model, then add a Product table from the Northwind database to your model.
This form will display the details of a single product. We will bind a Product
object to the form's controls using data binding.
frmDetail
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 LinkListViewToDatabase { public partial class frmDetail : Form { // Constructor receives a Product object public frmDetail(Product p) { InitializeComponent(); // Bind the product to the form's controls using data binding productBindingSource.DataSource = p; } } }
The productBindingSource
is bound to the Product
object, which allows the form to display the product details.
In Form1
, we will populate a ListView
with product data from the database. The user can select a product, and when activated, the selected product’s details will be shown in the frmDetail
form.
The btnLoad_Click
event handler loads data from the Northwind
database into the ListView
. It queries the Products
table using Entity Framework and adds each product as an item in the ListView
.
// Button click to load product data from the database private void btnLoad_Click(object sender, EventArgs e) { // Change cursor to wait while loading data Cursor.Current = Cursors.WaitCursor; // Create an instance of the database context using (NorthwindEntities db = new NorthwindEntities()) { // Get the list of products from the database List<Product> list = db.Products.ToList(); // Populate the ListView with product data foreach (Product p in list) { ListViewItem item = new ListViewItem(p.ProductID.ToString()); item.SubItems.Add(p.ProductName); item.SubItems.Add(p.UnitPrice.ToString()); item.SubItems.Add(p.UnitsInStock.ToString()); listView.Items.Add(item); } } Cursor.Current = Cursors.Default; }
The listView_ItemActivate
event is triggered when a user clicks on an item in the ListView
. This event fetches the product's details from the database and opens the frmDetail
form with the selected product.
// Event handler when a ListView item is selected private async void listView_ItemActivate(object sender, EventArgs e) { if (listView.SelectedItems.Count > 0) { // Get the selected ListViewItem ListViewItem item = listView.SelectedItems[0]; // Fetch the product from the database using the product ID using (NorthwindEntities db = new NorthwindEntities()) { string productId = item.SubItems[0].Text; Product p = await db.Products.FindAsync(Convert.ToInt32(productId)); // If the product exists, show the detail form if (p != null) { using (frmDetail frm = new frmDetail(p)) { // Show the detail form and check if the user clicked OK if (frm.ShowDialog() == DialogResult.OK) { // Process changes if any (e.g., saving data) } } } } } }
When the user clicks the Load button (btnLoad
), it fetches the product data from the database and populates the ListView
. When a user clicks on a product in the ListView
, the frmDetail
form is opened, showing detailed information about that product. The data from the ListView
is passed to the frmDetail
form, where it is bound to controls such as labels or text boxes.
This simple example demonstrates how to link a ListView
to a database in a Windows Forms application using Entity Framework. It provides a way to display a list of products, and upon selection, show the details of the selected product in a new form.
VIDEO TUTORIAL
- How to Print Text in a Windows Form Application Using C#
- How to fill ComboBox and DataGridView automatically in C#
- How to Read text file and Sort list in C#
- How to read and write to text file in C#
- How to make a Countdown Timer in C#
- How to Display selected Row from DataGridView to TextBox in C#
- How to Get all Forms and Open Form with Form Name in C#
- How to Get Checked Items In a CheckedListBox in C#