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);
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...
C# does not have Items property 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";
Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.