简体   繁体   English

如何使用C#填充WPF数据网格而不是直接从数据库填充

[英]How to fill a WPF Datagrid but not directly from the DB using C#

I have been banging my head against a wall for a while on this one. 我一直在这个墙上撞墙一段时间。 I'm trying to fill a WPF datagrid from a mysql database using C# .net. 我正在尝试使用C#.net从mysql数据库填充WPF数据网格。 The problem is that I need to modify the data before sticking it into the datagrid because the data is not in the right format. 问题是我需要在将数据粘贴到数据网格之前修改数据,因为数据格式不正确。 The only examples I can find when searching is how to automatically stick the data from the query directly into the database. 我在搜索时可以找到的唯一示例是如何自动将查询中的数据直接粘贴到数据库中。

Here is what my current table from mysql looks like. 这是我目前的mysql表格。

1   0456    clockin         5/14/2013 8:36:26 AM
2   0456    breakout    5/14/2013 8:36:39 AM
3   0456    breakin     5/14/2013 8:36:40 AM
4   0456    clockout    5/14/2013 8:36:41 AM
5   0456    clockin         5/14/2013 8:37:14 AM
6   0456    breakout    5/14/2013 8:50:27 AM
7   0456    breakin     5/14/2013 8:50:34 AM

Now I need to take out all the punches for the week(5/12 - 5/18) with the given userID(0456). 现在我需要用给定的用户ID(0456)取出本周(5月12日至5月18日)的所有拳击。 Then I need to stick those values into a datagrid that is set up with the seven days of the week. 然后我需要将这些值粘贴到一周中七天设置的数据网格中。 So under each day it would list all the punches(clockin, breakout, etc) with their associated times. 因此,在每一天,它会列出所有的冲击(clockin,breakout等)及其相关时间。

Edit: tnw asked for code. 编辑:tnw要求代码。 Here is my XML for the datagrid: 这是我的datagrid的XML:

<DataGrid AutoGenerateColumns="False" Height="253" HorizontalAlignment="Center" Margin="219,208,0,0" Name="weekHours" VerticalAlignment="Top" Width="556" HorizontalContentAlignment="Center" CanUserAddRows="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" Focusable="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Action" Width="100"/>
            <DataGridTextColumn Header="Sun" Width="65"/>
            <DataGridTextColumn Header="Mon" Width="65"/>
            <DataGridTextColumn Header="Tue" Width="65"/>
            <DataGridTextColumn Header="Wed" Width="65"/>
            <DataGridTextColumn Header="Thu" Width="65"/>
            <DataGridTextColumn Header="Fri" Width="65"/>
            <DataGridTextColumn Header="Sat" Width="65"/>
        </DataGrid.Columns>
    </DataGrid>

And then for the code behind, this is all I can find on how to fill a Datagrid. 然后对于后面的代码,这是我可以找到的关于如何填充Datagrid的全部内容。

public void FillDataGrid(String query, DataGrid dg)
    {
        //Open connection
        if (this.OpenConnection() == true)
        {
            //Create Command
            MySqlCommand cmd = new MySqlCommand(query, connection);
            DataTable dt = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(dt);
            dg.DataContext = dt;
        }
    }

Any help would be much appreciated. 任何帮助将非常感激。 I am brand new to C# and .net. 我是C#和.net的新手。 Maybe I should not even be using a datagrid for this. 也许我甚至不应该使用数据网格。 Thanks Mike 谢谢迈克

You could use a view model that is bound to your DataGrid . 您可以使用绑定到DataGrid的视图模型。 When you have loaded the data from database, iterate through the records and create a view model. 从数据库加载数据后,遍历记录并创建视图模型。 The following snippet shows a way how you could solve your problem: 以下代码段显示了解决问题的方法:

MyViewModel.cs: MyViewModel.cs:

 public class MyViewModel
 {
      public DateTime Date { get; set; }
      public int Punches { get; set; }
      public int UserId { get; set; }
 }

Here you bind the data to the DataGrid: 在这里,您将数据绑定到DataGrid:

 var viewModels = new List<MyViewModel>();

 foreach(var item in dt.Rows)
 {
      var vm = new MyViewModel();
      vm.UserId = item["UserId"];
      vm.Punches = item["Punches"];
      vm.Date = DateTime.Parse(item["Date"]);
      //....
      viewModels.Add(vm);
 }
 dg.DataContext = viewModels;

This is a common pattern if you work with WPF and maybe with MVVM. 如果您使用WPF并且可能使用MVVM,这是一种常见模式。 A view model contains the actual data but optionally modified for better displaying in the view (here your DataGrid ). 视图模型包含实际数据,但可以选择进行修改以便在视图中更好地显示(这里是您的DataGrid )。 If you do some research, you will notice, that mostly a view model implements INotifyPropertyChanged for change tracking. 如果您进行一些研究,您会注意到,大多数视图模型都会实现INotifyPropertyChanged以进行更改跟踪。 Your view model could also implement this interface, if you want to react when the user modifies something in the DataGrid . 如果您想在用户修改DataGrid中的内容时做出反应,您的视图模型也可以实现此接口。

you can use a ListView - GridView 你可以使用ListView - GridView

<ListView ItemsSource="{Binding YourData}">
    <ListView.View>
        <GridView>
            <!-- The columns here -->
        </GridView>
    </ListView.View>
</ListView>

In your code you will create a property (or as you like to do it) which is IEnumerable and you can databind with it. 在你的代码中,你将创建一个属性(或你喜欢这样做),它是IEnumerable ,你可以用它来数据绑定。 Otherwise you can set the Datasource property of the grid. 否则,您可以设置网格的“ Datasource属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM