Home > database >  How do I get multiple listbox selectedItems to a Grid
How do I get multiple listbox selectedItems to a Grid

Time:04-27

Hi Thanks so much for your help, I am just starting coding and trying to send more than one item to the Grid but using the clistBox1.SelectedIndices.Count is not working as it is only getting the same information n times (count result). I am looking for maybe something at the end of the dataGridView1.Rows.Add. Please help!

    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();


        }

    }

CodePudding user response:

Consider using a CheckedListBox with DataSource property set to a List, example.

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

Setup mocked data

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" }
    };
}

Have an extension method to get checked items from the 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();
}

In your form, use a BindingSource set to a, in this case a list of Item. In a button click event, get selected items from the CheckedListBox and add them to the DataGridView if not already in the 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);
                }
            }
        }

    }
}

Note Each column in the DataGridView has it's DataPropertyName set to a property in Item class.

enter image description here

CodePudding user response:

instead of SelectedIndices use SelectedItems:

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

    foreach (string frase in listBox1.SelectedItems)
    {
           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);
           ...               
    }
}
  • Related