html
/*没有在xaml设置上下文window.context是因为 命名空间一直对应不上 所以在xaml.cs
里面绑定*/
<Window x:Class="DataGrid.views.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:local="clr-namespace:DataGrid"
xmlns:AutoGrid="clr-namespace:WpfAutoGrid"
xmlns:viewmodels="clr-namespace:DataGrid.viewmodels"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<AutoGrid:AutoGrid ColumnCount="2" RowCount="2" Rows="5*,*" Columns="*,*" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<DataGrid Grid.ColumnSpan="2" ItemsSource="{Binding Itemsview}">
</DataGrid>
<TextBox HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Text="{Binding Pagenum}" ></TextBox>
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Command="{Binding NextPageCommand}">Next</Button>
</AutoGrid:AutoGrid>
</Grid>
</Window>
cs
using Bogus;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using Bogus;
using System.Collections.ObjectModel;
namespace DataGrid.viewmodels
{
public class student //类变量要是属性getset
{
public int Id { get; set; }
public string Name { get; set; }
public double Score { get; set; }
}
public class StudentDataGenerator
{
public static ObservableCollection<student> GenerateStudents(int count)
{
var fakeStudents = new Faker<student>()
.RuleFor(s => s.Id, f => f.IndexGlobal + 1) // ID从1开始
.RuleFor(s => s.Name, f => f.Name.FullName()) // 随机姓名
.RuleFor(s => s.Score, f => f.Random.Double(0, 100)) // 0-100的随机分数
.Generate(count); // 生成指定数量的学生
return new ObservableCollection<student>(fakeStudents);
}
} /*bogus生成数据*/
public partial class DataControl:ObservableObject /*这里一定要partial*/
{
private CollectionView _itemsview;
private const int _pagesize = 10;
private int _pagenum = 1;
public CollectionView Itemsview { get { return _itemsview; } }
public int Pagenum
{
get { return _pagenum; }
set { SetProperty(ref _pagenum, value); Itemsview?.Refresh(); }
}
public int TotalPages => (int)Math.Ceiling(Students.Count / (double)_pagesize);
[ObservableProperty]
ObservableCollection<student> _students;
[RelayCommand]
public void NextPage()
{
if (_pagenum < TotalPages) // 如果还有下一页
{
Pagenum++; //这里一定只能改属性
}
}
[RelayCommand] //relaycommand生成的命令会在函数后面加上command
public void PrevPage()
{
if (_pagenum > 1) // 如果不是第1页
{
Pagenum--;
}
}
//原理就是_itemsview匹配数据 过滤之后留下页码是1的内容 返回true的内容会被存入_itemsview
public DataControl(ObservableCollection<student> stus) //有参构造函数传数据
{
this._students = stus; //这里要用observablecollection 不能用enumerable
_itemsview = (CollectionView)CollectionViewSource.GetDefaultView(this._students);
_itemsview.Filter = (item) =>
{
var index = Students.IndexOf((student)item); int itemPage = (index / _pagesize) + 1;
return itemPage == Pagenum; // 只显示当前页的数据;
};
}
}
}
cs
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataGrid.viewmodels;
namespace DataGrid.views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new DataControl(StudentDataGenerator.GenerateStudents(36));
}
}
}
//xaml不好绑定viewmodel 就只能到cs里面绑定了 this.datacontext = new ........
//坏处就是xaml不会有提示
sqlsugar操作
cs
using SqlSugar;
using System;
public static class IntDbHelper
{
private static SqlSugarScope Connection => new SqlSugarScope(new ConnectionConfig()
{
ConnectionString = "Data Source=./data.db",
DbType = DbType.Sqlite,
IsAutoCloseConnection = true
});
// -------------------- 针对int值的操作 --------------------
// 插入数据(返回新增的ID)
public static int Insert(string tableName, Dictionary<string, object> values)
{
return Connection.Insertable(values).AS(tableName).ExecuteReturnIdentity();
}
// 查询数据(返回单个int值)
public static int GetInt(string tableName, string field, int id)
{
return Connection.Queryable<dynamic>()
.AS(tableName)
.Where($"Id = {id}")
.Select(field)
.First();
}
// 更新数据(更新指定int字段)
public static bool UpdateInt(string tableName, int id, string field, int newValue)
{
return Connection.Updateable<dynamic>()
.AS(tableName)
.SetColumns($"{field} = {newValue}")
.Where($"Id = {id}")
.ExecuteCommand() > 0;
}
// 删除数据(根据int ID)
public static bool Delete(string tableName, int id)
{
return Connection.Deleteable<dynamic>()
.AS(tableName)
.Where($"Id = {id}")
.ExecuteCommand() > 0;
}
}
反射和linq也能分页
object myObject = new MyClass();
PropertyInfo propertyInfo = myObject.GetType().GetProperty("Name");
if (propertyInfo != null)
{
object value = propertyInfo.GetValue(myObject);
Console.WriteLine($"属性值: {value}");
}
1建议一次只拿一个对象的属性 对象列表用循环多次读取
2GetValue是属性拿对应的值 所以要传对象
分析数据可以当参数传给后台
带参数的命令只支持object类型
linq里面只能OfType<student>() 不能强转
datagrid 返回的 selecteditems 是IList类型的
CollectionView 方式
- 优点:自动处理分页,删除后自动回填
- 缺点:需要调用 Refresh() 刷新视图
会自动回填
linq加反射则不会自动回填