How do I extract data from a DataTable?
The DataTable has a collection .Rows
of DataRow elements.
Each DataRow corresponds to one row in your database, and contains a collection of columns.
In order to access a single value, do something like this:
foreach(DataRow row in YourDataTable.Rows)
{
string name = row["name"].ToString();
string description = row["description"].ToString();
string icoFileName = row["iconFile"].ToString();
string installScript = row["installScript"].ToString();
}
How To Extract Values From DataTable To List in C#
To extract data from a data table use the .Field<T>()
extension method from System.Data.DataSetExtensions
. Please see the documentation DataRow.Field.
You need to create a list of type PurchaseInvoice
and iterate over the DataRow
collection table.Rows
.
Please see example below:
// create a list for your invoices
List<PurchaseInvoice> invoices = new List<PurchaseInvoice>();
foreach (DataRow row in dtPurchaseInvoice.Rows)
{ // ^ Rows
// create invoice
PurchaseInvoice invoice = new PurchaseInvoice();
// get an int
invoice.PurchaseInvoiceNo = row.Field<int>("PurchaseInvoiceNo");
// get a string
invoice.CustomerName = row.Field<string>("CustomerName");
// get a DateTime
invoice.PurchaseDate = row.Field<DateTime>("PurchaseDate");
// get a double
invoice.PurchaseValue = row.Field<double>("PurchaseValue");
// add invoice to list
invoices.Add(invoice);
}
Using .Field<T>()
extension means you do not need to convert/cast the object into the correct type - it is handled by the extension for you.
It also works with nullable fields:
DateTime? date = row.Field<DateTime?>("PurchaseDate");
Extract data from datatable
In order to get the sheet name, using oledb, you will need to use code that looks something like this (thanks to this SO post and answer):
DataTable dtSheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
List<string> sheets= new List<string>();
foreach (DataRow dr in dtSheets.Rows)
{
if (dr["TABLE_NAME"].ToString().Contains("$"))//checks whether row contains '_xlnm#_FilterDatabase' or sheet name(i.e. sheet name always ends with $ sign)
{
sheets.Add(dr["TABLE_NAME"].ToString());
}
}
Below is how you access the values from a datatable:
var someValue = dt.Rows[i][j]
You need to get the item at the column index (j) of the row, at the row index (i), of the current datatable (dt).
Conversely, you can use the name of the column as well.
var someValue = dt.Rows[i]["columnName"]
Extracting data from datatable with conditions c#
You can do the following:
private void GetRowsByFilter()
{
DataTable yourDataTable = new DataTable(); //Your DataTable is supposed to have the data
// Presuming the DataTable has a column named user.
string expression;
expression = "user = \"msalmon\"";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
Extract only filtered data from table in Dash Python
Globals are generally something to avoid, especially in Dash. The good news is, the fix should be pretty easy here. Since you're using custom filtering on the back end, rather than native, front-end filtering, I think what you need is to add the data
prop to your download callback as state. If you'd been using front-end filtering, you'd have to use the derived_virtual_data
prop instead. The following should work.
@app.callback(
Output("download-dataframe-csv", "data"),
Input("button_export", "n_clicks"),
State("table-sorting-filtering", "data"),
prevent_initial_call=True,
)
def export_on_click(n_clicks, table_data):
df = pd.DataFrame.from_dict(table_data)
return dcc.send_data_frame(df.to_excel, "export.xlsx")
Related Topics
Is There a C# Case Insensitive Equals Operator
How to Clean HTML Tags Using C#
Read and Write File on Streamingassetspath
Request Exceeds the Configured Maxquerystringlength When Using [Authorize]
How Does Hashset Compare Elements for Equality
What Is the Connection String for Localdb for Version 11
How to Use Enumwindows to Find Windows with a Specific Caption/Title
Enforce an Async Method to Be Called Once
Replace Only Some Groups with Regex
Log4Net Rolling Daily Filename with Date in the File Name
Adding Distance to a Gps Coordinate
How to Convert JavaScript Date Object to Ticks
Disable JavaScript Error in Webbrowser Control
How to Put a Task to Sleep (Or Delay) in C# 4.0
How to Test for the Presence of an Action Filter with Constructor Arguments