索引超出范围。必须为非负值并小于集合大小。 参数名: index 50
toolStripStatusLabel1.Text=yhxx.Yonghuxingming;toolStripStatusLabel2.Text=yhxx.Yonghu...
toolStripStatusLabel1.Text = yhxx.Yonghuxingming;
toolStripStatusLabel2.Text = yhxx.Yonghuquanxian;
string sql = "select * from dd where yonghuming='{0}'";
sql = string.Format(sql, toolStripStatusLabel1.Text);
SqlCommand comm = new SqlCommand(sql, conn.GetCon());
SqlDataReader dr = comm.ExecuteReader();
int row = 0;
if (dr.Read())
{
dataGridView1.Rows[row].Cells[0].Value = dr.GetString(0);
dataGridView1.Rows[row].Cells[1].Value = dr.GetString(1);
dataGridView1.Rows[row].Cells[2].Value = dr.GetString(2);
dataGridView1.Rows[row].Cells[3].Value = dr.GetString(3);
dataGridView1.Rows[row].Cells[4].Value = dr.GetSqlDateTime(4);
dataGridView1.Rows[row].Cells[5].Value = dr.GetString(5);
dataGridView1.Rows[row].Cells[6].Value = dr.GetString(6);
dataGridView1.Rows[row].Cells[7].Value = dr.GetString(7);
dataGridView1.Rows[row].Cells[8].Value = dr.GetString(8);
dataGridView1.Rows[row].Cells[9].Value = dr.GetString(9);
row++;
}
怎么改? 展开
toolStripStatusLabel2.Text = yhxx.Yonghuquanxian;
string sql = "select * from dd where yonghuming='{0}'";
sql = string.Format(sql, toolStripStatusLabel1.Text);
SqlCommand comm = new SqlCommand(sql, conn.GetCon());
SqlDataReader dr = comm.ExecuteReader();
int row = 0;
if (dr.Read())
{
dataGridView1.Rows[row].Cells[0].Value = dr.GetString(0);
dataGridView1.Rows[row].Cells[1].Value = dr.GetString(1);
dataGridView1.Rows[row].Cells[2].Value = dr.GetString(2);
dataGridView1.Rows[row].Cells[3].Value = dr.GetString(3);
dataGridView1.Rows[row].Cells[4].Value = dr.GetSqlDateTime(4);
dataGridView1.Rows[row].Cells[5].Value = dr.GetString(5);
dataGridView1.Rows[row].Cells[6].Value = dr.GetString(6);
dataGridView1.Rows[row].Cells[7].Value = dr.GetString(7);
dataGridView1.Rows[row].Cells[8].Value = dr.GetString(8);
dataGridView1.Rows[row].Cells[9].Value = dr.GetString(9);
row++;
}
怎么改? 展开
1个回答
展开全部
dataGridView1.Rows[row]当然会超出索引,因为dataGridView1的Rows是空的
绑定数据是用如下代码
dataGridView1.DataSource = dr;
绑定数据是用如下代码
dataGridView1.DataSource = dr;
更多追问追答
追问
我现在加了,可是还是上边的问题,怎么办?
追答
toolStripStatusLabel1.Text = yhxx.Yonghuxingming;
toolStripStatusLabel2.Text = yhxx.Yonghuquanxian;
string sql = "select * from dd where yonghuming='{0}'";
sql = string.Format(sql, toolStripStatusLabel1.Text);
SqlCommand comm = new SqlCommand(sql, conn.GetCon());
SqlDataReader dr = comm.ExecuteReader();
dataGridView1.DataSource = dr;
上面其他那一大段都不要了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询