【问题标题】:How to add data from Firebase to DataGridView using FireSharp如何使用 FireSharp 将数据从 Firebase 添加到 DataGridView
【发布时间】:2019-12-24 18:27:49
【问题描述】:

我只想将数据从 Firebase 检索到 DataGridView。我拥有的代码已经在检索数据,但是,它将所有内容检索到同一行而不是创建新行。我是编码初学者,所以我真的需要帮助。

我在网上看到 Firebase 不会“计算”数据,因此需要创建一个计数器,因此每次添加或删除数据时,都需要进行更新。我做到了,它正在工作。我创建了一个加载数据的方法。

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:一个 DataTable 已经存在(dataTable),还有一个 DataGridView 也有列(ID、Name、Image、Price、Discount、Description),这些列与给定的 .Cells[x] 的数量和顺序相匹配。当表单加载时,dtGProductsList.DataSource = dataTable;我尝试将 [0] 替换为 [i]。

我希望将正在检索的数据设置为新行而不是相同的行,并且不会跳过行。如果它太简单了,我很抱歉,但我看不到出路。

【问题讨论】:

    标签: c# firebase-realtime-database datagridview fire-sharp


    【解决方案1】:

    我遇到了同样的问题,这里是 mu 解决方案:

    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); 
    
    }
    

    【讨论】:

    • 注意,需要添加参考Netwonsoft.jason
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多