我想看到我在其他单元格中看到的东西,只有在当前行中,我尝试这样做,但没有结果:
dataGridView1.Rows[0].Cells[0].Value =
dataGridView1.Rows[0].Cells[1].Value;\\ cell 1 is my comboboxcell
发布于 2016-10-06 22:25:03
我有一个名为Item的类来添加列表中的项,
项目类;
public class Item
{
public string Name { get; set; }
public int Id { get; set; }
}
在Form_Load中,我加载了datagridview
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Columns.Add("test1", "test1");
DataGridViewComboBoxColumn testCol = new DataGridViewComboBoxColumn();
testCol.HeaderText = "comboValues";
dataGridView1.Columns.Add(testCol);
dataGridView1.Columns.Add("test2", "test1");
List<Item> items = new List<Item>();
items.Add(new Item() { Name = "One", Id = 1 });
items.Add(new Item() { Name = "Two", Id = 2 }); // created two Items
var cbo = dataGridView1.Columns[1] as DataGridViewComboBoxColumn; // index of 1 is the comboboxColumn
cbo.DataSource = items; // setting datasource
cbo.ValueMember = "Id";
cbo.DisplayMember = "Name";
dataGridView1.Rows.Add("", items[1].Id, "test1");
dataGridView1.Rows.Add("", items[0].Id, "test2");
dataGridView1.Rows.Add("", items[1].Id, "test3"); // and test rows
}
在新的选择之前,我们需要检查哪一行是前一行,所以我们需要使用Row_Leave事件。
int previousRowIndex = 0; // a variable to keep the index of row
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
previousRowIndex = e.RowIndex;
}
主要的活动是SelectionChanged,
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
dataGridView1.Rows[previousRowIndex].Cells[0].Value = ""; // first set the previous row's first cell value empty string.
DataGridViewComboBoxCell comboCell = dataGridView1.CurrentRow.Cells[1] as DataGridViewComboBoxCell;
dataGridView1.CurrentRow.Cells[0].Value = comboCell.EditedFormattedValue; // then set the first cell's value as the combobox's selected value.
}
结果;
希望能帮上忙
https://stackoverflow.com/questions/39905848
复制相似问题