任何人都可以告诉我为什么这段代码不起作用?
我试图将DataTable(数据表数组)与网格视图控件绑定,但网格视图不显示任何数据,并且运行代码时没有显示错误。
void GetTop()
{
int DtIndex = Convert.ToInt32(TxtNoGroups.Text);
String cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
DataTable[] Groups = new DataTable[DtIndex];
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("select top 4 * from student ORDER BY GRADE DESC", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
DtTopStudents = new DataTable();
DtTopStudents.Load(rdr);
for (int i = 0; i < DtTopStudents.Rows.Count; i++)
{
Groups[i] = new DataTable();
Groups[i].TableName = "Group" + i;
Groups[i].Columns.Add("ID");
Groups[i].Columns.Add("Name");
Groups[i].Columns.Add("Grade");
}
for (int i = 1; i < DtIndex; i++)
{
dr = Groups[i].NewRow();
dr = DtTopStudents.Rows[i];
Groups[i].ImportRow(dr);
}
}
GridView1.DataSource = Groups[0];
GridView1.DataBind();
}
void GetTop()
{
int DtIndex = Convert.ToInt32(TxtNoGroups.Text);
String cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
DataTable[] Groups = new DataTable[DtIndex];
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("select top 4 * from student ORDER BY GRADE DESC", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
DtTopStudents = new DataTable();
DtTopStudents.Load(rdr);
for (int i = 0; i < DtTopStudents.Rows.Count; i++)
{
Groups[i] = new DataTable();
Groups[i].TableName = "Group" + i;
Groups[i].Columns.Add("ID");
Groups[i].Columns.Add("Name");
Groups[i].Columns.Add("Grade");
}
for (int i = 1; i < DtIndex; i++)
{
dr = Groups[i].NewRow();
dr = DtTopStudents.Rows[i];
Groups[i].ImportRow(dr);
}
}
GridView1.DataSource = Groups[0];
GridView1.DataBind();
}
答案 0 :(得分:0)
调试并检查Groups [0]是否有数据或是否为null,如果没有数据则返回1步并控制DtTopStudents ......