如何从其他窗体向ListView保存值?解决ListView不新增数据问题
Hey there! Let's tackle this ListView issue you're having—sounds like you've got the value in hand (since debugging confirms it exists) but it's just not showing up in the list, which is super frustrating. Let's break down what might be going on and how to fix it.
First, let's look at the code snippet you shared
From what I can see, you're creating a new qtyOfOrders form instance when the DataGridView is double-clicked, but you're not fully passing the retrieved values to update the ListView. Here are the most common fixes based on your scenario:
Scenario 1: You want to add items to the ListView in the qtyOfOrders form
If your target ListView lives inside the qtyOfOrders window, you need to explicitly pass the values to that form and trigger the item addition. Here's how to adjust your code:
Add a method in
qtyOfOrdersto accept and add itemspublic class qtyOfOrders : Form { // Assume your ListView is named listViewOrders public void AddOrderItem(string mealName, string price) { // Create a new ListViewItem with your values ListViewItem newItem = new ListViewItem(mealName); newItem.SubItems.Add(price); // Add it to the ListView listViewOrders.Items.Add(newItem); // Critical: Ensure the ListView is set to Details view (if using columns) if (listViewOrders.Columns.Count == 0) { listViewOrders.Columns.Add("Meal Name", 150); listViewOrders.Columns.Add("Price", 100); listViewOrders.View = View.Details; } } }Update your Form1 double-click event to pass values
public void dataGridView1_DoubleClick(object sender, EventArgs e) { qtyOfOrders ordersForm = new qtyOfOrders(); if (dataGridView1.SelectedRows.Count > 0) { // Safely retrieve values (avoid null reference errors) string mealName = dataGridView1.SelectedRows[0].Cells[1].Value?.ToString() ?? string.Empty; string price = dataGridView1.SelectedRows[0].Cells[2].Value?.ToString() ?? string.Empty; // Adjust cell index to match your data // Pass values to the qtyOfOrders form and add the item ordersForm.AddOrderItem(mealName, price); } // Don't forget to show the form! ordersForm.Show(); }
Scenario 2: You want to add items to a ListView in Form1 itself
If your target ListView is in the same Form1 as the DataGridView, skip creating a new qtyOfOrders instance and directly update your local ListView:
public void dataGridView1_DoubleClick(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { string mealName = dataGridView1.SelectedRows[0].Cells[1].Value?.ToString() ?? string.Empty; string price = dataGridView1.SelectedRows[0].Cells[2].Value?.ToString() ?? string.Empty; // Create and add the item to your Form1 ListView ListViewItem newItem = new ListViewItem(mealName); newItem.SubItems.Add(price); this.listView1.Items.Add(newItem); // Replace listView1 with your actual ListView name // Ensure proper view setup if needed if (listView1.Columns.Count == 0) { listView1.Columns.Add("Meal Name", 150); listView1.Columns.Add("Price", 100); listView1.View = View.Details; } } }
Quick Debug Checks to Rule Out Other Issues
- Verify the ListView's View property: If you're using
Detailsview (to show multiple columns), you must add columns first—otherwise items will exist but won't be visible. - Check for accidental clears: Make sure no other code is calling
listView.Items.Clear()after you add the item. - UI Thread Safety: If you're working with async code anywhere, use
Invoketo update the ListView (though this is less likely in a simple double-click event):private void AddItemSafely(string mealName, string price) { if (listView1.InvokeRequired) { listView1.Invoke(new Action<string, string>(AddItemSafely), mealName, price); return; } // Add item code here }
内容的提问来源于stack exchange,提问作者Justine

