frankiehf
frankiehf

Reputation: 180

c# add row to a datagrid combobox

i've got a simple datagrid,with a combobox inside:

<DataGrid AutoGenerateColumns="False" CanUserSortColumns="False" x:Name="DataGridassemble" ItemsSource="{Binding Cicli_ops}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Prodotto" x:Name="tipo_prodotto" ItemsSource="{Binding}" SelectedItemBinding="{Binding tipo_prodotto}" Width="150"></DataGridComboBoxColumn></DataGrid.Columns>

the combobox has 4 values, populated by a list:

public ObservableCollection<Cicli_operazioni> Cicli_ops { get; set; }

//code etc

lista_prodotti.Add("cerchio");
lista_prodotti.Add("triangolo");
lista_prodotti.Add("quadrato");
lista_prodotti.Add("stella");
tipo_prodotto.ItemsSource = lista_prodotti;



//Cicli_operazioni class:
public class Cicli_operazioni
{
    public string Tipo_prodotto {get;set;}
}

everything works, the combobox is binded properly, the value is showed etc. But i wanted to add a few default lines in the datagrid, showing a few rows to make it easier for the user. So i tried:

Cicli_ops.Add(Tipo_prodotto= "cerchio");

but the element is a combobox so it gives me an error saying i cant convert a string into a datagridcomboboxcolumn, and i really dont know how to tell it to select an item out of it.

Upvotes: 2

Views: 602

Answers (1)

frankiehf
frankiehf

Reputation: 180

solved by myself,sorry.

Cicli_ops.Add(new Cicli_operazioni { Tipo_prodotto="cerchio" }); 

with Tipo_prodotto being a string,and the xaml becomes

<DataGridComboBoxColumn Header="Prodotto" x:Name="tipo_prodotto" ItemsSource="{Binding}" SelectedItemBinding="{Binding Tipo_prodotto}"

Upvotes: 1

Related Questions