我想从数据库插入datagridview,但我得到:
指数超出范围。必须是非负数且小于 集合。参数名称:index
datagridtampil.ColumnCount = 6;
datagridtampil.Columns.Insert(0, cek);
try
{
string sql = "select * from "+tipe;
cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
int j = 0;
while (reader.Read())
{
for (int i = 0; i <= 5; i++)
{
datagridtampil.Rows[j].Cells[i+1].Value = reader[i].ToString();
}
j += 1;
}
}
}
出了什么问题?
答案 0 :(得分:0)
试试这个
datagridtampil.Rows.Clear();
if (reader.HasRows)
{
int j = 0;
while (reader.Read())
{
j = datagridtampil.Rows.Add()
for (int i = 1; i < datagridtampil.Columns.Count; i++)
{
datagridtampil.Rows[j].Cells[i].Value = reader.GetValue(i-1).ToString();
}
}
}