簡體   English   中英

如何將多個列表框 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 屬性設置為 List,例如。

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);
           ...               
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM