DataTable.RowFilter using List results? - linq

I need to keep the original DataTable and filter it based on list values. I've tried to hide rows that don't match up to the list but it doesn't work instantly. I don't want to copy the list to another dataTable and bind my dataGridView to is cause i have too much tied up to the original DataTable. Filtering only works for a few conditions but the list is holds only the values that are needed. Is there a LINQ solution to this? C# only please. This is how I got my list. There is another dataGridView that holds primary key for the datatable that I need to filter. It's key is held in a comboBox.
DataTable MainTable = MainDataSet.Tables["MyTable"];
DataTable lookupTable = lookupDataSet.Tables["MyLookupTable"];
var List = (from x in lookupTable.AsEnumerable()
where x.Field<string>("kAutoInc") == comboBox.SelectedValue.ToString()
select x.Field<int>("Pct")).ToList();
var a = MainTable.AsEnumerable().Where(r =>
List .Any(id => id == r.Field<int>("Pct")));
This gives me values of int 30, 40, 50, 60...
The DataGridView has a matching column "Pct" that I need to filter rows that only contain these values. I know it seems easy but I just can't seem to get it to work.

Please use DataView instead.
dataview dv=new dataview(datatable);
dv.filter=string.format("column1={0}","value1");
Like that.

Related

QTP UFT How to find a row number and column number? Angular JS

When I spy the table it shows just browser - Page - WebElement and also UI is developed in Angular JS.
I there any way to find row number and column number? By the way I am using UFT/QTP
From the source-code's image which you have attached, it is quite evident that the webElement corresponding to the rows have class = "ui-grid-row ng-scope". So, you can make use of descriptive programming. I assume that you already have added the object Browser(...).Page(...) to your OR.
Set rowDesc = Description.Create
rowDesc("Class Name").value = "WebElement"
rowDesc("Class").value = "ui-grid-row ng-scope"
Set objRows = Browser(...).Page(...).ChildObjects(rowDesc)
rowCount = objRows.Count 'This variable should now contain the total number of rows"
Now, this is just an idea which you can give a try. If it works for you, you can further enhance it to get the column count. If there is no way you can get the column count, then you can get the total cell count using the same method. In that case, you just need to change the value of property "class" to the one mentioned in the Object Spy Image("ui-grid-cell-contents ng-binding ng-scope"). Now you have Row Count and Cell Count. To get the column count, you can divide cell count by row count.(Again, this will give you correct answer ONLY IF there are same number of columns for each of tbe rows).

join two tables in linq with special conditions

I hope one can help me, I am new in linq,
I have 2 tables name tblcart and tblorderdetail:
I just show some fields in these two tables to show whats my problem:
tblCart:
ID,
CartID,
Barcode,
and tblOrderDetail:
ID,
CartID,
IsCompleted
Barcode
when someone save an order, before he confirms his request,one row temporarily enter into the tblCart,
then if he or she confirms his request another row will be inserted into the tblOrderDetail ,
Now I wanna not to show the rows that is inserted into tblOrderDetailed(showing just temporarily rows which there is in tblCart),
In another words, if there is rows in tblCart with cartID=1 and at the same time there is the same row with CartID= 1 in tblOrderDetail, then I dont want that Row.
All in all, Just the rows that there isnt in tblOrderDetail, and the field to realize this is CartID,
I should mention that I make Iscompleted=true, and with that either we can exclude the rows we do not want,
I did this:
var cartItems = context.tblCarts
.Join(context.tblSiteOrderDetails,
w => w.CartID,
orderDetail => orderDetail.cartID,
(w,orderDetail) => new{w,orderDetail})
.Where(a=>a.orderDetail.cartID !=a.w.CartID)
.ToList()
however it doesn't work.
one example:
tblCart:
ID=1
CartID=1213
Barcode=4567
ID=2
CartID=1214
Barcode=4567
ID=3
CartID=1215
Barcode=6576
tblOrderDetail:
ID=2
CartID=1213
Barcode=4567
IsCompleted=true
with these data it should just show the last two Row in tblCart, I mean
ID=2
CartID=1214
Barcode=4567
ID=3
CartID=1215
Barcode=6576
This sounds like a case for WHERE NOT EXISTS in sql.
roughly translated this should be something like this in LINQ:
var cartItems = context.tblCarts.Where(crt => !context.tblSiteOrderDetails.Any(od => od.CartID == crt.cartID));
If you have a navigation property on cart to reference details (I'll assume it's called Details), then:
var results=context.tblCarts.Where(c=>!c.Details.Any(d=>d.IsCompleted));

Dynamic multicolumn sorting using fnPreDrawCallback

I have a table with 3 columns and when I sort column 1 or 2 then I would like to sort column 3 by descending.
I'm using fnPreDrawCallback (I tried to use fnDrawCallback too).
Here is my code:
"fnPreDrawCallback": function (oSettings) {
var column = oSettings.aaSorting[0][0];
if (column !== 3) {
oSettings.aaSorting.push([3, "desc"]);
}
}
This code looks fine, but, for example, when I sort column 1, the column 3 isn't sorted and, more awkward, when I try to sort at a 2nd time on column 1, the sort result is always ascending.
How can I achieve this?
The data has already been "sorted" by the time the preDrawCallback is fired, so changing aaSorting in here won't change the outcome of the table.
You can verify by checking oSettings. There is aiDisplay and aiDisplayMaster properties. aiDisplay is an ordered array of the elements that changes as they are sorted from clicks on a th. aiDisplayMaster is the original order that the data came back from the server or was existing on the page.
I would recommend manually calling fnSort from the click event of your th elements. To do this you will have to unbind the click event that datatables installs.
Create a function like this and call it from fnInitComplete so it only runs after datatables has been setup.
function unbindDTSorting() {
//unbind sort event, prevent sorting when header is clicked
$j('[id$=table] th').unbind('click.DT');
//create your own click handler for the header
$j('[id$=table] th').click(function(e) {
var oTable = $j('[id$=table]').dataTable();
//here is where you can do all of your if/else logic to create the desired multidimensional sorting
if(this.cellIndex == 3){
oTable.fnSort( [ [2, 'asc'],[3,'desc' ]] );
}
});
}

Linq comparing DataTable with List<String>

I have a string list (List) that contains delimited fields. An example would be:
List[0] = "7/1/2013,ABC,123456"
List[1] = "7/2/2013,DEF,234567"
I also have a DataTable where a record either will or will not contain the the values from the 2nd and 3rd column in the String List:
Example
Row[0][0]="ABC" <-----String
Row[0][1]=123456 <-----Int32
What I want to do is find any records (via Linq) in the DataTable that DO NOT have corresponding values in the String List.
I've been googling for a while, and can't quite find the right way to do this with Linq...can anyone help?
This code snippet should give you an enumeration of the indices that do not have the appropriate DataTable values:
var correspondingRecords =
from index in Enumerable.Range(0, List.Count)
let items = List[index].Split(',')
where !(item[1] == Row[index][0] && item[2] == Row[index][1])
select index;
The basic idea is to iterate over the indices in order to make sure that you're comparing the appropriate rows and list items to one another. Once you do that, it's simple enough to parse the list item and make the appropriate comparisons.

KendoUI Grid Create Columns Programmatically

I have an app with a KendoUI DropDownList that is populated with a list of tables from a database and a grid that is initially set with a columns atrribute of an empty array:
...
columns: []
...
The intent is to select a table from the list, send the table name to the server and have the server return JSON data containing the column names and the data from a "SELECT * FROM table" query. The data comes back as expected and the first time through I can use it as follows where "self" is just a reference to the grid in my view/model:
self.columns.removeAll();
for (var i = 0; i < joOutput["Cols"].length; i++) {
var col = { title: joOutput["Cols"][i], field: joOutput["Cols"][i] };
self.columns.push(col);
}
After extracting my data and assigning it to the grids datasource, the grid displays correctly with the correct column headers and data. However, when I select another table from the list and receive data from the server, the grid display does not update, even though it seems the grid columns have been updated though the execution of the above code. The end the result on screen is the column headers are the names of the columns from the first grid and the number of empty lines from the rows returned from the second query.
This dynamic manipulation of columns seems to be very difficult to do as seen by the forum post at http://www.kendoui.com/forums/ui/grid/dynamically-add-new-column.aspx but that post is over a year old now and I would've hoped some progress would've been made on this now, especially in light on the recent webcast on March 20 for the new release. So I guess the question remains: Is what I'm after even possible or am I SOL? Thanks.
You cannot dynamically change the columns of the grid after initialization. You can however create a new grid instance. Don't forget to call the destroy method of the old grid.

Resources