如何将多个列表框 selectedItems 放入网格

How do I get multiple listbox selectedItems to a Grid

您好,非常感谢您的帮助,我刚刚开始编码并尝试向网格发送多个项目,但使用 clistBox1.SelectedIndices.Count 不起作用,因为它只获得相同的信息 n 次(计数结果)。我正在寻找 dataGridView1.Rows.Add 末尾的内容。请帮忙!

    private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.Rows.Clear();


        for (int iCount = 0; iCount < listBox1.SelectedIndices.Count; iCount++)
        {

               string frase = listBox1.SelectedItem.ToString();

               string col1aux = frase.Substring(0, 2);
               string col2aux = frase.Substring(2, 11);
               string col3aux = frase.Substring(13, 10);
               string col4aux = frase.Substring(23, 50);
               string col5aux = frase.Substring(73, 22);
               string col6aux = frase.Substring(95, 3);
               string col7aux = frase.Substring(98, 8);
               string col8aux = frase.Substring(106, 8);
               string col9aux = frase.Substring(114, 1);

               int index = dataGridView1.Rows.Add();
               dataGridView1.Rows[index].Cells["Column1"].Value = col1aux.ToString();
               dataGridView1.Rows[index].Cells["Column2"].Value = col2aux.ToString();
               dataGridView1.Rows[index].Cells["Column3"].Value = col3aux.ToString();
               dataGridView1.Rows[index].Cells["Column4"].Value = col4aux.ToString();
               dataGridView1.Rows[index].Cells["Column5"].Value = col5aux.ToString();
               dataGridView1.Rows[index].Cells["Column6"].Value = col6aux.ToString();
               dataGridView1.Rows[index].Cells["Column7"].Value = col7aux.ToString();
               dataGridView1.Rows[index].Cells["Column8"].Value = col8aux.ToString();
               dataGridView1.Rows[index].Cells["Column9"].Value = col9aux.ToString();


        }

    }

考虑使用 CheckedListBox 并将 DataSource 属性 设置为列表,例如。

public class Item
{
    public string Name { get; set; }
    public string Country { get; set; }
    public override string ToString() => $"{Name} {Country}";
}

设置模拟数据

public class Mocked
{
    public static List<Item> List => new List<Item>()
    {
        new Item() { Country = "Canada", Name = "Jane" },
        new Item() { Country = "France", Name = "Luke" },
        new Item() { Country = "Japan", Name = "Anne" },
        new Item() { Country = "Africa", Name = "Mike" }
    };
}

有一个扩展方法来从 CheckedListBox 中获取选中的项目

public static class CheckedListBoxExtensions
{
    public static List<T> CheckedList<T>(this CheckedListBox source)
        => source.Items.Cast<T>()
            .Where((item, index) => source.GetItemChecked(index))
            .Select(item => item)
            .ToList();
}

在您的表单中,使用 BindingSource 设置为 a,在本例中为 Item 列表。在按钮单击事件中,从 CheckedListBox 中获取选定的项目并将它们添加到 DataGridView(如果尚未在 DataGridView 中)。

public partial class Form1 : Form
{
    private readonly BindingSource _bindingSource = 
        new BindingSource();

    public Form1()
    {
        InitializeComponent();

        checkedListBox1.DataSource = Mocked.List;
        _bindingSource.DataSource = new List<Item>();
        dataGridView1.DataSource = _bindingSource;
    }

    private void GetSelectedButton_Click(object sender, EventArgs e)
    {
        List<Item> selected = checkedListBox1.CheckedList<Item>();

        if (selected.Any())
        {
            var data = (List<Item>)_bindingSource.DataSource;
            foreach (var item in selected)
            {
                if (data.FirstOrDefault(x => x.Name == item.Name && x.Country == item.Country) == null)
                {
                    _bindingSource.Add(item);
                }
            }
        }

    }
}

备注 DataGridView 中的每一列都将其 DataPropertyName 设置为 Item class.

中的 属性

而不是 SelectedIndices 使用 SelectedItems:

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.Rows.Clear();

    foreach (var item in listBox1.SelectedItems)
    {
           var frase = item.ToString();
           int index = dataGridView1.Rows.Add();
           var cells = dataGridView1.Rows[index].Cells;

           cells["Column1"].Value = frase.Substring(0, 2);
           cells["Column2"].Value = frase.Substring(2, 11);
           ...               
    }
}