I Have question concerning about
datatabel.
When creating a datatable how can I change values in
this tabel .The way I'am trying to work out this problem
it just wount work. Code as follows:
using System;
using System.Data;
namespace ConsoleApplication4
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DataTable table = new DataTable("Customers");
//Creating columns
DataColumn column = new DataColumn("id");
DataColumn column2 = new DataColumn("customer");
//Adding columns to a table
table.Columns.Add(column);
table.Columns.Add(column2);
//Let's create few rows
DataRow row = table.NewRow();
row["id"] = 1;
row["customer"] = "John";
table.Rows.Add(row);
DataRow row2 = table.NewRow();
row2["id"] = 2;
row2["customer"] = "Mike";
table.Rows.Add(row2);
//This method won't work,it wont't set this value
table.Rows[1].ItemArray.SetValue("Changed Name",1);
//Step through datatable
for(int x=0;x<table.Rows.Count;x++)
{
Console.WriteLine(table.Rows[x].ItemArray.GetValue(1).ToString());
}
}
}
}

How To Modify DataRow Values In DataTable
John Penberthy
in datatable , but I found solution.
I don't know why's that so ,but in C#
I can change values in datatable using
foreach loop.
int x = 0;
foreach(DataRow myRow in table.Rows)
{
if(table.Rows[x].ItemArray.GetValue(1).ToString() == "Mike")
{
myRow[1] = "NEW CUSTOMER";
}
x++;
}
Thanks anyway :)
hocki101
table.Rows(x).Items("CUSTOMER") = "NEW CUSTOMER"
After you determine the number of rows you have, loop through and validate with an If-Then statement if you've reached the row related to the ID you pass to the routine. When the loop matches your ID, change the customer's name.
I don't know C# but here's a quick VB version
'build your datatable, assign those values to the 2 rows
DIM x as Integer
For x = 0 to table.rows.count - 1
If table.Rows(x).Items("ID") = 1 then
table.Rows(x).Items("CUSTOMER") = "New Name"
Exit For
End If
Next
If you can figure out that C# version, that will work...
Byrd Man
Ximena Cardenas
just like this
myRow[1]["customer"] = "It really works";
;)