# GrapeCity.Documents.Excel.IPivotField.Group

## Content

<div class="doc-site-dotnet-api-container">



<h1 id="GrapeCity_Documents_Excel_IPivotField_Group_" data-uid="GrapeCity.Documents.Excel.IPivotField.Group*">Group Method
</h1>
<div class="markdown level0 summary"></div>
<div class="markdown level0 conceptual"></div>



<a id="GrapeCity_Documents_Excel_IPivotField_Group_" data-uid="GrapeCity.Documents.Excel.IPivotField.Group*"></a>
<h4 id="GrapeCity_Documents_Excel_IPivotField_Group_GrapeCity_Documents_Excel_PivotFieldDateGroupOptions_" data-uid="GrapeCity.Documents.Excel.IPivotField.Group(GrapeCity.Documents.Excel.PivotFieldDateGroupOptions)">Group(PivotFieldDateGroupOptions)</h4>
<div class="markdown level1 summary"><p>Groups the PivotField by date or time units.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">void Group(PivotFieldDateGroupOptions options)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Sub Group(options As PivotFieldDateGroupOptions)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="GrapeCity.Documents.Excel.PivotFieldDateGroupOptions.html">PivotFieldDateGroupOptions</a></td>
      <td><span class="parametername">options</span></td>
      <td><p>Date grouping settings.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="GrapeCity_Documents_Excel_IPivotField_Group_GrapeCity_Documents_Excel_PivotFieldDateGroupOptions__examples">Examples</h5>
<pre><code class="lang-csharp">Workbook workbook = new Workbook();
workbook.Options.Data.AutomaticGroupDateTimeInPivotTable = false;
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:C5"].Value = new object[,]
{
    { "Order Date", "Region", "Amount" },
    { new DateTime(2024, 1, 5), "East", 10 },
    { new DateTime(2024, 1, 18), "West", 20 },
    { new DateTime(2024, 2, 2), "East", 30 },
    { new DateTime(2024, 2, 25), "West", 40 }
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:C5"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["E1"], "PivotTable1");
IPivotField orderDateField = pivotTable.PivotFields["Order Date"];
orderDateField.Orientation = PivotFieldOrientation.RowField;
pivotTable.PivotFields["Amount"].Orientation = PivotFieldOrientation.DataField;
PivotFieldDateGroupOptions options = new PivotFieldDateGroupOptions
{
    GroupBy = PivotFieldDateGroupBy.Months,
    AutoStart = true,
    AutoEnd = true
};
orderDateField.Group(options);
string groupedFieldName = pivotTable.RowFields[0].Name;</code></pre>



<a id="GrapeCity_Documents_Excel_IPivotField_Group_" data-uid="GrapeCity.Documents.Excel.IPivotField.Group*"></a>
<h4 id="GrapeCity_Documents_Excel_IPivotField_Group_GrapeCity_Documents_Excel_PivotFieldNumberGroupOptions_" data-uid="GrapeCity.Documents.Excel.IPivotField.Group(GrapeCity.Documents.Excel.PivotFieldNumberGroupOptions)">Group(PivotFieldNumberGroupOptions)</h4>
<div class="markdown level1 summary"><p>Groups the PivotField into numeric ranges.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">void Group(PivotFieldNumberGroupOptions options)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Sub Group(options As PivotFieldNumberGroupOptions)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="GrapeCity.Documents.Excel.PivotFieldNumberGroupOptions.html">PivotFieldNumberGroupOptions</a></td>
      <td><span class="parametername">options</span></td>
      <td><p>Numeric grouping settings.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="GrapeCity_Documents_Excel_IPivotField_Group_GrapeCity_Documents_Excel_PivotFieldNumberGroupOptions__examples">Examples</h5>
<pre><code class="lang-csharp">Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:C5"].Value = new object[,]
{
    { "Order ID", "Region", "Amount" },
    { 10348, "East", 10 },
    { 10362, "East", 20 },
    { 10375, "West", 30 },
    { 10391, "West", 40 }
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:C5"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["E1"], "PivotTable1");
IPivotField orderIdField = pivotTable.PivotFields["Order ID"];
orderIdField.Orientation = PivotFieldOrientation.RowField;
pivotTable.PivotFields["Amount"].Orientation = PivotFieldOrientation.DataField;
PivotFieldNumberGroupOptions options = new PivotFieldNumberGroupOptions
{
    Start = 10348,
    End = 10407,
    AutoStart = false,
    AutoEnd = false,
    Interval = 20
};
orderIdField.Group(options);</code></pre>



<a id="GrapeCity_Documents_Excel_IPivotField_Group_" data-uid="GrapeCity.Documents.Excel.IPivotField.Group*"></a>
<h4 id="GrapeCity_Documents_Excel_IPivotField_Group_GrapeCity_Documents_Excel_PivotFieldCustomGroupOptions_" data-uid="GrapeCity.Documents.Excel.IPivotField.Group(GrapeCity.Documents.Excel.PivotFieldCustomGroupOptions)">Group(PivotFieldCustomGroupOptions)</h4>
<div class="markdown level1 summary"><p>Creates a custom group from PivotItems in the PivotField.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">void Group(PivotFieldCustomGroupOptions options)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Sub Group(options As PivotFieldCustomGroupOptions)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="GrapeCity.Documents.Excel.PivotFieldCustomGroupOptions.html">PivotFieldCustomGroupOptions</a></td>
      <td><span class="parametername">options</span></td>
      <td><p>The custom grouping settings. Must not be <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null">null</a>.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="GrapeCity_Documents_Excel_IPivotField_Group_GrapeCity_Documents_Excel_PivotFieldCustomGroupOptions__examples">Examples</h5>
<pre><code class="lang-csharp">Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:C5"].Value = new object[,]
{
    { "Product", "Region", "Amount" },
    { "Apple", "East", 10 },
    { "Banana", "West", 20 },
    { "Carrot", "East", 30 },
    { "Date", "West", 40 }
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:C5"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["E1"], "PivotTable1");
IPivotField productField = pivotTable.PivotFields["Product"];
productField.Orientation = PivotFieldOrientation.RowField;
pivotTable.PivotFields["Amount"].Orientation = PivotFieldOrientation.DataField;
PivotFieldCustomGroupOptions options = new PivotFieldCustomGroupOptions
{
    Name = "Fruit Group",
    Items = new List&lt;string&gt; { "Apple", "Banana" }
};
productField.Group(options);</code></pre>

<h5 class="exceptions">Exceptions</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Condition</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.argumentnullexception">ArgumentNullException</a></td>
      <td><p>If <code class="paramref">options</code> is <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null">null</a>.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.notsupportedexception">NotSupportedException</a></td>
      <td><p>If this field cannot be grouped.</p>
</td>
    </tr>
  </tbody>
</table>
</div>
