0

I just want to retrieve data from a Firebase to DataGridView. The code I have is retrieving data already, however, it's retrieving everything to the same row instead of creating a new one. I'm a beginner in coding, so I really need help with that.

I read online that Firebase doesn't "Count" data, so it'd be needed to create a counter, so each time I add or delete data, an update would be needed. I did it and it's working. I created a method to load the data.

private async Task firebaseData()
{
 int i = 0;

 FirebaseResponse firebaseResponse = await client.GetAsync("Counter/node");
 Counter_class counter = firebaseResponse.ResultAs<Counter_class>();
 int foodCount = Convert.ToInt32(counter.food_count);

 while (true)
 {
  if (i == foodCount)
  {
   break;
  }
 i++;
 try
  {

   FirebaseResponse response2 = await client.GetAsync("Foods/0" + i);
   Foods foods = response2.ResultAs<Foods>();


   this.dtGProductsList.Rows[0].Cells[0].Value = foods.menuId;
   this.dtGProductsList.Rows[0].Cells[1].Value = foods.name;
   this.dtGProductsList.Rows[0].Cells[2].Value = foods.image;
   this.dtGProductsList.Rows[0].Cells[3].Value = foods.price;
   this.dtGProductsList.Rows[0].Cells[4].Value = foods.discount;
   this.dtGProductsList.Rows[0].Cells[5].Value = foods.description;

  }
   catch
   {

   }
 }
MessageBox.Show("Done");
}

OBS: A DataTable exists already(dataTable), there's a DataGridView too which has columns(ID,Name, Image, Price, Discount, Description), which match the number and order given to the .Cells[x]. When the Form loads, dtGProductsList.DataSource = dataTable; I tried replacing [0] for [i].

I expect the data that is beeing retrieved to be set to a new row and not to the same, and to not skip rows. I'm sorry if it's too simple, but I can't see a way out.

Ita.Rios
  • 1
  • 1

1 Answers1

0

I Faced the same problem and here is mu solution :

Counter_class  XClass = new Counter_class();                
FirebaseResponse firebaseResponse = await client.GetAsync("Counter/node");
string JsTxt = response.Body;
                if (JsTxt == "null")
                {

                    return ;
                }

 dynamic data = JsonConvert.DeserializeObject<dynamic>(JsTxt);
                var list = new List<XClass >();
                foreach (var itemDynamic in data)
                {
                  list.Add(JsonConvert.DeserializeObject<XClass > 
                  (((JProperty)itemDynamic).Value.ToString()));
                }
             // Now you have a list  you can loop through to put it at any suitable Visual  
            //control 
                foreach ( XClass  _Xcls in list)
                {
Invoke((MethodInvoker)delegate {
                            DataGridViewRow row(DataGridViewRow)dg.Rows[0].Clone();
                            row.Cells[0].Value =_Xdcls...
                            row.Cells[1].Value =Xdcls...
                            row.Cells[2].Value =Xdcls...
                            ......
                            dg.Insert(0, row); 

}
Isaac Be
  • 81
  • 1
  • 10