从LINQ加载DataGridView [英] DataGridView Loading From LINQ

查看:82
本文介绍了从LINQ加载DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DataGridViews的表单,该表单的DataSource设置为数据库的DataContext,以自动提取信息并允许对其进行编辑.但是我需要一种颜色来显示来自与颜色表中的颜色相对应的颜色id的颜色,而且我不知道如何通过允许它自动编辑和更新记录来实现这一点.

 private dbCounterTopsDataContext dbContext = new dbCounterTopsDataContext();

private void FrmCurrentInventory_Load(object sender, EventArgs e)
{
    dataGridColors.DataSource = dbContext.Colors;
    dataGridBarTops.DataSource = dbContext.BarTops;
    dataGridKitchenTops.DataSource = dbContext.Kitchens;
    dataGridVanityTops.DataSource = dbContext.Vanities;
}
 

我也尝试过这种方法,它可以与显示一起使用,但是我不能再直接从datagrid更新

 private void FrmCurrentInventory_Load(object sender, EventArgs e)
{
    dataGridColors.DataSource = dbContext.Colors.Select(o => new { ColorId = o.ColorID, Color = o.Color1 });
    dataGridBarTops.DataSource = dbContext.BarTops.Select(o => new { Color = o.Color.Color1, Length = o.Length, Width = o.Width });
    dataGridKitchenTops.DataSource = dbContext.Kitchens.Select(o => new { Color = o.Color.Color1, Length = o.Length }).ToList();
    dataGridVanityTops.DataSource = dbContext.Vanities;
}
 

解决方案

SSS注释中的一些不错的技巧,也许最简单的方法是使用AutogenerateColumns生成大多数内容,然后添加一些组合您的颜色的方框列.它可能会像这样:

dataGridColors.DataSource = 

dataGridBarTops.DataSource = dbContext.BarTops;
dataGridBarTops.AutogenerateColumns = false;
var c = new DataGridViewComboBoxColumn();
c.HeaderText = "Color";
c.DisplayMember = "Color1"; //name of property that says Blue in color entity
c.ValueMember = "ColorID"; //name of property that says 5 in color/entity
c.DataPropertyName = "BarTopColorId"; //name of property that says 5 in BARTOPS table/entity
c.DataSource = dbContext.Colors; //personally I would use a bindingSource here
dataGridBarTops.Columns.Add(cb);

我将删除文本框color is column作为练习-也许先留意一下组合的工作原理

Colors datagridview用于编辑实际的Colors定义等.bar top表中的组合框用于为单个bar top选择不同的颜色.如果编辑颜色"表,它将更改组合列表中的内容.如果您选择一个新的组合值,它将更改Bartop的颜色ID(无论该属性叫什么)的值

请注意不要编辑掉用于Bartops的颜色中的ID-不要在仍然有使用Bartops的Bartop时从Colors表中删除颜色ID 5.


我在这里对绑定源发表了评论.我从未将DGV直接绑定到LINQ实体列表,也可能不会(部分原因是因为它正在下载整个表,这可能会很大,部分原因是我的winforms数据绑定几乎只与强类型数据集绑定),但是您发现它可以工作(也许您需要整个表,无需过滤).但是,如果您开始看到奇怪的效果,例如在Colors数据网格视图中选择不同的行导致所有组合框更改值,请通过绑定源绑定组合,以便它们具有专门跟踪组合当前项目"的内容

I have a form with DataGridViews that have the DataSource set to a DataContext of my database to auto pull in the info and allow it to be edited. But I need a color to display a Color from the color id that corresponds to a color in a colors table and I don't know how I can achieve this with also allowing it to auto edit and update the records.

private dbCounterTopsDataContext dbContext = new dbCounterTopsDataContext();

private void FrmCurrentInventory_Load(object sender, EventArgs e)
{
    dataGridColors.DataSource = dbContext.Colors;
    dataGridBarTops.DataSource = dbContext.BarTops;
    dataGridKitchenTops.DataSource = dbContext.Kitchens;
    dataGridVanityTops.DataSource = dbContext.Vanities;
}

I have also tried this and it worked with displaying but I could no longer update directly from the datagrid

private void FrmCurrentInventory_Load(object sender, EventArgs e)
{
    dataGridColors.DataSource = dbContext.Colors.Select(o => new { ColorId = o.ColorID, Color = o.Color1 });
    dataGridBarTops.DataSource = dbContext.BarTops.Select(o => new { Color = o.Color.Color1, Length = o.Length, Width = o.Width });
    dataGridKitchenTops.DataSource = dbContext.Kitchens.Select(o => new { Color = o.Color.Color1, Length = o.Length }).ToList();
    dataGridVanityTops.DataSource = dbContext.Vanities;
}

解决方案

Some good tips in the comments from SSS, perhaps the easiest way to get this going is to use AutogenerateColumns to gen most of the stuff and then add a few combo box columns for your colors. It would probably go something like this:

dataGridColors.DataSource = 

dataGridBarTops.DataSource = dbContext.BarTops;
dataGridBarTops.AutogenerateColumns = false;
var c = new DataGridViewComboBoxColumn();
c.HeaderText = "Color";
c.DisplayMember = "Color1"; //name of property that says Blue in color entity
c.ValueMember = "ColorID"; //name of property that says 5 in color/entity
c.DataPropertyName = "BarTopColorId"; //name of property that says 5 in BARTOPS table/entity
c.DataSource = dbContext.Colors; //personally I would use a bindingSource here
dataGridBarTops.Columns.Add(cb);

I'll leave removing the text box color is column as an exercise for you- maybe leave it in first to see how combo is working

The Colors datagridview is for editing the actual Colors definitions etc. The combo box in the bar top table is for choosing a different color is for a single bar top. If you edit the Colors table it will change what is in the list of the combo. If you pick a new combo value it will change the bartop's color id (whatever that property is called) value

Be careful not to edit away ids in Colors that are used in bartops - don't delete color Id 5 from the Colors table while there is still a bartop that uses it


I put a comment there about bindingsources. I've never bound a DGV straight to a LINQ entity list, nor probably would I (partly because it's downloading an entire table, which could be huge and partly because my winforms databinding is near exclusively with strongly typed datasets) but you find it to work (and maybe you need the whole table, no filtering). If, however you start to see strange effects like selecting different rows in the Colors datagridview causes all the combo boxes to change value, bind the combo through a bindingsource so they have something that dedicatedly tracks "the current item" for the combo

这篇关于从LINQ加载DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆