Program that uses foreach on DataRow [C#]
using System;
using System.Data;
class Program
{
static void Main()
{
//
// Get the first row and loop over its ItemArray.
//
DataTable table = GetTable();
DataRow row = table.Rows[0];
foreach (object item in row.ItemArray)
{
if (item is int)
{
Console.WriteLine("Int: {0}", item);
}
else if (item is string)
{
Console.WriteLine("String: {0}", item);
}
else if (item is DateTime)
{
Console.WriteLine("DateTime: {0}", item);
}
}
}
}
Output
Int: 57
String: Koko
String: Shar Pei
DateTime: 4/6/2009 4:10:31 PMIs keyword. In this loop, we do not know what the exact type of each field in the ItemArray is. We can loop over the ItemArray with the foreach loop and then test each field. This is useful when developing methods that have to deal with flawed or invalid data. You can find more detailed information on foreach-loops on DataRows on this site.
DataTable Foreach Loop
Is Cast Example
Remove rows
You will find that there are Remove and RemoveAt methods on the DataRow collection. These function differently, but in both cases you must be certain after Remove that you are accessing valid data. In the next example, we remove the first row using Remove. If you try to access the DataRow, the runtime will throw an exception.
Program that removes DataRow [C#]
using System;
using System.Data;
class Program
{
static void Main()
{
//
// Get the first row for the DataTable
//
DataTable table = GetTable();
//
// Get the row and remove it.
//
DataRow row = table.Rows[0];
table.Rows.Remove(row);
//
// You cannot access row[0] now.
//
}
}Note. Just as a reminder, the exceptions are there to help you debug your program quicker. Don't let your blood pressure rise too much when you see them. Here is the exception raised by the above example.
Unhandled Exception:
System.Data.RowNotInTableException: This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.
Delete DataRows
In addition to Remove, the DataRow collection has a Delete instance method. This will cleanly delete that DataRow. When you try to access that row's index, you will get the next row. In other words, the DataRow erases the row and you cannot access it all after you call Delete.
Program that deletes DataRow [C#]
using System;
using System.Data;
class Program
{
static void Main()
{
//
// Get the first row for the DataTable
//
DataTable table = GetTable();
DataRow row = table.Rows[0];
//
// Delete the first row. This means the second row is the first row.
//
row.Delete();
//
// Display the new first row.
//
row = table.Rows[0];
Console.WriteLine(row["Name"]);
}
}
Output
Fido
Tuesday, June 19, 2012
Program that uses object array with DataTable
Program that uses object array with DataTable [C#]
using System;
using System.Data;
class Program
{
static void Main()
{
DataTable table = GetTable();
//
// We can instantiate a new object array and add it as a row.
//
object[] array = new object[4];
array[0] = 7;
array[1] = "Candy";
array[2] = "Yorkshire Terrier";
array[3] = DateTime.Now;
table.Rows.Add(array);
}
}
using System;
using System.Data;
class Program
{
static void Main()
{
DataTable table = GetTable();
//
// We can instantiate a new object array and add it as a row.
//
object[] array = new object[4];
array[0] = 7;
array[1] = "Candy";
array[2] = "Yorkshire Terrier";
array[3] = DateTime.Now;
table.Rows.Add(array);
}
}
Program that uses DataRow
Program that uses DataRow [C#]
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("Weight", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Breed", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now);
table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now);
table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
table.Rows.Add(25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now);
return table;
}
Result
(The DataTable contains five rows.)
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("Weight", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Breed", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now);
table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now);
table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
table.Rows.Add(25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now);
return table;
}
Result
(The DataTable contains five rows.)
Subscribe to:
Posts (Atom)