ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Change DataTable column value based on condition in C# and VB.Net<p>Hi <a class="username" rel="jon"> jon</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(string)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;,typeof(string)) }); dt.Rows.Add(&#34;1&#34;, &#34;John Hammond&#34;, &#34;United States&#34;); dt.Rows.Add(&#34;2&#34;, &#34;Mudassar Khan&#34;, &#34;India&#34;); dt.Rows.Add(&#34;003&#34;, &#34;Suzanne Mathews&#34;, &#34;France&#34;); dt.Rows.Add(&#34;4&#34;, &#34;Robert Schidner&#34;, &#34;Russia&#34;); DataTable newTable = dt.Copy(); foreach (DataRow row in newTable.Rows) { if (row[0].ToString().Length == 3) { row[0] = &#34;00&#34; + row[0].ToString(); } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(2) {New DataColumn(&#34;Id&#34;, GetType(String)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Country&#34;, GetType(String))}) dt.Rows.Add(&#34;1&#34;, &#34;John Hammond&#34;, &#34;United States&#34;) dt.Rows.Add(&#34;2&#34;, &#34;Mudassar Khan&#34;, &#34;India&#34;) dt.Rows.Add(&#34;003&#34;, &#34;Suzanne Mathews&#34;, &#34;France&#34;) dt.Rows.Add(&#34;4&#34;, &#34;Robert Schidner&#34;, &#34;Russia&#34;) Dim newTable As DataTable = dt.Copy() For Each row As DataRow In newTable.Rows If row(0).ToString().Length = 3 Then row(0) = &#34;00&#34; &amp; row(0).ToString() End If Next End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/QE3Ev0v.jpg" alt="" width="357" height="226" /></p>https://www.aspforums.net:443/Threads/158800/Change-DataTable-column-value-based-on-condition-in-C-and-VBNet/https://www.aspforums.net:443/Threads/158800/Change-DataTable-column-value-based-on-condition-in-C-and-VBNet/Thu, 20 Jun 2019 06:40:06 GMT