一、最简筛选
不考虑大小写的最简单情况
mydb db=new mydb();
db.x.load();
this.xBindingSource.DataSource = db.x.local.tobindinglist(); //datagridview.datasource=this.xBindingsource
这种情况下可以对数据进行修改,然后通过
this.datagridview.endedit();
db.savechanges()
保存数据修改
查datagrid是无法过滤筛选数据的,设置xBindingSource的filter是无效,这个只能用在list的筛选上,对bindinglist无效。
解决办法:
this.xBindingSource.DataSourcenew=new BindingList<x>(db.x.Local.Where(p => p.s == "xxx").ToList());
将筛选出的数据转为bindinglist给bindingsource赋值即可
这样可以实现筛选数据到datagridview的双向绑定,并可以在db.savechanges中保存数据
二、简单的详情
首先 我们要有对应的数据库 实体对象
public class product
{
/// <summary>
/// 商品信息id 主键
/// </summary>
//[DatabaseGenerated(DatabaseGeneratedOption.None)] 去除自增长
[ProductComm]
public long id
{
get;
set;
}
[ProductComm]
public string name
{
get;
set;
}
/// <summary>
/// 创建时间
/// </summary>
public DateTime ctime
{
get;
set;
}
/// <summary>
/// 0正常 1已删除
/// </summary>
public int s
{
get;
set;
}
[NotMapped]
public string username { get; set; }
}
对类对象进行构造 指定表名 主键名称 长度 等等规则
public class ProductMapping : EntityTypeConfiguration<product>
{
public ProductMapping()
{
this.ToTable("product");
this.HasKey(x => x.id);
this.Property(x => x.name).HasMaxLength(50);
//this.HasRequired(x => x.Member).WithMany().HasForeignKey(x => x.MemberId);
}
}
那么到了上面这一步 我们的数据库 已经和Data 类 相互关联起来了
public List<product> GetproductInfo()
{
ConnectionData c = new ConnectionData();
var datas= c.ProductInfo.Where(q => q.id == 1).ToList();
return datas;
}
到了这里 我们已经取出你的实体对象对应的数据库对象了。