嗨,非常感謝您的幫助,我剛剛開始編碼并嘗試將多個專案發送到網格,但使用 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();
}
}
uj5u.com熱心網友回復:
而不是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);
...
}
}
uj5u.com熱心網友回復:
考慮使用將CheckedListBoxDataSource 屬性設定為 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();
}
在您的表單中,使用設定為 a 的 BindingSource,在本例中為 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。

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/465781.html
