繁体   English   中英

为DataGridView设置DataSource

[英]set DataSource for DataGridView

在课堂上我有这两个属性:

public Dictionary<int, int> memCache { get; set; }
public Dictionary<int, MESIState> cacheStates { get; set; }

我使用此代码加入它们:

cache1 = new MESICache(mainMemory);
var info = from a in cache1.memCache
           join b in cache1.cacheStates on a.Key equals b.Key
           select new { address = a.Key, value = a.Value, state = b.Value };

当我将此info变量设置为DataGridView对象的DataSource时,我看不到任何输出。 但有一些警告,如:

Warning 2   The field 'MESI.Form1.addressDataGridViewTextBoxColumn' is never used   C:\Users\User\Path\MESI\MESI\Form1.Designer.cs  495 64  MESI

显然,我没有正确设置数据,但无法弄清楚我应该怎么做。

在LINQ结束时调用.ToList()后查看它是否有效:

var info = (from a in memCache
            join b in cacheStates on a.Key equals b.Key
            select new {address = a.Key, value = a.Value, state = b.Value}).ToList();

dataGridView1.DataSource = info;

当我没有尝试它,只是将IEnumerable绑定到DataGridView时,它不会显示。

至于你所看到的警告, “addressDataGridViewTextBoxColumn永远不会被使用” ,你可能在Form1中有一个未使用的类级变量,编译器只是告诉你你可能已经忘记了它(并且可能只是删除它)它)。

暂无
暂无

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

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