snipt

Ctrl+h for KB shortcuts

C#

Add new row to DataTable

1
2
3
4
5
6
7
DataRow newRow = datatable.NewRow();
newRow["equipCode"] = column1;
newRow["missingDate"] = column2;
datatable.Rows.Add(newRow);

// OR
datatable.Rows.Add(new object[] { column1.ToString(), column2.ToString() });
https://snippets.siftie.com/embed/a7217017322f817f923ba7da9d984d7c/
/raw/a7217017322f817f923ba7da9d984d7c/
a7217017322f817f923ba7da9d984d7c
csharp
C#
7
2019-02-25T03:33:30
True
False
False
/api/public/snipt/38631/
add-new-row-to-datatable
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">DataRow</span> <span class="n">newRow</span> <span class="p">=</span> <span class="n">datatable</span><span class="p">.</span><span class="n">NewRow</span><span class="p">();</span> </span><span id="L-2"><a name="L-2"></a><span class="n">newRow</span><span class="p">[</span><span class="s">&quot;equipCode&quot;</span><span class="p">]</span> <span class="p">=</span> <span class="n">column1</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="n">newRow</span><span class="p">[</span><span class="s">&quot;missingDate&quot;</span><span class="p">]</span> <span class="p">=</span> <span class="n">column2</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="n">datatable</span><span class="p">.</span><span class="n">Rows</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">newRow</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="c1">// OR</span> </span><span id="L-7"><a name="L-7"></a><span class="n">datatable</span><span class="p">.</span><span class="n">Rows</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="k">new</span> <span class="kt">object</span><span class="p">[]</span> <span class="p">{</span> <span class="n">column1</span><span class="p">.</span><span class="n">ToString</span><span class="p">(),</span> <span class="n">column2</span><span class="p">.</span><span class="n">ToString</span><span class="p">()</span> <span class="p">});</span> </span></pre></div> </td></tr></table>
DataTable