Подскажите пожалуйста, как в vs2010 c# winforms выполнить следующую задачу:
На Form1 есть CheckListBox1 и кнопка button_add, с помощью которой происходит добавление строк в CheckListBox1 из DataGridView1 (состоящего из 3-х столбцов), расположенного на форме Form2. Кодом ниже получается добавить строку в CheckListBox1 только из выбранной строки 1-го столбца DataGridView1 (т.е. "getItem()"). Как сделать добавление значений из выбранной строки из 3-х столбцов DataGridView1 в три столбца CheckListBox1 соответственно?
private void Form1_Load(object sender, EventArgs e)
{
CheckListBox1.Sorted = true;
CheckListBox1.TopIndex = 0;
CheckListBox1.CheckOnClick = true;
CheckListBox1.SelectionMode = SelectionMode.One;
CheckListBox1.MultiColumn = true;
}
private void button_add_Click(object sender, EventArgs e)
{
var form2 = new Form2();
if (form2.ShowDialog(this) == DialogResult.OK)
{
CheckListBox1.SetSelected(CheckListBox1.Items.Add(form2.getItem()), true);
}
form2.Close();
form2.Dispose();
}
public Form2()
{
InitializeComponent();
button1.DialogResult = DialogResult.OK;
}
//0столбец:
public string getItem()
{
String Tab = (String)this.dataGridView1[0, this.dataGridView1.CurrentRow.Index].Value;
return Tab;
}
//1столбец:
public string getItem1()
{
String Tab1 = (String)this.dataGridView1[1, this.dataGridView1.CurrentRow.Index].Value;
return Tab1;
}
//2столбец:
public string getItem2()
{
String Tab2 = (String)this.dataGridView1[2, this.dataGridView1.CurrentRow.Index].Value;
return Tab2;
}
public List<string> GetItems()
{
List<string> lOut = new List<string>();
foreach(DataGridViewColumn item in dataGridView1.Columns)
{lOut.Add((String)this.dataGridView1[dataGridView1.Columns[item.Name].Index,dataGridView1.CurrentRow.Index].Value;
}
return lOut;
}
private void button_add_Click(object sender, EventArgs e)
{
var form2 = new Form2();
if (form2.ShowDialog(this) == DialogResult.OK)
{
var lst =GetItems();
foreach (var item in lst)
{
CheckListBox1.SetSelected(CheckListBox1.Items.Add(item, true);
}
}
form2.Close();
form2.Dispose();
}
Сборка персонального компьютера от Artline: умный выбор для современных пользователей