How to add different items in a ListView - c#

I have a ListView of this type:
<ListView x:Name="LView">
<ListView.View>
<GridView x:Name="GView"></GridView>
</ListView.View>
</ListView>
Initially I don't know how many columns there will be in my GridView, and I don't know what is the type of a single cell.
I need to have the possibility to add at run-time new columns and especially new rows whose cells contain different objects.
ListView has the property Items, so if I have a single column of string and Iwant to add a row i write:
LView.Items.Add("my string");
Now i want to add a new column:
GView.Columns.Add(new GridViewColumn());
A new column is added but the second cell of the first row contains the string "my string", why? I don't want this behavior but a new empty cell.
And especially if I want to add a new row with the first cell as String and the second cell as CheckBox what I have to do?
I tried:
LView.Items.Add("my second string", new CheckBox());
But, obviously, it does not work.
I tried to add a new Object:
public class obj
{
public string MyString{ get; set; }
public CheckBox MyCBox { get; set; }
}
LView.Items.Add(new obj());
But it dows not work because it display a string that contains (Namespace "+ obj"), and I don't know how many columns there will be, so I can't know how the obj class have to be initially.
What I have to do to solve my problems? Thanks.

I think you mix data items with ui elements
LView.Items.Add("my string");
will be presented as a textelement using default template
If you havent defined any data templates and want to explicitly add ui elements try this instead:
LView.Items.Add(new TextBlock { Text = "My string"});
However, I think GridView is suppoused to be populated with data (along with power of data bindings), not explicitly ui elements. Column Template should define how data is presented (in your case an checkbox or textelement).
See example http://www.c-sharpcorner.com/uploadfile/mahesh/gridview-in-wpf/

Related

WPF drop-down list for auto generated column in datagrid

How to make a drop-down list when you click on an element of a specific table column in which you can select an element for this cell? Column is auto generated.
A Combobox in xaml/wpf is used like this:
<ComboBox x:Name="some Name" SelectionChanged="comboboxChanged">
<ComboBoxItem>The Content of your Combobox</ComboBoxItem>
</Combobox>
ComboBoxItems are essentially the dropdown part. You can add as many as you want.
In your back end (c#) you can get the selected Value as soon as the "SelectionChanged"-event is triggered. The code for getting the selected Value can be done multiple ways. Example:
private void comboboxChanged(object sender, SelectionChangedEventArgs e){
string comboboxvalue = comboboxname.Text;
//Then set associated textblock or label
labelname.Content = comboboxvalue;
}
The code above would be static though. Dynamically generating these elements could look like this for instance.
When auto-generating, using an inline function for the event is easy.
for (int i = 0; i < 10; i++){
ComboBox comboboxname = new ComboBox();
comboboxname.SelectionChanged += (ss,ee) { string comboBoxValue = comboboxname.Text; labelname.Content = comboBoxValue;}
}
Labelname being the name of the Label you want to set. In that loop you will need to implement a way of giving each box a unique name and getting the name of the associated label in there as well. That you will have to figure out on your own as i do not know how and what exactly is generated and what is static.
You will also need to add your dynamically created combobox to your listpanel or grid or whatever you are using. This works like this:
listpanelname.Children.Add(comboboxname);
Just add that to the "for" loop.

Adding different List<string> to DataGridViewComboBoxCell in DataGridView

I am having trouble implementing DataGridViewComboBoxCell inside the DataGridView control. What I want to do is to display in every row different set of choices – so each ComboBox has a different list to display. I am using Visual Studio designer.
I have assigned to myDataGridView.DataSource the BindingSource (variable name is bindingSourceComponenetFileInfo) to which DataSource (bindingSourceComponentFileInfo.DataSource) I have assigned my data called EclipseComponentFileInfo which is defined like this:
namespace EclipseSuiteMakerWindowsForms
{
class EclipseComponentFileInfo
{
public EclipseComponentFileInfo()
{ }
private string path;
private string pathName;
private string[] fileName;
public string[] FileName { get => fileName; set => fileName = value; }
public string Path { get => path; set => path = value; }
}
}
The problem I am having is the ‘filename’ field – because it is not a plain string but an array of strings, it does not appear in Visual Designer under DataGridView/Columns/SelectedColumns (only PathName column is available). I used Visual Designer to create for my DataGridView an ‘Unbound column’ (which I still don’t quite understand what it is and how it works – no docs) specifying as ‘Name: FileName/Header text: FileNameHeader /Type:DataGridViewBoxColumn’ and then when I run my app, my ComboBox column in every row came up as an empty string – I kind of expected that.
So next I assigned (using Visual Designer) to the DataSource of the unbound column created above (DataGridViewBoxColumn.DataSource) variable (of type BindingSource) called fileNameBindingSource. Next I have assigned to ‘fileNameBindingSource .DataSource’, the earlier mentioned bindingSourceComponenetFileInfo (of type EclipseComponentFileInfo) with the DataPropertyName called FileName.
Then all rows’s ComboBox columns are filled with the name that appears in the first row for some reason.
My question is:
Can I have rows of ComboBox columns filled with different set of items or every ComboBox has to have the same set of items (e.g first row has ComboBox with array of one item “Item1”, but the second row has a comboBox with the array of two items “Item1/Item2”).
The list of items doesn't come from the grid's data source. Generally speaking, you set a data source for the column and each cell inherits that. If you want a different list for each cell though, you need to set a different data source for each cell.

Empty WPF DataGrid should be filled by user

I am using DataGrid view in WPF which is inserted by the following XAML Code
<DataGrid x:Name="DVA_Zyklus_DataGrid" IsManipulationEnabled="True"/>
</Grid>
Now I add a column in my C# Code
DataGridTextColumn c0 = new DataGridTextColumn();
c0.Header = "Test";
c0.Binding = new Binding("TimeToRamp");
c0.Width = 110;
DVA_Zyklus_DataGrid.Columns.Add(c0);
and a line
DVA_Zyklus_DataGrid.Items.Add(new DVA_Zyklus_DataGrid_Single_Pump_Item() { TimeToRamp = 0.0});
where
public class DVA_Zyklus_DataGrid_Single_Pump_Item
{
public double TimeToRamp { get; set; }
}
This works fine and results in one cell with value 0. Now the user should be able to change this value. How ever Selecting the cell and pressing any key results in crashing of the programm.
Exception Type System.InvalidOperationException, Additional Information EditItem is not allowed.
Any idea of howto to allow the user to do this?
You should set the ItemsSource of the DataGrid, instead of inserting items to the Items property:
var list = new ObservableCollection<DVA_Zyklus_DataGrid_Single_Pump_Item>();
list.Add(new DVA_Zyklus_DataGrid_Single_Pump_Item() { TimeToRamp = 0.0 });
dg.ItemsSource = list;
I think its better to follow MVVM approach to fill the DataGrid. There are lots of posts regarding it, such as this one.

How to save CheckBoxRowSelect in Devexpress GridView to class object

I've simple class called "Order".
class Order {
public string ID { get;set;}
public string Something {get;set;}
.... more fields...
}
I then assign list of orders into DataSource of GridControl.
List <Order> ListOfOrders = new Order();
gridControl.DataSource = ListOfOrders;
I've added a CheckBoxRowSelect option in GUI so that user can choose rows by selecting the checkbox. What value do I need to add to class Order so that any checkbox change in that GridControl gets updated in instant in it's own field so that the ListOfOrders is always up to date and has that checkbox value stored so it can be processed?
I've made similar thing with ComboBox inside one of the columns and it seems to work instantly without any additional change on my part.
var columnKurier = view.Columns.AddField("Courier");
columnKurier.ColumnEdit = riCombo;
columnKurier.VisibleIndex = 0;
columnKurier.OptionsColumn.AllowFocus = true;
I simply had to add Courier field into Order class and that's it. But i don't know which "field name" should it be for checkbox and whether it will get the same behaviour as for the ComboBox.
Add in your object order :
public Boolean Mark{get;set;}
and in your your form contructor :
InitializeComponent();
new GridCheckMarksSelection(gridControl);
i have atach a class GridCheckMarksSelection you must add it in your project.
MarkClass
I hop that you want
You can not add the option CheckBoxRowSelect to GridControl, and just use a bool field in your Class Order, it should be displayed as checkbox in GridControl.

C#: Listbox: How to add data in addition to text for each item

Consider the following text as two lists(separated with ':'), as you can see the second rows are always unique but items in the first row can be repetitive;
Book:m234
Clover:h67
Pencil:a12
Book:x67
I want to populate a listbox with items in the first column(Book, Clover, ...) but the problem is that when I'm going to retrieve the selected item in the listbox, I can't be sure about it's respective value in second column. (for example in case of 'Book');
NOTE: I'm not looking for any workaround for solving this problem because there are many. What I want to know is that:
Is it possible to pass and object to ListBox.Items.Add() in a way that the object carries two values(each value/property for each column) and in time of getting the selected item, we would have an object with the two values(maybe as the properties of the object)?
Is such a thing possible in C#? (.NET 4.5)
You can pass objects that pair data with names to your ListBox, and control what gets displayed and what gets returned back to you by using DisplayMember and ValueMember:
class ListBoxItem {
public string DisplayName {get;set;}
public string Identifier {get;set;}
}
...
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Book", Identifier = "m234"
});
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Clover", Identifier = "h67"
});
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Pencil", Identifier = "a12"
});
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Book", Identifier = "x67"
});
ListBox.DisplayMember = "DisplayName";
ListBox.ValueMember = "Identifier";
Now your list box displays the same list of strings, but the values returned for end-user selections would be different.

Categories

Resources