List view, display text vertically - c#

Is there any way to get a list view to display vertically? Rigth now for my list view, the view is "List", but if there are too many entries, then you have to scroll to the right to view the rest, I just want to be able to scroll down to see the rest.
I tried all the other options, but they don't seem to work.
Any ideas?

I think to get the effect you want, you need to set the view to "Details", then add a single column (either through the designer or in code).
This is what I do in mine:
SelectionList.View = View.Details;
SelectionList.HeaderStyle = ColumnHeaderStyle.None;
SelectionList.Columns.Add(new ColumnHeader { Width = SelectionList.ClientSize.Width - SystemInformation.VerticalScrollBarWidth });
Then to add items:
foreach (var item in _collectionOfSomething)
{
SelectionList.Items.Add(new ListViewItem(item.ItemName) {Tag = item.Ident});
}

Related

See no Items after adding them

My Listview show nothing after adding Items from a List into it. Why?
My List is not empty And my program goes into this loop.
So Is the code wrong for adding items into an listview? Because I saw many chatrooms and also from Microsoft that I can add them like this.
I tried it also with this code:listView1.Items.Add(new ListViewItem { ImageKey = "Person", Text = database1[counter1].name });
Here a picture of my imglist, and the list is chosen at thelistview:
enter image description here
You can't add items if there are no columns, or you can but they wont show up.
Have you added some columns to your listView?
//Adds needed columns
this.listView1.Columns.Add("<column_name>", 50);
If you have one column, you can simply add items by:
ListViewItem itm = new ListViewItem("my_item");
listView1.Items.Add(itm);
If you have multiple columns instead of a string, you can do the same but with a string array where the array size equals to the number of columns.
string[] items = new string[listView1.Columns.Count];
Try this in your code too, in my code only works with this:
this.listView1.View = View.Details;

WPF DataGrid editable cells with Items put directively

So, I have a DataGrid, that I want to manipulate programmatically a lot.
string[] values = new string[something.Count];
for (int i = 0; i < somethingElse.Count; i++)
{
if (condition)
values[i] = Data[i].ToString();
else
values[i] = "";
}
var style = new System.Windows.Style(typeof(DataGridRowHeader));
style.Setters.Add(new Setter(DataGridRowHeader.ContentProperty, Data[0].ToString()));
MyGrid.Items.Add(new DataGridRow()
{
HeaderStyle = style,
Item = values
});
This I do in a loop and I am able to fill in my grid with all the data I need.
Later, I am able to access cells, edit them, take their values, whatever I want and need.
However, when user wants to use the grid as you would in MS Excel, the cells are not editable.
So I went the other way and created a :
ObservableCollection<ObservableCollection<string>> gridData = new ObservableCollection<ObservableCollection<string>>();
//*** ... *** the gridData is filled in the same way, you can imagine
MyGrid.ItemsSource = gridData;
This does fill in the data perfectly the same way, more than that, the data are now editable.
But my custom row headers disappeared.
I need them, also, I do not think I want to use binding for row header values.
Can the first approach be somehow modified to still be editable, but rows with data being filled the very same way?
I eventually solved this combining both the abovementioned approaches.
First, I generate ObservableCollection<ObservableCollection<string>> dataCollection, fill it with data.
Next I generate an ObservableCollection<DataGridRow> rowCollection collection.
In my declarative part of the loop from the first approach, that you can see in the question, I do this
rowCollection.Add(new DataGridRow()
{
HeaderStyle = style,
Item = dataCollection[i] //for the corresponding iteration element
});
This all ends up when setting
MyGrid.ItemsSource = rowCollection;
Till now (very little time of testing), this seems to be what I was looking for, hope it might help someone else aswell.

changes to the children of the grid dynamically

I'm trying to implement a search suggestion feature using the grid view of Xamarin forms. I was wondering if there is anyway to change the children of the grid dynamically according to the text get entered in the search box.
grid.Children.Add(new Label
{
Text = x.Text,
TextColor = Color.White,
BackgroundColor = Color.Blue
}, 0, x.Id);
If you want to use Grid , then you should specify row ands colums, otherwise all the texts will overlaps each other. Instead of Grid, you should use StackLayout.
hintSL = new StackLayout(){
HorizontalOptions= LayoutOptions.Fill,
//Orientation = Vertical or Horizontal (however you want)
};
//if you want to add hintsSl into your grid
Grid.SetRow(hintsl, 4);
Grid.SetColumn(hintsl, 4);
searchText.Changed+=(s,e)=>{
hintSL.Children.Clear();
foreach(var hint in YourHintsList)
hintsl.Children.Add(new Label(){Text=hint, and other properties});
};
Here are two ways to do this:
The first is to use binding. Bind the text field of the label to a bindable property that changes according to the user's entry.
The second is to make the label a member of whatever class holds the grid, rather than anonymous. Then you can catch the event (eg OnEntryChanged) generated when the user enters the text and dynamically set the label in the code.

Search within Datagrid and scroll & select newly added item

I'm using the Telerik Datagrid control to display 100's of records. In my app the user selects a they want to edit and I use a form to allow the user to update the record.
Likewise, if they wish to add a new record they click 'Add' and a blank version of the form appears.
I'm not binding direct to a data source. I'm providing the data to the grid by setting the Itemsource to my List of records.
When the user has edited a record I can scroll and select the edited record quite easily:
//find row index of selected item
var lastRowUpdated = RadGridAssetTable.Items.IndexOf(this.RadGridAssetTable.SelectedItem);
// move to index following edit
RadGridAssetTable.ScrollIndexIntoView(lastRowUpdated);
RadGridAssetTable.SelectedItem = lastRowUpdated;
However, when I add a new record I cannot figure out how to programmatically scroll and select the newly added grid item.
Effectively I want to search the grid rows for the (hidden) record ID, select the row index in the control and scroll to it
There doesn't appear to be a way of searching data in the grid programmatically, can someone point in the correct direction on how to do this?
Regards
Ok, so I solved like this.
var lastRowUpdated = 0;
var i = 0;
if (_assetsavedData.AssetId == -1)
{
foreach (var rowItem in from object row in RadGridAssetTable.Items select row as AssetLinked)
{
Debug.WriteLine(rowItem.AssetItems.AssetCommonName);
if (rowItem.AssetItems.AssetCommonName.Equals(_assetsavedData.AssetCommonName))
{
lastRowUpdated = i;
Debug.WriteLine("found at " + i);
break;
}
i++;
}
}
else
{
lastRowUpdated = RadGridAssetTable.Items.IndexOf(this.RadGridAssetTable.SelectedItem);
}

Dynamic Grid Measurements in WPF - Rendering Controls

I am working on a Drag and Drop Editor that is creating items and adding them dynamically to a WPF Canvas. For each item, it is creating a dynamic grid and addign that to the canvas. I need layout state information about each one of these grids as it is added so that I know its coordinates on the canvas. The problem I am having is that when I try to access the Height/ActualHeight/RenderedSize information of each of these Grids that I add, it always turns out to be 0. I am assuming that I might need to render the items so that the new state information is registered, but I am not quite sure how to do this. I have seen support through the InvalidateXXXX() methods that are provided, but I am not sure if/or which one I should be using. there is also an UpdateLayout() function, but I am not sure if this is what I need.
Here is the code that I have running through a loop and adding Grids, which represent LineItems in the Canvas.
/*Initialize Grid Layout*/
Grid newGrid = new Grid();
newGrid.MinHeight = 50;
newGrid.Width = PreviewWindow.ActualWidth;
newGrid.Background = Brushes.Beige;
newGrid.ShowGridLines = true;
/*Define Column Definitions*/
List<ColumnDefinition> columns = new List<ColumnDefinition>(fieldItemList.Count);
foreach (ColumnDefinition column in columns)
{
ColumnDefinition labelColumn = new ColumnDefinition();
newGrid.ColumnDefinitions.Add(labelColumn);
newGrid.ColumnDefinitions.Add(column);
}
/*Define Row Definitions*/
RowDefinition row = new RowDefinition();
newGrid.RowDefinitions.Add(row);
int colCount = 0;
for (int i = 0; i < fieldItemList.Count; i++)
{
FieldItem fieldItem = fieldItemList[i];
/*Initialize Example Label*/
Label label = new Label();
label.Content = fieldItem.Label;
Grid.SetRow(label, 0);
Grid.SetColumn(label, colCount);
newGrid.Children.Add(label);
/*Initialize Example Text Box*/
TextBox textBox = new TextBox();
Grid.SetRow(textBox, 0);
Grid.SetColumn(textBox, colCount + 1);
newGrid.Children.Add(textBox);
colCount++;
}
stackPanel.Children.Add(newGrid);
//I need to Access the Height of each of the grids here somehow
lastTop += (int)newGrid.ActualHeight ;
newGrid.InvalidateMeasure();
//Or InvalidateVisual(), or do I perform this on something else?
}
There is reference to a StackPanel, all of these Grids are being added to a stack panel, and they will eventuall be arranged by the coordinates(which I currently cannot grab). The problem is right now it is adding all these items to a list that will need to be sorted by the Top coordinate, but since the Top coordinate is 0, it will not sort correctly.
A few notes:
The StackPanel does not provide size Layout suggestions to its children, so the Grid will need to supply it's own Layout size. In order for this to work, you'll need to set the Row and Column sizes to "Auto" so that they expand to accommodate their contents. Even then, I'm not entirely sure if this works (usually you combine Auto with "*" rows or columns, rather than having all Auto columns, having them expand to a fixed or stretched overall Grid size.) The layout I've proposed here will behave a bit oddly, as it will dynamically resize columns based on the contents of the column text boxes; for instance, if you have no text in any row, you'll get zero width text boxes with only margin and border providing any size.
Once the StackPanel has had its children added, you'll need to call UpdateLayout on the StackPanel. This will cause it to compute the layout information for each of its children and then position them appropriately. This can become expensive if you keep adding grids dynamically, so fair warning.

Categories

Resources