C# 如何在DataGrid内的表中显示来自数组的数据?

C# 如何在DataGrid内的表中显示来自数组的数据?,c#,wpf,C#,Wpf,大家好,我使用Visual Studio 2019和WPF,我的目标是一个GUI,用户可以在其中选择用户希望监视的机器变量。 目前,我的GUI上有2个Datagrid和3个按钮 我想从文件中读取数据,并在左边的Datagrid[pic]中显示数据 如何将数据插入到正确的网格中?我的Begin是否正确 文件外的数据示例: ablauf 0x200003e4 u32 第一个是varname,第二个是adrese,最后一个是变量的大小,我只需要表中的parametername和address 我找到了

大家好,我使用Visual Studio 2019和WPF,我的目标是一个GUI,用户可以在其中选择用户希望监视的机器变量。 目前,我的GUI上有2个Datagrid和3个按钮 我想从文件中读取数据,并在左边的Datagrid[pic]中显示数据

如何将数据插入到正确的网格中?我的Begin是否正确

文件外的数据示例:

ablauf 0x200003e4 u32

第一个是varname,第二个是adrese,最后一个是变量的大小,我只需要表中的parametername和address

我找到了100种将Datagrid与表和SQL数据库连接起来的解决方案,但没有一种是与数组和表连接的,所以当有人知道更好的方法时,请给我写信

为了更好地理解我的代码sofar XAML:

    <Viewbox Margin="0,0,-8,-1">
    <Grid Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="400" Height="280">
        <DataGrid Name="tableAllVar" ItemsSource ="{Binding}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="220" Width="150" Margin="5,30,0,0" FontSize="6" ColumnHeaderHeight="15" AutoGenerateColumns="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Parameter" Binding="{Binding maschinenParameter}" Width="60" />
                <DataGridTextColumn Header="Adresse" Binding="{Binding valueAdresse}" Width="40"/>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid Name="tableChosenVar" ItemsSource ="{Binding}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="220" Width="150" Margin="240,30,0,0" FontSize="6" ColumnHeaderHeight="15" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Parameter" Binding="{Binding maschinenParameter}" Width="60" />
                <DataGridTextColumn Header="Value" Binding="{Binding maschinenValue}" Width="40"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Name="buttonAddVar"  HorizontalAlignment="Left" Margin="178,60,0,0" Width="40" Height="40" VerticalAlignment="Top">
            <StackPanel>
                <Image Source="C:\Users\mgleich\source\repos\Inspect\Inspect\Resources\Rechter_Pfeil_48.png"/>
            </StackPanel>
        </Button>
        <Button Name="buttonDeleteVar"  HorizontalAlignment="Left" Margin="178,110,0,0" Width="40" Height="40" VerticalAlignment="Top">
            <StackPanel>
                <Image Source="C:\Users\mgleich\source\repos\Inspect\Inspect\Resources\Löschen_48.png"/>
            </StackPanel>
        </Button>
        <Button Name="buttonSaveVarList"  HorizontalAlignment="Left" Margin="178,160,0,0" Width="40" Height="40" VerticalAlignment="Top">
            <StackPanel>
                <Image Source="C:\Users\mgleich\source\repos\Inspect\Inspect\Resources\schwarz_save_48.png"/>
            </StackPanel>
        </Button>
        <Label Content="Alle Programmvariablen:" HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Top" FontSize="8" Height="20" Width="150"/>
        <Label Content="Zu überwachende Variablen:" HorizontalAlignment="Left" Margin="240,5,0,0" VerticalAlignment="Top" FontSize="8" Height="20" Width="150"/>
    </Grid>
</Viewbox>
和我的addVar.cs:

    public partial class addVar : Window
{
    public addVar()
    {
        InitializeComponent();
    }

    private void Window_Activated(object sender, EventArgs e)
    {
        int zahler = 0;

        System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\mgleich\source\repos\Inspect\Variablenliste.txt");   //Erstellung Streamreader
        while (zahler < 2)
        {
            string line = sr.ReadLine();
            if (line != "" && line != null)
            {
                //create a new Row
                string[] linearray = line.Split(' ');
                //add the elements from linearray[0-2] into the new Row 



            }
            else {
                zahler++;
            }

        }
    }//End Window_Activated
}

创建具有两个公共字符串属性的类:

public class YourType
{
    public string maschinenParameter { get; set; }
    public string valueAdresse { get; set; }
}
然后,您可以为文件中的每一行创建此类的实例,并将它们添加到您设置或绑定DataGrid的ItemsSource的列表中:


WPF不关心数据来自哪里—SQL Server、平面文件或内存阵列或集合。在所有情况下,控件都绑定到数据集合。事实上,从SQL Server加载数据的解决方案本身会将数据加载到数组、列表或其他容器中。您是否尝试过在已经找到的任何解决方案中使用该代码?您是否遇到了特定的问题?看起来唯一的问题是如何从CSV中读取,这实际上不是WPF问题。您可以使用CsvHelper之类的库将文件加载到数组或对象列表中。例如,我读取filetxt文件没有问题,我使用streamreader,这很好,但我不知道我必须做什么才能在网格中显示变量,这是将Datagrid与Datatable一起使用的正确方法,还是仅使用数据网格就足够了?
private void Window_Activated(object sender, EventArgs e)
{
    int zahler = 0;

    System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\mgleich\source\repos\Inspect\Variablenliste.txt");   //Erstellung Streamreader
    List<YourType> sourceCollection = new List<YourType>();
    while (zahler < 2)
    {
        string line = sr.ReadLine();
        if (line != "" && line != null)
        {
            //create a new Row
            string[] linearray = line.Split(' ');
            //add the elements from linearray[0-2] into the new Row 
            sourceCollection.Add(new YourType()
            {
                maschinenParameter = linearray[0],
                valueAdresse = linearray[1]
            });
        }
        else
        {
            zahler++;
        }
    }
    tableAllVar.ItemsSource = sourceCollection;
}