One option is to modify your SQL to always return a row. If you're executing an SP, you can do a select count(*)
from the table using your where clause, and if that's zero, do something like select '' col1, '' col2 ...
and return that.
Another option is to check the count of rows return in your code. If you're using a DataTable
or DataSet
, this is easy, as you can look at DataTable.Rows.Count
or DataSet.Tables[0].Rows.Count
respectively. If you have none, add a row to the table then bind it to your grid. If you're binding to a DataReader, you can look at the DataReader.HasRows
property.
Another option is to extend the DataGrid and add your own "No Rows Available" display mode.
The best option all all depends on your level of experience and the environment in which this code is running.