向dataTable添加行以在需要时填充

时间:2013-04-11 23:40:09

标签: c# logic

我从dataTable获得了这些数据:

Recom   House   49
Recom   Street  47
Recom   Floor   29
Area    House   5
Area    Floor   1
Observ  Floor   1

但我想要这样的事情:

Recom   House   49
Recom   Street  47
Recom   Floor   29
Area    House   5
**Area  Street  0**
Area    Floor   1
**Observ    House   0**
**Observ    Street  0**
Observ  Floor   1

我无法通过商店程序进行操作,因此我尝试了代码隐藏,但我真的不知道,我尝试了一些东西,但它没有用,而且不是最好的溶液

int getTotal = ((from a in dtSerieDrill.AsEnumerable() 
                                             where a.Field<int>("idTipoRevision") == Int32.Parse(dtSerie.Tables[0].Rows[i]["idTipoRevision"].ToString())
                                             select a.Field<string>("TipoRevision")).Distinct()).ToArray().Length;

                            if (getTotal == arrayCat.Length) 
                            {
                                auxData += "{";
                                auxData += "name:'" + dtSerieDrill.Rows[n]["TipoRevision"].ToString() + "',";
                                auxData += "y:" + Double.Parse(dtSerieDrill.Rows[n]["Total"].ToString()) + "";
                                auxData += "},";
                            } else {
                                for (int g = 0; g < arrayCat.Length - 1; g++) 
                                {
arrayCat //categories(house,street,floor,etc)
                                    if (!arrayCat[g].Equals(dtSerieDrill.Rows[n]["Especialidad"].ToString()) && !(totalAux == (g+1))) 
                                    {
                                        auxData += "{";
                                        auxData += "name:'" + arrayCat[g] + "',";
                                        auxData += "y:0";
                                        auxData += "},";
                                        break;
                                    } else {
                                        auxData += "{";
                                        auxData += "name:'" + dtSerieDrill.Rows[n]["TipoRevision"].ToString() + "',";
                                        auxData += "y:" + Double.Parse(dtSerieDrill.Rows[n]["Total"].ToString()) + "";
                                        auxData += "},";
                                        break;
                                    }
                                }
                            }

我有那个代码因为我将它用于脚本。我可以添加空行&#34; total&#34;或创建一些

2 个答案:

答案 0 :(得分:1)

我认为您可以使用数组或某种集合类型更轻松地实现此目标。

E.g。

//create you context
YourContext dbContext = new YourContext();

String[] firstCategory = dbContext.Categories.Where(x => x.categoryType == "YourFirstCategoryType").ToArray();
String[] secondCategory = dbContext.Categories.Where(x => x.categoryType == "YourSecondCategoryType").ToArray();

DataTable dt = new DataTable();
dt.Columns.Add("Category1");
dt.Columns.Add("Category2");
dt.Columns.Add("Total");

foreach(var c1 in firstCategory)
   foreach(var c2 in firstCategory)
   {
      //create a new row object 
      DataRow R = dt.NewRow();

      //set value to the colums
      R[0] = c1;
      R[1] = c2;
      R[2] = GetTotal(c1,c2);//GetTotal is some function that query database and returns total when you have c1 and c2 parameter, you should check is this combination doesn't exist you returns 0
      dt.Rows.Add(R);
   }

我希望它可以帮助你。

答案 1 :(得分:0)

假设您有一个DataTable dt,可以使用dt.Rows.Add(参数)添加新行