C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
DataColumn: This is a separate class. One is added when we invoke Columns.Add on a DataTable. This is confusing at first.
DataColumnGetTable: The DataTable is populated with five rows, each a separate dog with four data cells.
Add: Rows (a DataRowCollection) has an Add method that receives an object array. The array must have the expected number of elements.
C# program that uses DataRow
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;
}
Note: The object array here has an array Length equal to the number of columns. Object arrays can store any element type.
Array LengthObject ArrayC# program that uses object array with DataTable
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);
}
}
Here: We use the Rows indexer, Rows[0], to get the first row. We get the last row in the Rows collection by subtracting 1 from the Count.
C# program that gets DataRows
using System;
using System.Data;
class Program
{
static void Main()
{
//
// Get the first row from the DataTable.
//
DataTable table = GetTable();
DataRow row = table.Rows[0];
Console.WriteLine(row["Breed"]);
//
// Get the last row in the DataTable.
//
DataRow last = table.Rows[table.Rows.Count - 1];
Console.WriteLine(last["Breed"]);
}
}
Output
Shar Pei
Yorkshire Terrier
Note: You can avoid casting with the Field generic method. Please see the section further down for more info.
Here: We loop over the ItemArray with the foreach-loop and then test each field. This helps with flawed or invalid data.
IsC# program that uses foreach on DataRow
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 PM
Next: We remove the first row using Remove. If we try to access the DataRow, the runtime will throw an exception.
Important: Please remember that the exceptions are there to help you debug your program quicker.
C# program that removes DataRow
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 can no longer access row[0].
//
}
}
Output
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.
C# program that deletes DataRow
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
So: You can, for example, access int, string, and DateTime fields using the Field method.
Int, uintStringsDateTimeType: The type parameter, in sharp brackets, accepts the field type, while the function parameter accepts the field index.