2011-08-13 125 views
2

你好我有一個代碼,使用數據集拉到GridView的數據,什麼是最好的方式來檢查,如果Gridview是空的,如果它不會拋出一個錯誤..正確的現在我的GridView有如果空的,以顯示消息的設置..但我只是想試圖獲取數據在數據集中Gridview檢查是否爲空或空

Students students = new Students(); 
    DataSet studentsList = students.GetAllStudents(); 
    GridView1.DataSource = studentsList; 
    GridView1.DataBind(); 

回答

3

如果我明白你的問題正確,後爲空,空管檢查爲什麼不在將它綁定到GridView之前,請檢查if the DataSet is empty

如果是,就不要綁定它。

DataSet studentsList = students.GetAllStudents(); 
bool empty = IsEmpty(studentsList); // check DataSet here, see the link above 
if(empty) 
{ 
    GridView1.Visible = false; 
} 
else 
{ 
    GridView1.DataSource = studentsList; 
    GridView1.DataBind(); 
} 
0

你可以指望返回的行如果有數據或不:

DataSet studentsList = students.GetAllStudents(); 

if(studentList.Tables[0].Rows.Count > 0) //COUNT DATASET RECORDS 
{ 
    GridView1.DataSource = studentsList; 
    GridView1.DataBind(); 
} 
else 
{ 
    lblError.Text = "NO RECORDS FOUND!"; 
} 

問候