如何将DataGridView里的某行数据显示到相应的TextBox里? 15
希望可以点击DataGridView里的某行,数据就显示到已布置好的各个TextBox里去,当点击DataGridView里的另外一行,此行数据覆盖原来在上一个选中时各个...
希望可以点击DataGridView里的某行,数据就显示到已布置好的各个TextBox里去,当点击DataGridView里的另外一行,此行数据覆盖原来在上一个选中时各个TextBox里去。下面是我的代码,不知道为什么实现不了。。
private void MerchandiseForm_Load(object sender, EventArgs e)
{
SqlConnection thisConnection = new SqlConnection("Data Source=YDKG-8518D67926\\SQLEXPRESS;Initial Catalog=InventoryManagement;Integrated Security=True");
SqlCommand MyCommand = new SqlCommand("select * from 商品信息 ", thisConnection);
SqlDataAdapter thisAdapter = new SqlDataAdapter();
DataSet thisDataSet = new DataSet();
thisAdapter.SelectCommand = MyCommand;
thisConnection.Open();
thisAdapter.Fill(thisDataSet, "商品信息");
thisConnection.Close();
dataGridView1.DataSource = thisDataSet.Tables["商品信息"];
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
try
{
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
}
catch { }
} 展开
private void MerchandiseForm_Load(object sender, EventArgs e)
{
SqlConnection thisConnection = new SqlConnection("Data Source=YDKG-8518D67926\\SQLEXPRESS;Initial Catalog=InventoryManagement;Integrated Security=True");
SqlCommand MyCommand = new SqlCommand("select * from 商品信息 ", thisConnection);
SqlDataAdapter thisAdapter = new SqlDataAdapter();
DataSet thisDataSet = new DataSet();
thisAdapter.SelectCommand = MyCommand;
thisConnection.Open();
thisAdapter.Fill(thisDataSet, "商品信息");
thisConnection.Close();
dataGridView1.DataSource = thisDataSet.Tables["商品信息"];
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
try
{
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
}
catch { }
} 展开
2个回答
展开全部
虽然不知道你编这样的功能干嘛
不过我还是给出我的答案吧
dataGridView1.CurrentRow.Index表示的是你当前选择的行,你把当前选择的行在赋值给当前行当然看不出改变啦
如果要实现你所说功能 应该改成这样:
首先定义一个全局整形变量 preIndex 初值为-1吧(千万不要在0以上的数,要保证preIndex不等于dataGridView1行索引)
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{ if( preIndex==-1)
preIndex=dataGridView1.CurrentRow.Index;//第一次点击行时要保
//存的索引
else
{
try
{
textBox1.Text = dataGridView1.Rows[preIndex].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[preIndex].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[preIndex].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.Rows[preIndex].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.Rows[preIndex].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.Rows[preIndex].Cells[5].Value.ToString();
preIndex=dataGridView1.CurrentRow.Index;//重新记录所点击行的索引
}
catch { }
}
}
测试一下吧 祝你成功
不过我还是给出我的答案吧
dataGridView1.CurrentRow.Index表示的是你当前选择的行,你把当前选择的行在赋值给当前行当然看不出改变啦
如果要实现你所说功能 应该改成这样:
首先定义一个全局整形变量 preIndex 初值为-1吧(千万不要在0以上的数,要保证preIndex不等于dataGridView1行索引)
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{ if( preIndex==-1)
preIndex=dataGridView1.CurrentRow.Index;//第一次点击行时要保
//存的索引
else
{
try
{
textBox1.Text = dataGridView1.Rows[preIndex].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[preIndex].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[preIndex].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.Rows[preIndex].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.Rows[preIndex].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.Rows[preIndex].Cells[5].Value.ToString();
preIndex=dataGridView1.CurrentRow.Index;//重新记录所点击行的索引
}
catch { }
}
}
测试一下吧 祝你成功
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询