I have created and returned datatable, this table has 10 columns. Now i want to filter from this table based on some dynamic search parameters. How to do this? any idea would be timely help.
// This function will create and return the source table.
var DisplayTable = CreateQueryTable();
Here I want to do dynamic search like If col1=MyName and Col2=MyCity
ResultGrid.DataSource = DisplayTable;
ResultGrid.DataBind();
Panel1.Controls.Add(ResultGrid);
You can do this in these way,
1.Creating DataView Like
var dv = dataTable.DefaultView;
dv.RowFilter = "col1='MyName' and Col2='MyCity'"; // if MyName and MyCity are literal string.
or
dv.RowFilter = "col1='"+MyName+"' and Col2 ='"+ MyCity +"'";// if MyName and MyCity are string variable.
2.With DataTable Select Method, It will return array of DataRow
var rows = dataTable.Select("col1='MyName' and Col2='MyCity'"); //if string literal
or
var rows = dataTable.Select("col1='"+MyName+"' and Col2='"+MyCity+"'"); // if string variable
3.By Linq
var filterdData = from row in dataTable.AsEnumerable()
where row.Field<string>("col1") == "MyName"
&& row.Field<string>("col2") == "MyCity"
select row;
you create DataView of your datatable and use Filter
// Create a DataView
DataView dv = new DataView(yourDataTable);
dv.RowFilter = "col1='MyName' and Col2='MyCity'";
//Bind your grid with DataView
You can also use select method on your table
DataRow[] foundRows;
foundRows = yourDataTable.Select("col1='MyName' and Col2='MyCity'");
You can also use Linq To DataTable
var results = from myRow in yourDataTable.AsEnumerable()
where myRow.Field<string>("col1") == Myname &&
myRow.Field<string>("Col2") == MyCity
select myRow;
Related
DataTable Master_Table = Execute_Manager.getTableDataSet(connection, select_query, master_table);
string RuleName = (From EachRow in Master_Table
where EachRow.Field<string>("RuleID") == "123456"
Select EachRow.Field<string>("RuleName")).CopyToDataTable().ToString();
I need to get single Column Value using LINQ in Datatable c#
You could do something like the following I have added in the two examples below:
string carManufacturer = "BMW";
DataTable dt = new DataTable();
int id = (from DataRow dr in dt.Rows
where (string)dr["CarManufacturer"] == carManufacturer
select (int)dr["id"]).FirstOrDefault();
string columnValue = dt.Rows[0]["ColumnName"].ToString();
What column value are you trying to fetch from your data table?
I have a data table and i'm filtering data from it. now I want to get full table into var like below
DataView dv = new DataView(table);
dv.RowFilter = "[CRUISE-ONLY]='YES' AND [NOW-AVAIL]='YES' AND [1A] <> 'N/A'";
table = dv.ToTable();
var allneededmsc = (from DataRow dr in table.Rows
where (string)dr["CRUISE-ONLY"] == "YES" &&
(string)dr["NOW-AVAIL"] == "YES" &&
(string)dr["1A"] != "N/A"
select (string)dr["aLL COLUMNS"]);
//here I want to select full table which fulfill the conditions.
how can I do this.
I'm trying to set selectively display certain text using a databind.
the code looks like this..
DataTable oDt;
oDt = Apps.GetAll();
if (oDt.Rows.Count > 0)
{
oDt.Columns.Add("AppName_ID", typeof(string), "App_Name + ' (' + App_ID + ')'");
CmbApps.DataSource = oDt;
CmbApps.DataValueField = "App_ID";
CmbApps.DataTextField = "AppName_ID";
CmbApps.DataBind();
}
The problem is that the first value shows up as: Select (0).. so I'm trying to change the datatextfield when "App_ID" = 0 so that App_ID is NOT displayed, but is in all other values.
Not sure about the syntax, but it will be close to the below.
DataTable dt2 = oDt.Select("(App_ID != 0)").CopyToDataTable();
Using Linq-To-DataTable
DataTable tblFiltered = oDt.AsEnumerable()
.Where(row => row.Field<String>("App_ID") == "1")
.CopyToDataTable();
Or you can use DataView and RowFilter
DataView dataView = oDt.DefaultView;
dataView.RowFilter = "App_ID <> 0";
UPDATE
foreach (DataRow DRow in oDt.Rows)
{
if(DRow["app_id"].ToString().Equals("0"))
DRow["AppName_ID"] = "Select";
}
I have to perform the aggregate function on the DataTable like Datatable.Compute but compute return the object i want to perform the aggregate function on the datatable and get the datarow .
_summaryTable.Compute("min(FareAdult)", whereClause
+ "AirlineDisplayName='"
+ Convert.ToString(airline["AirlineDisplayName"])
+ "' and ( Stops=0) ");
but above code will only return the min(FareAdult) but i want to select the two column based on the above condition from datatable.
How can i do it through Linq
I have to select min(FareAdult) and the TotelPrice value of same row
use Select instead compute
_summaryTable.Select("FilterationExpression");
DataRow[] dr = _summaryTable.Select("min(FareAdult),AirlineDisplayName='" + Convert.ToString(airline["AirlineDisplayName"]) + "' and ( Stops=0) ");
Here is a LINQ method. This is pseudocode since I don't know the typing of your row, and I haven't been able to test it, but the idea is the same. Use LINQ to select the rows that match your criteria, order by the FareAdult and then select the first (minimum).
var minResult = (from row in _summaryTable.Rows
where row.AirlineDisplayName == airline["AirlineDisplayName"] && row.Stops == 0
orderby row.FareAdult
select row).FirstOrDefault();
private void CalcColumns()
{
DataTable table = new DataTable ();
//enter code here
// Create the first column.
DataColumn priceColumn = new DataColumn();
priceColumn.DataType = System.Type.GetType("System.Decimal");
priceColumn.ColumnName = "price";
priceColumn.DefaultValue = 50;
// Create the second, calculated, column.
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
taxColumn.ColumnName = "tax";
taxColumn.Expression = "price * 0.0862";
// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
totalColumn.Expression = "price + tax";
// Add columns to DataTable.
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
DataRow row = table.NewRow();
table.Rows.Add(row);
DataView view = new DataView(table);
dataGrid1.DataSource = view;
}
i have a DataTable that has a column ("Profit"). What i want is to get the Sum of all the values in this table. I tried to do this in the following manner...
DataTable dsTemp = new DataTable();
dsTemp.Columns.Add("Profit");
DataRow dr = null;
dr = dsTemp.NewRow();
dr["Profit"] = 100;
dsTemp.Rows.Add(dr);
dr = dsTemp.NewRow();
dr["Profit"] = 200;
dsTemp.Rows.Add(dr);
DataView dvTotal = dsTemp.DefaultView;
dvTotal.RowFilter = " SUM ( Profit ) ";
DataTable dt = dvTotal.ToTable();
But i get an error while applying the filter...
how can i get the Sum of the Profit column in a variable
thank you...
Set the datacolumn to a numeric type (int, decimal, whatever):
DataColumn col = new DataColumn("Profit", typeof(int));
dsTemp.Columns.Add(col);
Use Compute:
int total = dsTemp.Compute("Sum(Profit)", "");
Note that aggregation is not a type of filter, which is the main problem with the approach you are tyring.
I used the DataTable's Compute method as suggested, and it works fine. I apply the same filtering as I use for the DataView (which is used to display the sorted and filtered data in the data grid) together with an aggregate function.
string SingleVocheridSale ="1";
DataView view = new DataView(dt);
view.RowFilter = string.Format("VOCHID='" + SingleVocheridSale + "'");
dataGridview1.DataSource = view;
object sumObject;
sumObject = dt.Compute("Sum(NETAMT)", view.RowFilter);
lable1.Text = sumObject.ToString();