Link to home
Start Free TrialLog in
Avatar of dprimeus
dprimeusFlag for United States of America

asked on

Proper way to Display a empty row in a gridview

I am using gridviews in my application and they have bound columns and the datasource is a objectdatasource.  When no records are returned I would like the gridview to display an empty row (with column headers etc.).  From what I can tell the only way to accomplish this, without using an image within a EmptyDataTemplate or the worthless EmptyDataText property, is to create an arraylist and toss an empty oject into it and use that for the datasource. After the grid is bound I then have to blank out some cells because they have some default text in them (dates, boolens etc..).   Then I get my empty row.

Is there an easier way to accomplish what I am looking for?  Or I am I stuck just doing it like this?  The reason why I would like to change it is because there is no way to force the objectdatasource to give me a record count (that I can find anyway) before it is bound.  So I have to bind the objectdatasource to the grid then check the grid's Row.Count property and then if it is zero bind it again to my empty arraylist.  Kinda wasteful.  

Any Ideas?
Avatar of kGenius
kGenius
Flag of Belgium image

Is your objectdatasource an arraylist?
You can probably check before returning the arraylist if it is not null or length > 0. Else return an arraylist of your object with one item having the default empty values (nulls or empty strings, ...)

ie
if (myArrayList.Count > 0 || myArrayList != null) {
  return myArrayList;
} else {
  _myObj=new YourObject("", "", null, null);
  myArrayList.Add(myObj);
  return myArrayList;
}

hope this 'll help
kGenius
Avatar of dprimeus

ASKER

The ObjectDataSource class points to a class and that class has a procedure that returns all of the records from a table.  I was going to just have the class return an arraylist with an empty object when there are no records to return like you suggest (that was my first thought as well) but I do not want my classes returning an arraylist that has a count of 1 but does not actually have anything in it.  I think that could bite me later when I use the class for something else.  I would then have to determin if it only returns one row if that row is an empty row or a real row.  I think that could cause me more problems in the future in other places that class is used.

Nice thinking though.

I could create a special Grid procedure in my classes for returning the results for grids.  And have it call the normal method for return them then have it put the empty object in the arraylist.  That way I could still use the "normal" procedure for everything else.  Makes the class kinda clunky but that would be better than binding all the grids twice.

So I would have two:
Customer.GetCustomers
Customer.GetCustomersGrid (calls GetCustomers but puts a blank object in there if it is empty)

ASKER CERTIFIED SOLUTION
Avatar of kGenius
kGenius
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right that is what I am talking about doing.  I am going to just create another method GetCustomersGrid and it will call GetCustomers and check if the arraylist is empty.  If it is then it will put a empty Customer object in the arraylist.  My ObjectDataSource will just use the GetCustomersGrid method.