上天试道者 2024-04-26 17:51 采纳率: 28.6%
浏览 6
已结题

如何根据sqlite查询方法在ViewModels绑定数据到表格

SQLite数据库查询方法


 // 查询数据的方法
    public DataTable SelectData(string tableName, string condition)
    {
        using (SQLiteConnection connection = OpenConnection()) // 使用using语句确保连接被正确关闭
        {
            string query = $"SELECT * FROM {tableName} WHERE {condition};"; // 构造查询数据的SQL语句
            using (SQLiteCommand command = new SQLiteCommand(query, connection)) // 创建SQLiteCommand对象
            {
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command); // 创建SQLiteDataAdapter对象,用于填充数据表
                DataTable dataTable = new DataTable(); // 创建数据表对象
                adapter.Fill(dataTable); // 填充数据表
                return dataTable; // 返回数据表对象
            }
        }
    }

调用查询方法

 DataTable dataTable = dbManager.SelectData("Config");

如何在ViewModels绑定数据到表格


```xml
 <DataGrid  x:Name="myDataGrid" Grid.Row="1" SelectionMode="Single" ItemsSource="{Binding AllocationModels}" AutoGenerateColumns="False" >
   
   <DataGrid.Styles>
     <Styles>
       <Style Selector="DataGridRow:nth-child(odd)">
         <Setter Property="Background" Value="White"/>
         <!-- 奇数行背景色 -->
       </Style>
       <Style Selector="DataGridRow:nth-child(even)">
         <Setter Property="Background" Value="#97FFFF"/>
         <!-- 偶数行背景色 -->
       </Style>
       
     </Styles>
   </DataGrid.Styles>
   <DataGrid.Columns>
     <DataGridTextColumn
         Width="auto"
         Binding="{Binding XGateIP}"
         Header="XGateIP" />
     <DataGridTextColumn
         Width="auto"
         Binding="{Binding Bus}"
         Header="Bus" />
     <DataGridTextColumn
         Width="auto"
         Binding="{Binding Address}"
         Header="Address" />
     <DataGridTextColumn
        Width="auto"
        Binding="{Binding Type}"
        Header="Type" />
     <DataGridTextColumn
         Width="auto"
         Binding="{Binding Groups}"
         Header="Groups" />
     <DataGridTextColumn
         Width="auto"
         Binding="{Binding IsText}"
         Header="IsText" />
     <DataGridTextColumn
         Width="auto"
         Binding="{Binding Shelves}"
         Header="货架" />
     <DataGridTextColumn
        Width="auto"
        Binding="{Binding Level}"
        Header="层面" />
     <DataGridTextColumn
         Width="auto"
         Binding="{Binding ListNumber}"
         Header="列数" />
    
   </DataGrid.Columns>
   <!-- 添加一列包含按钮的模板列 -->
   <DataGrid.Columns>
     <DataGridTemplateColumn Header="绑定">
       <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
           <Button Content="生成"
                  Background="Green"
                   />
         </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>
   </DataGrid.Columns>
 </DataGrid>


```

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-04-26 17:51
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    问题概括

    这个问题的目的是将SQLite数据库查询得到的数据绑定到一个表格(DataGrid)控件中,以便在ViewModels中展示和操作。

    问题出现原因

    问题出现的原因是用户需要知道如何将数据库查询结果与界面控件(如DataGrid)进行绑定,以便在应用程序的视图(View)中展示数据。

    问题解决方案

    为了解决这个问题,我们需要做以下几个步骤:

    1. 定义数据模型:首先,需要定义一个与数据库表结构相匹配的C#类,这个类将作为数据模型(Model)。

    2. 转换DataTable到List:将查询得到的DataTable转换为一个对象列表(如List<AllocationModel>)。

    3. 在ViewModel中绑定数据:在ViewModel中创建一个属性,该属性的类型是ObservableCollection<AllocationModel>,用于绑定到DataGrid的ItemsSource

    4. 更新视图:当数据更新时,通知视图进行更新。

    提供代码

    以下是根据上述步骤修改后的代码示例:

    数据模型类(AllocationModel.cs)

    public class AllocationModel
    {
        public string XGateIP { get; set; }
        public string Bus { get; set; }
        public string Address { get; set; }
        public string Type { get; set; }
        public string Groups { get; set; }
        public string IsText { get; set; }
        public string Shelves { get; set; }
        public string Level { get; set; }
        public string ListNumber { get; set; }
        // 其他需要的属性...
    }
    

    ViewModel中绑定数据(AllocationViewModel.cs)

    using System.Collections.ObjectModel;
    // ...
    
    public class AllocationViewModel
    {
        private ObservableCollection<AllocationModel> _allocationModels;
        public ObservableCollection<AllocationModel> AllocationModels
        {
            get => _allocationModels;
            set
            {
                _allocationModels = value;
                OnPropertyChanged(nameof(AllocationModels)); // INotifyPropertyChanged
            }
        }
    
        public AllocationViewModel()
        {
            // 假设dbManager是已经初始化的SQLite数据库管理类
            DataTable dataTable = dbManager.SelectData("Config");
            AllocationModels = ConvertDataTableToAllocationModelList(dataTable);
        }
    
        private ObservableCollection<AllocationModel> ConvertDataTableToAllocationModelList(DataTable dataTable)
        {
            var list = new ObservableCollection<AllocationModel>();
            foreach (DataRow row in dataTable.Rows)
            {
                list.Add(new AllocationModel
                {
                    XGateIP = row["XGateIP"].ToString(),
                    Bus = row["Bus"].ToString(),
                    Address = row["Address"].ToString(),
                    // ...其他属性
                });
            }
            return list;
        }
    
        // INotifyPropertyChanged的实现...
    }
    

    XAML中绑定ViewModel

    <Window ...>
        <!-- DataContext设置为AllocationViewModel的实例 -->
        <Window.DataContext>
            <local:AllocationViewModel/>
        </Window.DataContext>
        <!-- DataGrid绑定代码不变 -->
        <!-- ... -->
    </Window>
    

    代码运行方式

    1. 创建一个新的WPF项目。
    2. 将上述代码添加到项目中相应的文件。
    3. 运行项目,查看DataGrid是否正确显示了SQLite查询结果。

    代码预期运行结果

    运行代码后,DataGrid控件将展示SQLite数据库中查询到的数据,每行数据将根据定义的列进行展示。

    推荐相关链接

    请注意,上述代码仅为示例,实际使用时需要根据具体的数据库结构和业务逻辑进行调整。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月8日
  • 已采纳回答 4月30日
  • 创建了问题 4月26日