-1

enter image description here what i want is my combobox to be loaded in the 1st row of my DataGrid and my header consist of checkboxes

ObservableCollection<dynamic> items = new ObservableCollection<dynamic>();
private IEnumerable<string> PropertyNames; //Store the properties names of the dynamic object

//First i am reading my dt through Excel;

            DataSet result = new DataSet();
            DataTable dt = new DataTable();
            if (fileName.EndsWith(".xlsx"))
            {
                // Reading from a binary Excel file (format; *.xlsx)
                FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                result = excelReader.AsDataSet();
                dt = result.Tables[0];
                excelReader.Close();
            }
            if (fileName.EndsWith(".xls"))
            {
                // Reading from a binary Excel file ('97-2003 format; *.xls)
                FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
                IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                result = excelReader.AsDataSet();
                excelReader.Close();
            }

// Then i am creating my header as shown in the attachment as per my dt column count

         for (int i = 0; i < dt.Columns.Count; i++)
            {

                DataGridTextColumn checkBoxColumn = new DataGridTextColumn();

                System.Windows.Controls.CheckBox headerCheckBox = new System.Windows.Controls.CheckBox();
                headerCheckBox.Content = "UnNamedColumn" + i;

                checkBoxColumn.Header = headerCheckBox;
                checkBoxColumn.Binding = new System.Windows.Data.Binding(dt.Rows[0][i].ToString());

                dataGrid.Columns.Add(checkBoxColumn);
            }

// I am trying to load combobox in the 1st row of the DataGrid

            PropertyNames = new List<string>();
            items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == 0)
                {
                    dynamic item = new DynamicObjectClass();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //    DataGridColumn columns = GetNewComboBoxColumn("UnNamedColumn" + j, dt.Rows[0][j].ToString(), binddt)
                        var cb = new System.Windows.Controls.ComboBox();
                        cb.ItemsSource = new List<string> { "C50", "C40", "C30" };
                        cb.SelectedItem = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());
                        item.TrySetMember(new SetPropertyBinder(dt.Rows[0][j].ToString()), cb);
                    }
                    PropertyNames = item.GetDynamicMemberNames();
                    items.Add(item);
                    dataGrid.ItemsSource = items;
                    break;
                }
            }

// And at last i am appending the whole dt in datagrid

        for (int i = 0; i < dt.Rows.Count; i++)
            {
                dynamic item = new DynamicObjectClass();
                for (int j = 0; j < dt.Columns.Count; j++)
                {  
                    item.TrySetMember(new SetPropertyBinder(dt.Rows[0][j].ToString()), dt.Rows[i][j].ToString());
                }                    
                PropertyNames = item.GetDynamicMemberNames();
                items.Add(item);
                dataGrid.ItemsSource = items;
            }

1 Answers1

0

You can do the following and replace the List by your data:

var bindingSource = new BindingSource();
bindingSource.DataSource = new List<string> { "C50", "C40", "C30" };

comboBox1.DataSource = bindingSource.DataSource;

comboBox1.DisplayMember = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());
comboBox1.ValueMember = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());

source: How to bind a List to a ComboBox?

Zen Zac
  • 136
  • 1
  • 9
  • No i want only 1st row of grid to be filled by comboboxes not the whole Grid and as if now i have given hard code data in my combobox i.e C50, C40 ,C30. once this will work, i will replace the data with my json data – ashif shaikh Jan 16 '20 at 10:19
  • i have tryed, that way it gives me column conversion error i.e(for checkbox my column type is DataGridTextColumn and for combobox it requires Column type DataGridColumn). – ashif shaikh Jan 16 '20 at 14:29
  • I think that's because you check for `if (i == 0)`, which is the row with the checkboxes. If you change that to `i == 1`, it should do the same, but for the second row. – Zen Zac Jan 16 '20 at 14:46
  • i have checked if(i == 0) ,because i want my combobox on the 1st row, and checkboxes are my column header. – ashif shaikh Jan 17 '20 at 05:43