Questions tagged [datatable]

The term "datatable" is ambiguous. In .NET, it's a class that represents a table of in-memory data. In component based MVC frameworks like JSF and Wicket, it's an UI component that dynamically renders a HTML table based on a collection. For jQuery DataTables plugin, please use the [datatables] tag, for the data.table R package please use [data.table]. For the Python datatable package, use [py-datatable].

DataTable in .NET

A DataTable is a .NET class that represents one table of in-memory data. Unlike other programming languages and platforms, a .NET DataTable is not a GUI control but rather a representation of a SQL table directly accessible in code and a source of data for other controls.

A DataTable may exist as part of a DataSet, which represents an in-memory relational data store. In that context, they can be connected through instances of the DataRelation class, and constrained by ForeignKeyConstraint or UniqueConstraint instances.

A DataTable has a set of DataColumn instances, which describe its schema. Data is stored in DataRow instances.

A DataTable may be sorted and filtered without changing the data by attaching it to a DataView. The sorted and filtered rows are then accessed as instances of the DataRowView class.


DataTable in JSF

A <h:dataTable> is an UI component which allows you to render a HTML table dynamically based on a given List<Entity>. You can specify columns using <h:column>. Assuming that Entity is a fullworthy javabean with 3 properties id, name and value, here's an example how you can render a dynamically sized HTML table out of it:

<h:dataTable value="#{bean.entities}" var="entity">
    <h:column>#{entity.id}</h:column>
    <h:column>#{entity.name}</h:column>
    <h:column>#{entity.value}</h:column>
</h:column>

DataTable in Wicket

A data table builds on data grid view to introduce toolbars. Toolbars can be used to display sortable column headers, paging information, filter controls, and other information.

Data table also provides its own markup for an html table so the user does not need to provide it himself. This makes it very simple to add a datatable to the markup, however, some flexibility. (from Wicket 1.4.18 Javadoc)

22197 questions
95
votes
7 answers

Access cell value of datatable

Can anyone help me how to access for example value of first cell in 4th column? a b c d 1 2 3 5 g n m l for example, how to access to value d, if that would be datatable? Thanks.
el ninho
  • 4,183
  • 15
  • 56
  • 77
91
votes
7 answers

How I can filter a Datatable?

I use a DataTable with Information about Users and I want search a user or a list of users in this DataTable. I try it butit don't work :( Here is my c# code: public DataTable GetEntriesBySearch(string username,string location,DataTable table) …
Tarasov
  • 3,625
  • 19
  • 68
  • 128
90
votes
6 answers

How can I add a new column and data to a datatable that already contains data?

How do I add a new DataColumn to a DataTable object that already contains data? PseudoCode //call SQL helper class to get initial data DataTable dt = sql.ExecuteDataTable("sp_MyProc"); dt.Columns.Add("NewColumn",…
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
90
votes
4 answers

How to view a DataTable while debugging

I'm just getting started using ADO.NET and DataSets and DataTables. One problem I'm having is it seems pretty hard to tell what values are in the data table when trying to debug. What are some of the easiest ways of quickly seeing what values have…
Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
84
votes
7 answers

How to convert a column of DataTable to a List

I have a DataTable with multiple columns. I want to get a List out of first column of DataTable. How can I do that?
shrishjain
  • 841
  • 1
  • 6
  • 3
84
votes
3 answers

Querying DataColumnCollection with LINQ

I'm trying to perform a simple LINQ query on the Columns property of a DataTable: from c in myDataTable.Columns.AsQueryable() select c.ColumnName However, what I get is this: Could not find an implementation of the query pattern for source…
David Brown
  • 35,411
  • 11
  • 83
  • 132
81
votes
5 answers

.NET - Convert Generic Collection to DataTable

I am trying to convert a generic collection (List) to a DataTable. I found the following code to help me do this: // Sorry about indentation public class CollectionHelper { private CollectionHelper() { } // this is the method I have been…
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
79
votes
3 answers

How to add New Column with Value to the Existing DataTable?

I have One DataTable with 5 Columns and 10 Rows. Now I want to add one New Column to the DataTable and I want to assign DropDownList value to the New Column. So the DropDownList value should be added 10 times to the New Column. How to…
thevan
  • 10,052
  • 53
  • 137
  • 202
79
votes
6 answers

How do you Sort a DataTable given column and direction?

I need to resort, in memory, a DataTable based on a column and direction that are coming from a GridView. The function needs to look like this: public static DataTable resort(DataTable dt, string colName, string direction) { DataTable dtOut =…
Sam Gentile
  • 1,259
  • 7
  • 22
  • 28
78
votes
11 answers

SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column

I'm getting this exception when trying to do an SqlBulkCopy from a DataTable. Error Message: The given value of type String from the data source cannot be converted to type money of the specified target column. Target Site: System.Object…
Ricketts
  • 5,025
  • 4
  • 35
  • 48
78
votes
5 answers

Export DataTable to Excel with EPPlus

I want to export a data table to an Excel file with EPPlus. That data table has a property with int type, so I want the same format in the Excel file. Does anyone know way to export a DataTable like this to Excel?
Davood Hanifi
  • 1,234
  • 1
  • 13
  • 19
75
votes
5 answers

How do I get column names to print in this C# program?

I've cobbled together a C# program that takes a .csv file and writes it to a DataTable. Using this program, I can loop through each row of the DataTable and print out the information contained in the row. The console output looks like this: ---…
Kevin
  • 4,798
  • 19
  • 73
  • 120
75
votes
9 answers

Convert JSON to DataTable

I have JSON in the following format: [ {"id":"10","name":"User","add":false,"edit":true,"authorize":true,"view":true}, {"id":"11","name":"Group","add":true,"edit":false,"authorize":false,"view":true}, …
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
72
votes
11 answers

How to select min and max values of a column in a datatable?

For the following datatable column, what is the fastest way to get the min and max values? AccountLevel 0 1 2 3
Ahmed Atia
  • 17,848
  • 25
  • 91
  • 133
72
votes
10 answers

Convert IEnumerable to DataTable

Is there a nice way to convert an IEnumerable to a DataTable? I could use reflection to get the properties and the values, but that seems a bit inefficient, is there something build-in? (I know the examples like:…
Yvo
  • 18,681
  • 11
  • 71
  • 90