JayJay
JayJay

Reputation: 1068

Populate ComboBox inside xamDataGrid

i have a xamDatagrid set up as follows :

 <igDP:XamDataGrid  DataSource="{Binding}"  x:Name="xamContact" >
                                <igDP:XamDataGrid.FieldLayouts>
                                    <igDP:FieldLayout>
                                        <igDP:Field Name="DepartmentName" Label="Department Name" />
                                        <igDP:Field Name="FirstName" Width="100" Label="First Name" />
                                        <igDP:Field Name="LastName" Label="Last Name" />
                                        <igDP:Field Name="RoleName" Label="Role Name" />
                                       <igDP:ComboBoxField Name="Contactby" Visibility="Visible" ItemsSource="{Binding}"  Label="Contactby"></igDP:ComboBoxField>
                                        <igDP:Field  Name="ContactType" Label="ContactType" Visibility="Collapsed" />
                                   </igDP:FieldLayout>
                                </igDP:XamDataGrid.FieldLayouts>

in the code behind i want populate the xamDataGrid included the ComboBox field"Contactby"(situated in the xamDataGrid) as follows:

 public DataTable GetContTable()
 {

        DataTable tableCont = new DataTable();
        tableCont.Columns.Add("DepartmentName", typeof(string));
        tableCont.Columns.Add("FirstName", typeof(string));
        tableCont.Columns.Add("LastName", typeof(string));
        tableCont.Columns.Add("RoleName", typeof(string));
        tableCont.Columns.Add("Contactby", typeof(object));
        tableCont.Columns.Add("ContactType", typeof(string));
      tableCont.Columns[5].ColumnMapping = MappingType.Hidden;
        return tableCont;
 }

 public void AddRowsCont(DataTable dtcont)
 {

        string[] card = new string[1000];
        for (int i = 0; i < lstContactBy.Items.Count; i++)// in this listbox the number of items is dinamic then can change number each time insert a new record 
        {
            card[i] = lstContactBy.Items[i].ToString();
        }
        dtcont.Rows.Add(txtDepartment.Text, txtFirstName.Text, txtlastName.Text, txtROlename.Text,card  ,"SKYPE"); 

 }

    //here is the button to insert the data in the xamfdatagrid

            AddRowsCont(tableCont);
            xamContact.DataSource = tableCont.DefaultView;

the result is the xamdatagrid can be populate instead the ComboBox "Contactby" is populate only with a message'String[] Array' How can i populate the ComboBox without geting any errors?

Upvotes: 0

Views: 2470

Answers (1)

SamTh3D3v
SamTh3D3v

Reputation: 9944

First i think that it will be much more elegant if you define a Model class and work base on it, let say a class Item:

public class Item
{
    public String DepartmentName { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String RoleName { get; set; }
    public String Contactby { get; set; }
    public String ContactType { get; set; }
}

Secondly Bind your DataGrid DataSource to an ObservableCollection and implement the INotifyChangedInterface, That way you don't need to warry about updating the DataGrid each time an item on the collection or the collection it self has changed, so in your code behind (or ViewModel) :

private ObservableCollection<Item> _listDg = new ObservableCollection<Item>();
    public ObservableCollection<Item> ListDg
    {
        get
        {
            return _listDg;
        }

        set
        {
            if (_listDg == value)
            {
                return;
            }

            _listDg = value;
            OnPropertyChanged();
        }
    }

Now regarding that ComboBoxField inside your DataGrid, the best way to handle it is to :

1. Create another collection that hold the ComboBoxField Items, You can bind that collection as well to another listView so that each time you add an item to the list the collection on the ComboBoxField will be updated as well,

private ObservableCollection<String> _contactByCollection = new ObservableCollection<string>()
    {
        "One","Two","Three"   // you can Bind that collection to a list or add their items programaticlly
    };
    public ObservableCollection<String> ContactByCollection
    {
        get
        {
            return _contactByCollection;
        }

        set
        {
            if (_contactByCollection == value)
            {
                return;
            }

            _contactByCollection = value;
            OnPropertyChanged();
        }
    }

2. Bind that collection to a CollectionViewSource, and use it to set the comboBox ItemSource:

<Window.Resources>
    <CollectionViewSource x:Key="list" Source="{Binding ContactByCollection}"/>
</Window.Resources>

Finally, set the DataGrid DataSource and you page DataContext

<Window 
     ...
   DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <CollectionViewSource x:Key="list" Source="{Binding ContactByCollection}"/>
</Window.Resources>
<Grid>        
    <igDP:XamDataGrid  DataSource="{Binding ListDg}"  x:Name="xamContact" BindToSampleData="True" IsSynchronizedWithCurrentItem="False" IsNestedDataDisplayEnabled="True" >
        <igDP:XamDataGrid.ViewSettings>
            <igDP:GridViewSettings/>
        </igDP:XamDataGrid.ViewSettings>
        <igDP:XamDataGrid.FieldLayouts>
            <igDP:FieldLayout>
                <igDP:Field Name="DepartmentName" Label="Department Name" />
                <igDP:Field Name="FirstName" Width="100" Label="First Name" />
                <igDP:Field Name="LastName" Label="Last Name" />
                <igDP:Field Name="RoleName" Label="Role Name" />
                <igDP:ComboBoxField  Name="Contactby"  ItemsSource="{Binding Source={StaticResource list}}"  Visibility="Visible"  Label="Contact by">                        
                </igDP:ComboBoxField>
                <igDP:Field  Name="ContactType" Label="ContactType" Visibility="Collapsed" />
            </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>
</Grid>

Here a more elaborate example, with the ContactBy Items inside a ListView

        Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <CollectionViewSource x:Key="list" Source="{Binding ContactByCollection}"/>
</Window.Resources>
<Grid> 
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>            
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <Button Click="ButtonBase_OnClick" Content="Add new ContactBy Item"/>
        <ListView ItemsSource="{Binding ContactByCollection}" Margin="3">                
        </ListView>            
    </StackPanel>
    <StackPanel Grid.Column="1">
        <igDP:XamDataGrid   DataSource="{Binding ListDg}"  x:Name="xamContact" BindToSampleData="True" IsSynchronizedWithCurrentItem="False" IsNestedDataDisplayEnabled="True" >
            <igDP:XamDataGrid.ViewSettings>
                <igDP:GridViewSettings/>
            </igDP:XamDataGrid.ViewSettings>
            <igDP:XamDataGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:Field Name="DepartmentName" Label="Department Name" />
                    <igDP:Field Name="FirstName" Width="100" Label="First Name" />
                    <igDP:Field Name="LastName" Label="Last Name" />
                    <igDP:Field Name="RoleName" Label="Role Name" />
                    <igDP:ComboBoxField  Name="Contactby"  ItemsSource="{Binding Source={StaticResource list}}"  Visibility="Visible"  Label="Contact by">
                    </igDP:ComboBoxField>
                    <igDP:Field  Name="ContactType" Label="ContactType" Visibility="Collapsed" />
                </igDP:FieldLayout>
            </igDP:XamDataGrid.FieldLayouts>
        </igDP:XamDataGrid>
        <Button Content="Add an Item To the Grid" Click="AddToGrid_Click"></Button>
    </StackPanel>

</Grid>

and the codebehind

public class Item
{
    public String DepartmentName { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String RoleName { get; set; }
    public String Contactby { get; set; }
    public String ContactType { get; set; }
}
public partial class MainWindow : Window, INotifyPropertyChanged
{       
    private ObservableCollection<String> _contactByCollection = new ObservableCollection<string>()
    {
        "One","Two","Three"   // you can Bind that collection to a list or add their items programaticlly
    };
    public ObservableCollection<String> ContactByCollection
    {
        get
        {
            return _contactByCollection;
        }

        set
        {
            if (_contactByCollection == value)
            {
                return;
            }

            _contactByCollection = value;
            OnPropertyChanged();
        }
    }

    private ObservableCollection<Item> _listDg = new ObservableCollection<Item>();
    public ObservableCollection<Item> ListDg
    {
        get
        {
            return _listDg;
        }

        set
        {
            if (_listDg == value)
            {
                return;
            }

            _listDg = value;
            OnPropertyChanged();
        }
    }
    public MainWindow()
    {
        InitializeComponent();            
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        ContactByCollection.Add("NewContactByItem");
    }

    private void AddToGrid_Click(object sender, RoutedEventArgs e)
    {
        ListDg.Add(new Item()
        {
            FirstName = "FirstName" //Other properties
            ,Contactby = "Three" //..
        });
    }
}

enter image description here

Upvotes: 2

Related Questions