I can't do it so that the text in ComboBox is displayed in Excel, tell me what else do I need to add to the code? I get that: the first column that refers to the DataGridTextColumns text in Excel is visible, but the column that belongs to the DataGridComboBoxColumns text in Excel is not visible enter image description here

<Window x:Class="ComboboxExcel.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:ComboboxExcel" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ObjectDataProvider x:Key="PersonEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type Type="local:SecondName"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="30"/> </Grid.RowDefinitions> <DataGrid x:Name="DGR" AutoGenerateColumns="False" ItemsSource="{Binding AllPerson}"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> <DataGridComboBoxColumn Header="SecondName" SelectedItemBinding="{Binding SecondName, Mode=TwoWay}" ItemsSource="{Binding Source={StaticResource PersonEnum}}"/> </DataGrid.Columns> </DataGrid> <Button x:Name="btn" Grid.Row="1" Width="70" Content="Save Excel" Click="btn_Click" /> </Grid> 

 public partial class MainWindow : Window { private MVVM mvvm = new MVVM(); public MainWindow() { InitializeComponent(); this.DataContext = mvvm; } private void btn_Click(object sender, RoutedEventArgs e) { Excel.Application excelApp = new Excel.Application(); excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Add(System.Reflection.Missing.Value); Microsoft.Office.Interop.Excel.Worksheet woorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; excelApp.Visible = true; excelApp.SheetsInNewWorkbook = 1; excelApp.Sheets[1].Name = "Лист 1"; excelApp.DisplayAlerts = true; for(int i = 0; i < DGR.Columns.Count; i++) { Microsoft.Office.Interop.Excel.Range woorkrange = (Microsoft.Office.Interop.Excel.Range)woorksheet.Cells[1, i + 1]; woorksheet.Columns.get_Range("A1", "B1").Borders.LineStyle = Excel.XlLineStyle.xlContinuous; woorksheet.get_Range("A1", "B1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; woorkrange.Value2 = DGR.Columns[i].Header; } for (int i = 0; i < DGR.Columns.Count - 1; i++) { for (int j = 0; j < DGR.Items.Count; j++) { TextBlock b = DGR.Columns[i].GetCellContent(DGR.Items[j]) as TextBlock; Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)woorksheet.Cells[j + 2, i + 1]; myRange.Value2 = b.Text; } } } } 

}

 public class Person { public string Name { get; set; } public SecondName SecondName { get; set; } } public enum SecondName { Иванов, Петров } 

}

 public class MVVM { private ObservableCollection<Person> allPerson = new ObservableCollection<Person>(); public ObservableCollection<Person> AllPerson { get { return allPerson; } set { allPerson = value; } } } 

}

  • Strange you ... The previous questions were with at least some hint of MVVM and suddenly you, all in one pile, all mixed up, all MVVM rules were violated. I advise you to decide on what you specifically want from your application! And then at a pace you’ll soon say, “I hate WPF, well, it’s nafig” ... PS I don’t see any point in the datagrid tags, the combobox and I don’t see the main tag - c# ... - EvgeniyZ
  • @EvgeniyZ I tried to simplify the code for the question. - Eugene
  • Why do you need MVVM class if you have all the logic inside the window? :) - Andrey NOP
  • Since you are using a binding, why are you going to write data to Excel in DataGrid? Why not take them from AllPerson ? - Andrey NOP
  • @AndrewNOP I initially simplified the code, then complicated it :))))), but if I take AllPerson, I hardly can handle it, and I tried to do without MVVM first. - Eugene

0