GST Billing Software

header image

DataTable Methods

Erachana Line

A DataTable object represents tabular data as an in-memory, tabular cache of rows, columns, and constraints. You typically use the DataTable class to perform any disconnected data access. The DataTable is a central object in the ADO.NET library. Other objects that use the DataTable include the DataSet and the DataView.

DataTable objects are conditionally case-sensitive. For example, if you are using one DataTable named "ObjDataTable" and another one named "objDataTable", a string used to search for one of the tables is regarded as case-sensitive. However, if "ObjDataTable" exists and "objDataTable" does not, the search string is regarded as case-insensitive.


Table 1: The Data Table Class Methods

METHOD

DESCRIPTION

AcceptChanges

Commits all the changes made since last AcceptChanges was called

Clear

Deletes all data table data

Clone

Creates a clone of a DataTable including its schema

Copy

Copies a data table including its schema

NewRow

Creates a new row, which is later added by calling the Rows.Add method

RejectChanges

Reject all changed made after last AcceptChanges was called

Reset

Resets a data table's original state

Select

Gets an array of rows based on the criteria

Compute

Compute Method is used to perform operations on the Rows

AcceptChanges()

This method saves changes which made with records in a Datatable.
Take another dataGridView control and a Button. Add below code.

private void btnacceptchanges_Click(object sender, EventArgs e)
{
dt.AcceptChanges();
dataGridView2.DataSource = dt;
}

Run the application. Make some changes in rows value. I have changed department name as "cs" for first row and added a new row at last.

Output

Data Table

Click the "AcceptChanges" button. Second dataGridView will show updated record.

Data Table
Clear()

This method clears (removes) all data from datatable. Take a button and write the following code on click event.

private void btnclear_Click(object sender, EventArgs e)
{
dt.Clear();
}

Output

Data Table

Click the "Clear" button. It will clear all the data.

Output

Data Table

Clone()

The clone method copy the structure of Datatable. Means it copy only schema not full records of Datatable. Take another dataGridView control and a Button. Add below code.

private void btnclone_Click(object sender, EventArgs e)
{
DataTable dt1 = dt.Clone();
dataGridView2.DataSource = dt1;
}

Output
Data Table

Click the "Clone" button.

Output


Data Table

Copy()

The copy method copy the whole records with structure of Datatable. Take another dataGridView control and a Button. Write the following code on button click.

private void btncopy_Click(object sender, EventArgs e)
{
DataTable dt1 = dt.Copy();
dataGridView2.DataSource = dt1;
}

Output

Data Table

Click the "copy" button. It will copy all the data with structure of datatable into another datatable and show records in dataGridView.

Output

Data Table


GetChanges()

This method does copy of all records of datatable with changes made with record. Add a button anddataGridView and write the following code on button click.

private void btnGetChanges_Click(object sender, EventArgs e)
{
dt.GetChanges();
dataGridView2.DataSource = dt;
}

Output

I have changed some column value like department as "CS" of first row and city as "Varanasi" for last row.

Data Table

Click the "GetChanges" button. It will show all record.
Output

Data Table
NewRow()

The NewRow method creates a new row with same schema of datatable. Take a button > Set it's Text property to "NewRow" and write the following code.

private void btnnewrow_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["ID"] = 444;
dr["Name"] = "Pramod";
dr["Department"]="CS";
dr["City"] = "Allahabad";
//Add the row to datatable
dt.Rows.Add(dr);
}

Run the application.

Output

Data Table

Click the "NewRow" button. It will add new row to datatable.

Output

Data Table


Compute()

The DataTable Compute Method is used to perform operations on the Rows.

It can be used to perform aggregate operations on the DataTable.

Syntax

public Object Compute(
string expression,
string filter
)

Here expression is the expression to compute

Filter - The filter is similar to where condition clause in SQL Server to restrict rows.

Performing Operations using Compute Method

Let us Perform Avg of Salary on the DataTable which we have created.

Snippet :

// Declare an object variable.
object AvgTotal;
AvgTotal = dt.Compute("Avg(Salary)", "");
MessageBox.Show("Average Salary is : " + AvgTotal.ToString());

Here, the Filter condition is empty indicating that all rows are computed.

Data Row Methods

A DataRow represent a row of data in data table. You add data to the data table using DataRow object. A DataRowCollection object represents a collection of data rows of a data table. You use DataTable's NewRow method to return a DataRow object of data table, add values to the data row and add a row to the data Table again by using DataRowCollection's Add method.



Table 1: The Data Row class Methods

METHOD

DESCRIPTION

AcceptChanges

Comments all the changes made to this row

BeginEdit

Starts an edit operation on a row

CancelEdit

Cancels the current edit on a row

Delete

Deletes a DataRow

EndEdit

Ends the current edit on a row

GetChildRows

Returns child rows of a DataRow

GetParentRows

Returns parent rows of a DataRow

RejectsChanges

Rejects all the changes made since last AcceptChanges

private void CreateCustomersTable()
{
// More code . . .
row3.RejectChanges();
row2.Delete();

// Bind dataset to the data grid
dataGrid1.DataSource = ds.DefaultViewManager;
}



Data Table

If we execute above function, then output will be


Data Table