Xamarin grid, column and row amounts - xamarin.forms

Hi im relatively new to c# code and i was wondering if there is any way to get the amount of columns and rows in a grid and store that amount in a variable
Something like:
var columnamount = grid.columnamount;
But i could not find anything that works
Thanks

You can use the following code to get a count of the columns and rows directly via the ColumnDefinitions and RowDefinitions properties. No need to enumerate the children of the grid because you may not have views in every column/row.
var columnCount = grid.ColumnDefintions.Count;
var rowCount = grid.RowDefinitions.Count;

For reference the documentation.
You might be able to do it this way, purely based on what I see in the docs:
var countColumns = grid.Children.Where( c => c.Column).Max();
var countRows = grid.Children.Where( c => c.Row).Max();
But I'm not sure if you can access Row anf Column properties on the child element.

This is not the best way to check, I guess, but it's working (same thing for columns):
EDIT: nope, for columns it doesn't work
int GetRowsCount(Grid grid)
{
var item = grid.Children.FirstOrDefault();
return item != null ? Grid.GetRow(item) + 1 : 0;
}

Related

QML - How to use GridView::itemAt(real x, real y)?

I can get item of Repeater like this:
var item = repeater.itemAt(index);
But how can I get item of GridView? I saw itemAt function. But it is different from itemAt of Repeater. It wants x and y coordinates but we can use index in GridView like Repeater. How can I access the item?
Edit:
gridView.currentIndex = index;
var a = gridView.currentItem;
gridView.currentIndex = lastIndex;
var b = gridView.currentItem;
if (a.imageSource === b.imageSource) console.log('Equals!');
I'm always seeing Equals! output. But they (images) aren't equals (a has 1.png, b has 2.png).
It seems there is no build in function for that, but you may try this approach:
gridView.currentIndex = index; // your index
var item = gridView.currentItem;
// reset the current index to the previous value if you have to
Maybe this will also work instead (not tested):
var item = gridView.children[index];
That is more like a work around but should work in your case.

Hide columns in a GridView when new columns are added

Have landed into a scenario.
I have a gridview with 22 static columns (few are BoundFields, few are TemplateFields).
We have a scenario in our project that we need to order the GridView according to the columns selected. The order of the columns selected is provided from the UI.
For ex: We have Doc1 to Doc23 as the columns.
Now, from the functionality provided in the UI, I am passing some 4 columns say Doc2,Doc4,Doc5,Doc7.
Now I want that only these 4 columns should be present in my grid as a final output.
Have tried some code, but it doesn't seem to work.
Below is my code:
public int GridColumnOrdering(string columnList)
{
string[] test = columnList.Split(';');
var docCatColumn = gridResultSet.Columns[0];
var docTypeColumn = gridResultSet.Columns[1];
int columnCount = 0;
int testCount = 0;
for (int i = 0; i < test.Count(); i++)
{
if (test[i] == "Doc2")
{
gridResultSet.Columns.Insert(i , docCatColumn);
columnCount++;
}
if (test[i] == "Doc3")
{
gridResultSet.Columns.Insert(i , docTypeColumn);
columnCount++;
}
}
gridResultSet.Columns[2].Visible = false;
gridResultSet.Columns[3].Visible = false;
}
columnList is a parameter passed which has values such as Doc2;Doc3.
My idea is that I get the static columns which resemble the column gotten from the UI, change its position to the very next position, and then hide that static column. In this way, we actually have 2 columns by the same name, but I am trying to hide the static one and display the dynamic one.
I know it sounds weird and hectic, but this is what came to my mind.
Now the problem is that if I try to change the visibility of the static column, the visibility of the dynamic one also changes.
Can experts help on this issue or point out to some easy method in this regards??
Regards
Anurag

how to select a row in a kendogrid using a string as parameter

I need help about select a row in a kendoGrid.
I have a simple kendoGrid with selection enabled, and when i click on a button in a webpage, i have to use a string (for example "cod001") for selecting a row in my kendogrid by a column....
for example:
var grid = $("#grid").data("kendoGrid");
grid.select("??????????");//here i sould select a row where the unique value is "cod001" in a defined column
hope someone can help me.
thanks in advance.
I find an alternative solution, without each functions...
i'll post my solution, and hope can help somoeone with my same issues!!!
var g = $("#grid").data("kendoGrid");
var selectedRow = g.select();
var index = selectedRow.index();
... and then...
var ddl = $("#grid").data("kendoGrid");
ddl.select("tr:eq(" + index + ")");
you can make a loop on each line of your grid to check what the column your looking for is and then select it.
var linesToSelect = [];
$.each($('.k-grid-content tbody').children(), function(index, line){
// column is the column's value you want to test
if ($("#grid").data("kendoGrid").dataItem(line).column == "cod001")
linesToSelect.push(line);
});
$("#grid").data("kendoGrid").select(linesToSelect);
This is not a perfect solution since you do a loop on every rows of your grid, but it should help until you find a better solution!

Flex : AdvancedDataGrid columns dataProvider?

I have a AdvancedDataGrid, defined as:
<mx:AdvancedDataGrid id="mainGrid" width="200" height="200" designViewDataType="flat" >
<mx:columns>
</mx:columns>
</mx:AdvancedDataGrid>
I am adding dynamically the columns to the grid, and the main question is:
How to setup an array or vector as dataprovider to each Column?
P.S.
this function I am using to fill up the data:
private function buildUpTheGrid() : void {
masterColumns = new Array();
masterData = new Array();
var tempColumn : AdvancedDataGridColumn;
for( var iY : int = 0; iY < columsCount; iY++ )
{
masterData.push( new Array() );
tempColumn = new AdvancedDataGridColumn();
tempColumn.width = 20;
for( var iX : int = 0; iX < rowsCount; iX++ )
masterData[ iY ].push( iX );
// tempColumn.dataField = ???
masterColumns.push( tempColumn );
}
mainGrid.columns = masterColumns;
mainGrid.validateNow();
}
Short answer to your question "How to setup an array or vector as dataprovider to each Column ?": You cannot.
The data 'views' in flex are all row based, not column based. So, for this to work, you'll need to transform your columns into rows. It's a fairly simple algorithm that I'm sure you can figure out.
As for adding the columns dynamically, you just need to add the columns to an array and then set that array in the 'columns' property of the grid. You'll need to set a 'dataField' property on the column for it to know which data property to display from the row data you've just transformed.
How to setup an array or vector as dataprovider to each Column ?
In the architecture of a DataGrid, and AdvancedDataGrid individual columns do not have their own, separate, dataProviders. To set the dataProvider on the component, you can do something like this:
<mx:AdvancedDataGrid id="mainGrid" dataPRovider="{myDataProvider}" >
in ActionScript you can set the dataProvider like this:
mainGrid.dataProvider = myDataProvider
If you have data coming from a myriad of different sources that you need to display in a single DataGrid, you should try to combine that data into a single dataProvider; using some common element, such as a database primary key value. Creating a single object to contain each separate element should work. Conceptually something like this:
public class myCombinedObject{
public var commonElement : int;
public var fromDataProvider1 : myCustomDataClass1;
public var fromDataProvider2 : myCustomDataClass2;
}
From there just write itemRenderers for each column to show the relevant data.
Usually we will set the dataprovider to the Datagrid. And in the columns we can set the datafield, which will take the property from the objects of the dataprovider to be displayed in the particular column. Are you aiming to make the columns coming from different arrays? In that case it will be a tricky one.
I've used an Array of Arrays as a dataProvider.
You can just set the dataField property to the index of the "sub Array".
For example:
tempColumn.dataField = iY.toString();
This works, (it fills the column with every value from the the sub array [iY]), but I can't recommend doing this because if the order of your data in the sub arrays changes
it could be become difficult to debug...

Is swapping the columns and rows of a flex datagrid possible?

I have a 1 row, many column flex datagrid. I would like to turn the dataGrid on its side, so that the column headers become a single column running down and v.v.
Is there a way to do that in the DataGrid?
Or am I stuck manipulating the data presented to the grid? If so whats your recommendation?
The main idea here is I have an object like:
x=y
b=u
o=p
u=e
w=p
And I'd like a control that is visually similar to that. Currently the datagrid displays the object as:
x b o u w
y u p e p
Which is too horizontal for my case. Thx
I presume that you want to convert your columns in to a single column
this can be done by getting all the columns and put the in array as provide it as a dataprovider.
DataGrid.columns
will return the columns.
and you can do some think like this to create columns.
public function createColumns():Array{
var advancedDataGridColumn:AdvancedDataGridColumn;
var i:int;
var columnsArray:Array = new Array();
for(i=0;i<columns.length;i++){
advancedDataGridColumn=new AdvancedDataGridColumn();
advancedDataGridColumn.headerText=columns[i].dispheader.toString();
advancedDataGridColumn.dataField="#"+columns[i].name.toString();
advancedDataGridColumn.itemRenderer=new ClassFactory(Styler);
if(columns[i].descending!=undefined ){
if(columns[i].descending.toString()=="true")
sortField = new SortField("#"+columns[i].name.toString(),false,true,null);
else
sortField = new SortField("#"+columns[i].name.toString(),false,false,null);
}
if(advancedDataGridColumn.headerText == Constants.price||
advancedDataGridColumn.headerText == Constants.quantity||
advancedDataGridColumn.headerText == Constants.askPrice||
advancedDataGridColumn.headerText == Constants.bidPrice||
advancedDataGridColumn.headerText == Constants.netAmount||
advancedDataGridColumn.headerText == Constants.interestAmount||
advancedDataGridColumn.headerText == Constants.principalAmount||
advancedDataGridColumn.headerText == Constants.accruedInterestAmount){
var currencyFormattor:CurrencyFormatter = new CurrencyFormatter();
currencyFormattor.useThousandsSeparator=true;
currencyFormattor.currencySymbol="";
currencyFormattor.thousandsSeparatorFrom=",";
currencyFormattor.thousandsSeparatorTo=",";
advancedDataGridColumn.formatter=currencyFormattor;
}
columnsArray.push(advancedDataGridColumn);
}
return columnsArray;
}
sorry i just copied the code but i think it will help you.
Set the DataGrid to have only 2 columns and transform the original dataset to an array collection of {propName, propValue}.
Say you have:
var originalDataSet : ArrayCollection;
var dataSet : ArrayCollection;
var columnSet : ArrayCollection;
Once you have the original values, you'll do something like:
dataSet = new ArrayCollection();
for (var i : int; i < originalDataSet.length; i++)
{
dataSet.addItem({name : columnSet.getItemAt(i), value : originalDataSet.getItemAt(i)});
}
myDataGrid.dataProvider = dataSet;//set the data provider of the grid to the transformed data set.
To clarify:
{name : columnSet.getItemAt(i), value : originalDataSet.getItemAt(i)}
This creates a new instance of type Object and assigns the name and value dynamic properties to their respective values. Instead of this you might want to define your own class with bindable properties. Note that the property names are just for this example because I don't know what you're working with actually.
The data grid at that point should have two columns defined by you, with their dataField properties set accordingly. Also, this example assumes columnSet collection contains the "horizontal columns" that you want displayed vertically. If you can obtain these based on the values in the originalDataset, you might not even need columnSet.

Resources