0

I need to add some "total rows" to a table. The code I am currently using for the sub totals is:

DataRow shRow = ds.Tables[0].NewRow();
shRow["a"] = "SubHead";
shRow["b"] = "Wheel Subs";
shRow["c"] = totalWheels;

Since I need totals and sub totals and sub sub totals I have to cut and paste this code (changing the values) for each new sub total.

I would like to have a function that takes the current table and values and adds the new row. Something like:

public DataRow SubRow(DataTable ResultsTable, string SubHead, string SubHeader, string SubHeadValue)

That would then update the table in the main function.

Thanks for any help. I just can't 'pass' the table to and from the new function.

jimmy
  • 709
  • 3
  • 15
  • 33

2 Answers2

0

Whats wrong with that?

public DataRow SubRow(DataTable resultsTable, string subHead, string subHeader, string subHeadValue)
{
    DataRow shRow = resultsTable.NewRow();
    shRow["a"] = subHead;
    shRow["b"] = subHeader;
    shRow["c"] = subHeadValue;
    return shRow;
}

Usage:

DataRow shRow = SubRow(ds.Tables[0], "SubHead", "Wheel Subs", totalWheels);

By the way, this will not add the row to the table, but only create a row that can fit into the table appropriately. If you actually want to ADD the row to the table, you can do:

DataRow row = ds.Tables[0].Rows.Add("SubHead", "Wheel Subs", totalWheels);

That will add a row with the given values to the Add method to the datatable, and return the row that has been added.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
0

I would like to have a function that takes the current table and values and adds the new row

public DataTable CreateNewRow(DataTable ResultsTable, String ColumnA, 
      String ColumnB, String ColumnC, String ColumnAVal, String ColumnBVal, 
      String ColumnCVal)
{
    DataRow shRow = ResultsTable.NewRow();
    shRow[ColumnA] = ColumnAVal;
    shRow[ColumnB] = ColumnBVal;
    shRow[ColumnC] = ColumnCVal;
    ResultsTable.Rows.Add(shRow);
    return ResultsTable; 
}

using(DataTable ResultsTable = CreateNewRow
                               (
                                  ds.Tables[0], 
                                  "a",
                                  "b",
                                  "c",
                                  "SubHead", 
                                  "Wheel Subs", 
                                  totalWheels
                               ))
{
      //Your logic
}
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Why on earth would you want to dispose the whole datatable after adding a row to it? I would think that he needs that table alive. On a different subject - if you want to add the row to the table, you don't need a method to it because one already exists in the framework (see my answer), and I think it is more logical to return the added datarow rather than the datatable, in general. – SimpleVar Jun 12 '12 at 16:39
  • It's a good practice to `dispose` the objects once it is done using it. and should be placed inside `Using` statement. Besides this, the data will still there after dispose until rows/columns are manually cleared. But after dispose, Memory management job is done and leaks will no longer be there. – Pankaj Jun 12 '12 at 16:54
  • The overload that accepts object params returns the datarow that has been added. Paste to VS and see for yourself. – SimpleVar Jun 12 '12 at 17:05
  • And I just don't think that he is DONE using it, that is what I meant. The scope of the datatable is unknown to us, and since he accesses it via ds.Tables[0], then it is probably not ours to dispose. – SimpleVar Jun 12 '12 at 17:09
  • In the **Using Statement** I mentioned the **Your Logic**. Complete logic should be written here. So once the `DataTable` job will be done, object will be `disposed` automatically after leaving this `scope`. – Pankaj Jun 12 '12 at 17:11
  • But the datatable doesn't only exists in the scope. He is probably using it all over the place. You shouldn't dispose a parameter, in general, because you don't expect a method to dispose something you are giving it for usage. – SimpleVar Jun 12 '12 at 17:13
  • No, OP is merely asking for a sample function. It's nowhere mentioned about global usage. Anyways, `Diposing` the `DataTable` never means that records/columns are removed. Information will always be there and memory leaks will be removed. – Pankaj Jun 12 '12 at 17:35
  • "the system.data namespace (ADONET) does not contain unmanaged resources. Therefore there is no need to dispose any of those as long as you have not added yourself something special to it." ([source](http://stackoverflow.com/questions/913228/should-i-dispose-dataset-and-datatable)). More over - as you said, the OP is merely asking for a sample function - so why add stuff that he doesn't necessarily need? Let him have the basic functionality, and decide for himself what he should add to the code. If you insist on adding functionality, you should do it as a suggestion (though now, not needed). – SimpleVar Jun 12 '12 at 18:07
  • Check with **[Red Gate ANTS Memory Profiler](http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/)** - Ignoring the memory leaks will cause `Memory Out of Exception`. – Pankaj Jun 12 '12 at 18:11
  • You keep talking about memory leaks - but there won't be any memory leaks. Once an object is no longer used, the garbage collector identifies that and disposes it, freeing it's memory. Memory leaks are more often seen when subscribing to events and not unsubscribing to them, etc, where a reference to an object still exists someplace though the object isn't used anymore. Has nothing to do with the case. By the way, it is `OutOfMemoryException`, not `Memory Out of Exception`. – SimpleVar Jun 12 '12 at 18:14
  • Garbage Collection can do it but it's not good practice to not dispose the objects manually. Did you check with Above link? – Pankaj Jun 12 '12 at 18:16
  • Yes, it is irrelevant in my opinion. Let us stop this conversation here. – SimpleVar Jun 12 '12 at 18:20