Not able to display the values and text in Textblock from csv file using Wpf

jessy mary 1 Reputation point
2022-01-19T05:44:06.763+00:00

Hi,
i have to display the values and text that is stored in Textblock from csv file using wpf.
I dont have any idea can anyone help me on this?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,669 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    2022-01-20T03:04:35.93+00:00

    MainWindow.xaml:

    <StackPanel>  
        <DataGrid x:Name="datagrid"  Width="80" Height="100" HeadersVisibility="None"  AutoGenerateColumns="False" >  
            <DataGrid.ColumnHeaderStyle>  
                <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">  
                    <Setter Property="LayoutTransform">  
                        <Setter.Value>  
                            <TransformGroup>  
                                <RotateTransform Angle="-90"/>  
                                <ScaleTransform ScaleX="1" ScaleY="-1" />  
                            </TransformGroup>  
                        </Setter.Value>  
                    </Setter>  
                </Style>  
            </DataGrid.ColumnHeaderStyle>  
            <DataGrid.CellStyle>  
                <Style  TargetType="DataGridCell">  
                    <Setter Property="LayoutTransform">  
                        <Setter.Value>  
                            <TransformGroup>  
                                <RotateTransform Angle="-90"/>  
                                <ScaleTransform ScaleX="1" ScaleY="-1" />  
                            </TransformGroup>  
                        </Setter.Value>  
                    </Setter>  
                </Style>  
            </DataGrid.CellStyle>  
            <DataGrid.LayoutTransform>  
                <TransformGroup>  
                    <RotateTransform Angle="90"/>  
                    <MatrixTransform Matrix="-1,0,0,1,0,0"/>  
                </TransformGroup>  
            </DataGrid.LayoutTransform>  
            <DataGrid.Columns>  
                <DataGridTextColumn Binding="{Binding TotalExection}">  
                    <DataGridTextColumn.ElementStyle>  
                        <Style>  
                            <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>  
                        </Style>  
                    </DataGridTextColumn.ElementStyle>  
                </DataGridTextColumn>  
                <DataGridTextColumn Binding="{Binding Successful}">  
                    <DataGridTextColumn.ElementStyle>  
                        <Style>  
                            <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>  
                        </Style>  
                    </DataGridTextColumn.ElementStyle>  
                </DataGridTextColumn>  
                <DataGridTextColumn Binding="{Binding Error}">  
                    <DataGridTextColumn.ElementStyle>  
                        <Style>  
                            <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>  
                        </Style>  
                    </DataGridTextColumn.ElementStyle>  
                </DataGridTextColumn>  
            </DataGrid.Columns>  
        </DataGrid>  
        <Button x:Name="btn1" Height="30" Width="100" Content="import" Click="btn1_Click"/>  
    </StackPanel>  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        private void btn1_Click(object sender, RoutedEventArgs e)  
        {  
          datagrid.ItemsSource = ReadCSV(@"C:\Users\huiliul\Desktop\temp.csv");  
        }  
        public IEnumerable<Data> ReadCSV(string fileName)  
        {  
          string[] lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName, ".csv"));  
      
          return lines.Select(line =>  
          {  
            string[] data = line.Split(';');  
      
            return new Data(data[0], data[1], data[2]);  
      
          });  
        }  
      }  
      public class Data  
      {  
        public Data() { }  
        public Data(string successful , string error , string total)  
        {  
          TotalExection = total;  
          Successful= successful;  
          Error=error;  
        }  
        public string TotalExection { get; set; }  
      
        public string Successful { get; set; }  
      
        public string Error { get; set; }  
      }  
    

    temp.csv:
    166661-image.png

    result:
    166579-2.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments