简体   繁体   中英

how can i get the selected items of a checked listbox to a particular column in datagridview in c# windows application?

    string items;
    string total;

    private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
    {

        foreach (string index in checkedListBox1.CheckedItems)
        {
            for (int cnt = 0; cnt < checkedListBox1.Items.Count; cnt++)
                // total_items.(getdata.Items[index].ToString());
                total = index.ToString();     
        }
        dataGridView1.Columns.Add("Items", "Choosed Items");
        dataGridView1.Rows.Add(total);
        dataGridView1.ClearSelection();
    }

I think you haven`t any other variant for add rows to datagridView.

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{  
   if(listBox1.SelectedIndex != -1)  {         
       // Get the currently selected item in the ListBox.
       total = listBox1.SelectedItem.ToString();
       dataGridView1.Rows.Add(new object[] {total});
       listbox1.ClearSelected();
   }
}

Other variant maybe you can use bindings and filter values if it`s checked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM