代码如上:要实现功能是选中dgv1一行数据,选中dgv2一行数据,点击按钮,把dgv2行数据填入dgv1行,同时dgv1自动跳到上一行
代码如下:要实现功能是选中dgv1一行数据,选中dgv2一行数据,点击按钮,把dgv2行数据填入dgv1行,同时dgv1自动跳到下一行。
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.MultiSelect == true)
{
dataGridView1.CurrentRow.Cells["状态"].Value = "已配准";
dataGridView1.CurrentRow.Cells["名称"].Value = dataGridView2.CurrentRow.Cells["名称"].Value.ToString();
}
{
int i = dataGridView1.CurrentRow.Index;
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[i + 1].Selected = true;
if (this.dataGridView1.Rows[i+1].Selected == true)
{
this.dataGridView1.MultiSelect = true;
textBox1.Text = this.dataGridView1.CurrentRow.Cells["名称"].Value.ToString();
//textBox2.Text = this.dataGridView1.CurrentRow.Cells["地址"].Value.ToString();
textBox3.Text = this.dataGridView1.CurrentRow.Cells["区划代码"].Value.ToString();
}
}
}
代码是可以实习,填入后,跳到下一行,但是下一行只是以不同颜色突出显示出来,数据无法实现填入textbox中,我想要让它跳到下一行的同时,突出显示,并且把值填入textbox中。
------解决方案--------------------
Selected后有row并不等于当前row
要设置row的cell为当前
该cell所在行才是CurrentRow
------解决方案--------------------
this.dataGridView1.Rows[i + 1].Selected = true
this.dataGridView1.CurrentCell=this.dataGridView1.Rows[i + 1].Cell[0]
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.MultiSelect == true)
{
dataGridView1.CurrentRow.Cells["状态"].Value = "已配准";
dataGridView1.CurrentRow.Cells["名称"].Value = dataGridView2.CurrentRow.Cells["名称"].Value.ToString();
}
{
int i = dataGridView1.CurrentRow.Index;
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[i + 1].Selected = true;
if (this.dataGridView1.Rows[i+1].Selected == true)
{
this.dataGridView1.MultiSelect = true;
textBox1.Text = this.dataGridView1.CurrentRow.Cells["名称"].Value.ToString();
//textBox2.Text = this.dataGridView1.CurrentRow.Cells["地址"].Value.ToString();
textBox3.Text = this.dataGridView1.CurrentRow.Cells["区划代码"].Value.ToString();
}
}
}
代码是可以实习,填入后,跳到下一行,但是下一行只是以不同颜色突出显示出来,数据无法实现填入textbox中,我想要让它跳到下一行的同时,突出显示,并且把值填入textbox中。
------解决方案--------------------
Selected后有row并不等于当前row
要设置row的cell为当前
该cell所在行才是CurrentRow
------解决方案--------------------
this.dataGridView1.Rows[i + 1].Selected = true
this.dataGridView1.CurrentCell=this.dataGridView1.Rows[i + 1].Cell[0]