2 Replies Latest reply on Feb 4, 2008 10:28 AM by stoopkid

    Checking for an empty DataGrid

    stoopkid Level 1
      Hello everyone! I'm working on an application for my University (NIU), and I'm almost finished but I'm having issues with one problem; checking if a datagrid is empty or not.

      The application sends an HTTPService request, and I have a result handler that I want to use to see if the DataGrid is filled or not. It's possible that users could search, but not get any results back. I'm trying to scan if a DataGrid is empty or not. Here is what I have so far, but it doesn't work =(


      if(urDG.rowCount==0) {
      Alert.show("No Result");
      } else {
      status1.text="Finished!";
      }

      Help is greatly appreciated! I'm a student application developer, so I still have lots to learn!
        • 1. Re: Checking for an empty DataGrid
          Ratsnackbar Level 2
          Well rowCount is only going to show the number of rows that will actually be displayed in the grid. So for example if you had a grid that defaults to displaying 5 rows REGARDLESS of what was returned by the data provider. (I.E. no data you still see 5 empty rows.) then using that as your indicator will not work. (At least I believe that is correct. Anyone feel free to correct me if I am wrong.)

          Even still what you want to do is to count the the number of records in your data provider. For example of you are using a List, Array or ArrayCollection you can use the Length property.

          Example:

          [Bindable]
          private var myGridDP:ArrayCollection = new ArrayCollection();

          // WebService or RemoteObject call to propigate the array collection here. I recommend RemoteObjects through //AMF to a ColdFusion back end. WebService's are for those who cannot figure out ColdFusion. :D

          In your data grid MXML call bind to the array collection:
          <mx:DataGrid id="myGrid" dataProvider="{this.myGridDP}" click="doSomething();" width="100%">

          When ever you want to check the record count of the data provider you could use...

          if(this.myGridDP.length <= 0){
          //do something here if it is less then 0
          } else {
          //You apparently have records so do something else here.
          }

          Or something like that. As for XML I believe there is a function to count the nodes but avoid XML at all costs. AMF has much less overhead then WebServices and ArrayCollections built in Methods make it SO much easier. (My Opinion). If you are stuck with WebServices and XML though just check the documentation and it should be there.

          Anyway hope that helps and was not too long winded.
          • 2. Re: Checking for an empty DataGrid
            stoopkid Level 1
            Thanks so much Ratsnackbar. This was the perfect fix. I really thought I'd be able to work through the DataGrid, instead of the DataProvider. I can't beleive I never thought to check the length of my array though!