Lorenzo
Lorenzo

Reputation: 797

Add rows to dataTable to fill when it needs

well I have this data from a dataTable:

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

but I want something like this:

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

I can't do it from the store procedure, so I'm trying in the codebehind but I really have not idea, I try something but it's not working and well is not the best solution.

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

I have that code cause I will use it for a script. I can add rows with empty "total" or create some

Upvotes: 1

Views: 229

Answers (2)

Miguel Ruiz
Miguel Ruiz

Reputation: 61

I think that you can reach this goal easier using arrays or some collection kind.

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

I hope that it can help you.

Upvotes: 1

Tianyun Ling
Tianyun Ling

Reputation: 1097

suppose you have a DataTable dt, you can add new rows by using dt.Rows.Add(parameters)

Upvotes: 0

Related Questions