我有一个带有列的DGV,我希望用另一个数据库填充信息,我只想读取ID并将与该ID相关联的名称写入列。所以,我写了这个:
string constring5 = @"Data Source=antonio;Initial Catalog=JurisFrameWorkSPROC;User ID=;Password=";
string Query5 = "select Nome from Advogado where IDAdvogado= '" + dataGridView1.CurrentRow.Cells[6].Value + "'";
SqlConnection cn5 = new SqlConnection(constring5); /*Ligação à base de dados*/
SqlCommand cmd5 = new SqlCommand(Query5, cn5); /*Declaracao para usar comandos SQL*/
try
{
cn5.Open();
using (SqlDataReader read5 = cmd5.ExecuteReader())
{
while (read5.Read())
{
dataGridView1.CurrentRow.Cells[7].Value = (read5["Nome"].ToString());
}
}
}
finally
{
cn5.Close();
}
并且它有效,但仅适用于第1行,但我希望它适用于所有行! 行[e.RowIndex]在这里不起作用,因为发件人。
Anyony可以帮帮我吗? 大家都是
我已经这样做了
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string constring5 = @"Data Source=antonio;Initial Catalog=JurisFrameWorkSPROC;User ID=sa;Password=1236540nine";
string Query5 = "select Nome from Advogado where IDAdvogado= '" + dataGridView1.Rows[i].Cells[6].Value + "'";
SqlConnection cn5 = new SqlConnection(constring5); /*Ligação à base de dados*/
SqlCommand cmd5 = new SqlCommand(Query5, cn5); /*Declaracao para usar comandos SQL*/
try
{
cn5.Open();
{
using (SqlDataReader read5 = cmd5.ExecuteReader())
{
while (read5.Read())
{
//dataGridView1.CurrentRow.Cells[7].Value = (read5["Nome"]);
dataGridView1.Rows[i].Cells[7].Value = (read5["Nome"]);
}
}
}
}
finally
{
cn5.Close();
}
}
}
但是,如果我进行某种搜索,此代码到达的所有填充列都会消失:\