We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Programmatically adding GridComboBoxColumn to the grid

Hi, 

I'm adding data to sfDataGrid in C# code dynamically depending of the datasource. ItemsSouce is DataTable from DataSet and data is displayed.
When I want to add GridComboBoxColumn() then data is not shown


Code;

XAML
 <Syncfusion:SfDataGrid  x:Name="sfDataGrid2"    HorizontalAlignment="Left" Height="159" Margin="53,440,0,0" VerticalAlignment="Top" Width="922" AllowFiltering="True"  RowHeaderWidth="15" AddNewRowPosition="FixedTop" FooterRowsCount="1" RowHeight="18" AutoGenerateColumns="False" AutoGenerateColumnsMode="ResetAll"/>

C#
        public void FillDataSet()
        {
        
            DataTable dtArticles = new DataTable("Articles");
            dtArticles.Columns.Add("id");
            dtArticles.Columns.Add("name");
            dtArticles.Columns.Add("idgroup");

            dtArticles.Rows.Add(1, "Cola",1);
            dtArticles.Rows.Add(2, "Pizza",2);
            dtArticles.Rows.Add(3, "Steak", 2);

            DataTable dtGroups= new DataTable("Groups");
            dtGroups.Columns.Add("idgroup");
            dtGroups.Columns.Add("groupname");

            dtGroups.Rows.Add(1,"Drink");
            dtGroups.Rows.Add(2,"Food");

            ds = new DataSet("gastronomy");

            ds.Tables.Add(dtArticles);
            ds.Tables.Add(dtGroups);

        }

  private void FillDataGrid()
        {
            FillDataSet();

            sfDataGrid2.Columns.Clear();

            sfDataGrid2.ItemsSource = ds.Tables["Articles"].DefaultView;
            sfDataGrid2.AutoGenerateColumns = false;
            sfDataGrid2.AutoGenerateColumnsMode = AutoGenerateColumnsMode.ResetAll;

            //Column definition
            sfDataGrid2.Columns.Add(new GridNumericColumn() { HeaderText = "Article ID", MappingName = "id", NumberDecimalDigits = 0, ColumnSizer = GridLengthUnitType.SizeToHeader });
            sfDataGrid2.Columns.Add(new GridTextColumn() { HeaderText = "Name", MappingName = "name" });
            //This column is only to show that data exists in this column
            sfDataGrid2.Columns.Add(new GridNumericColumn() { HeaderText = "Group ID", MappingName = "idgroup", NumberDecimalDigits = 0, ColumnSizer = GridLengthUnitType.SizeToHeader });

            sfDataGrid2.Columns.Add(new GridComboBoxColumn()
            {
                ItemsSource = ds.Tables["Groups"].DefaultView,
                HeaderText = "Group",
                MappingName = "idgroup",
                DisplayMemberPath = "groupname",
            });
        }

What I get is:


Nothing is displayed in Group column. Ofc in this column I want the user to choose from  Food, Drink options and bind that to idgroup 
I'm missing something but can't figured out. Any help is appreciated.



6 Replies

SV Srinivasan Vasu Syncfusion Team July 17, 2017 04:40 AM UTC

Hi Dubravko, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and we have prepared a sample as per your requirement. By default, GridComboBoxColumn displays the value using MappingName property. You can set DisplayMemberPath which denotes the path to a value on the source object(GridComboBoxColumn.ItemsSource) to serve as visual representation of object. You can set SelectedValuePath which denotes the path to get the SelectedValue from the SelectedItem. We have provided support for bound DataTable Collection as ItemSource in GridComboBoxColumn in Syncfusion version 15.2.0.40. 
 
 
Please refer the below code. 
 
   <Syncfusion:SfDataGrid  x:Name="sfDataGrid2"    
                                HorizontalAlignment="Left" 
                                Height="159" 
                                VerticalAlignment="Top" 
                                Width="922"  
                                AllowFiltering="True"  
                                RowHeaderWidth="15"                   
                                AllowEditing="True" 
                                FooterRowsCount="1"  
                                RowHeight="18"  
                                AutoGenerateColumns="False"  
                                AutoGenerateColumnsMode="ResetAll"> 
        </Syncfusion:SfDataGrid> 
     //Column definition 
            sfDataGrid2.Columns.Add(new GridNumericColumn() 
            { 
                HeaderText = "Article ID", 
                MappingName = "id", 
                NumberDecimalDigits = 0, 
                ColumnSizer = GridLengthUnitType.SizeToHeader 
            }); 
            sfDataGrid2.Columns.Add(new GridTextColumn() { HeaderText = "Name", MappingName = "name" }); 
            //This column is only to show that data exists in this column 
            sfDataGrid2.Columns.Add(new GridNumericColumn() { HeaderText = "Group ID", MappingName = "idgroup", NumberDecimalDigits = 0, ColumnSizer = GridLengthUnitType.SizeToHeader }); 
 
 
            sfDataGrid2.Columns.Add(new GridComboBoxColumn() 
            { 
                MappingName = "idgroup", 
                ItemsSource = view.ComboBoxDetails.DefaultView, 
                HeaderText = "Group",                 
                DisplayMemberPath = "groupname", 
                SelectedValuePath = "idgroup" 
            }); 
            sfDataGrid2.ItemsSource = view.Details.DefaultView; 
 
 
 
Regards, 
Srinivasan 



DF df July 17, 2017 11:38 AM UTC

Thank you for the reply. I still have the same problem. 

When I run your project I get this


When I click on the Group field I can see Drink and Food in Combobox 


but after selection, nothing is displayed in previous field


This is the same problem I had before. Could you please advise how to proceed.

Best regards,




SV Srinivasan Vasu Syncfusion Team July 18, 2017 06:36 PM UTC

Hi Dubravko, 

 
We have tested the provided sample and the sample working fine in our side. We have attached the tested video for your reference. In our previous update, we have mentioned the support provided for bound DataTable Collection as ItemsSource  in GridComboBoxColumn in Syncfusion version 15.2.0.40. So, could you please confirm your syncfusion product version? It will be helpful to analyze further. 

 
Regards, 
Srinivasan 



DF df July 19, 2017 06:09 AM UTC

Hi,

my bad. I still have 14.2460.0.26 version installed. I installed 15.0.2.40 right away to check it. The newest version I installed on my laptop but I didn't tried there.

I will try and get back to you.

Thanks for your effort and time.



DF df replied to Dubravko Fistric July 19, 2017 08:58 AM UTC

Hi,

my bad. I still have 14.2460.0.26 version installed. I installed 15.0.2.40 right away to check it. The newest version I installed on my laptop but I didn't tried there.

I will try and get back to you.

Thanks for your effort and time.


Hi

just to confirm it works with new version.

Thank you again.



SV Srinivasan Vasu Syncfusion Team July 19, 2017 04:50 PM UTC

Hi Dubravko, 
  
Thanks for your update. 
  
Please let us know if you need further assistance on this. 
  
Regards, 
Srinivasan 


Loader.
Up arrow icon