你如何从绑定到一个列表℃的绑定源的正确映射名称; T&GT ;,或匿名类型,在一个使用的DataGridTableStyle? [英] How do you get the proper mapping name from a binding source bound to a List<T>, or an anonymous type, to use on a DataGridTableStyle?

查看:186
本文介绍了你如何从绑定到一个列表℃的绑定源的正确映射名称; T&GT ;,或匿名类型,在一个使用的DataGridTableStyle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个DataGridTableStyle对象,以便我可以控制一个DataGrid的列宽。我创建绑定到一个列表中的BindingSource对象。其实它绑定到下列方式虽然LINQ的创建一个匿名类型列表(变量名称更改为我在做什么的清晰度):

I'm trying to create a DataGridTableStyle object so that I can control the column widths of a DataGrid. I've created a BindingSource object bound to a List. Actually it's bound to an anonymous type list created though Linq in the following manner (variable names changed for clarity of what I'm doing):

List<myType> myList = new List<myType>(someCapacity);
.
...populate the list with query from database...
.

var query = from i in myList
            select new
            {
                i.FieldA,
                i.FieldB,
                i.FieldC
            };

myBindingSource.DataSource = query;
myDataGrid.DataSource = myBindingSource;



然后我创建了一个DataGridTableStyle对象并将其添加到数据网格。然而,它永远不会适用于我的表样式属性我设置了,因为我似乎无法设置适当的myDataGridTableStyle.MappingName属性。

Then I create a DataGridTableStyle object and add it to the datagrid. However, it never applies my table style properties I set up because I can't seem set the proper myDataGridTableStyle.MappingName property.

我已经搜索谷歌约1 / 2小时并保持看到链接到同一个问题在整个一群不同论坛(字面相同的文字一样,有人只是复制和粘贴的问题...我恨......)。总之,没有一个建议的工作,就像人说的所有其他网站。

I've searched Google for about 1/2 an hour and keep seeing links to the same question throughout a bunch of different forums (literally the same text, like someone just copied and pasted the question... I hate that...). Anyway, none of the suggestions work, just like the guy says on all the other sites.

所以在这里所做的任何人知道我需要什么,以MappingName属性设置为以有我TABLESTYLE实际工作是否正常?我在哪里可以抓住从名字? (它不能为空...这仅适用于绑定到DataTable或SqlCeResultSet等一个的BindingSource)。

So does anybody here know what I need to set the MappingName property to in order to have my TableStyle actually work properly? Where can I grab the name from? (It can't be blank... that only works with a BindingSource that is bound to a DataTable or SqlCeResultSet etc.).

我想它可以使用LINQ创建一个匿名的,更专业的对象,只有我需要的字段版本是一个问题,和我在一起。如果我只是尝试到BindingSource直接绑定到List对象?或者甚至直接绑定的DataGrid中的列表对象,并完全跳过绑定源。

I'm thinking it could be an issue with me using Linq to create an anonymous, more specialized version of the objects with only the fields I need. Should I just try to bind the BindingSource directly to the List object? Or maybe even bind the DataGrid directly to the List object and skip the binding source altogether.

感谢

PS - C#,Compact Framework的v3.5版本

PS - C#, Compact Framework v3.5

更新:

我已经张贴的答案低于解决了我问题。它是否是最好的方法,它的工作。值得如果你有同样的问题,我有一个偷看。

I've posted an answer below that solved my problem. Whether or not it's the best approach, it did work. Worth a peek if you're having the same issue I had.

推荐答案

我发现,使这项工作的方式。我会打破它段...

I've found the way to make this work. I'll break it out into sections...

List<myType> myList = new List<myType>(someCapacity);
.
...populate the list with query from database...
.







DataGridTableStyle myDataGridTableStyle = new DatGridtTableStyle();
DataGridTextBoxColumn colA = new DataGridTextBoxColumn();
DataGridTextBoxColumn colB = new DataGridTextBoxColumn();
DataGridTextBoxColumn colC = new DataGridTextBoxColumn();

colA.MappingName = "FieldA";
colA.HeaderText = "Field A";
colA.Width = 50; // or whatever;

colB.MappingName = "FieldB";
.
... etc. (lather, rinse, repeat for each column I want)
.

myDataGridTableStyle.GridColumnStyles.Add(colA);
myDataGridTableStyle.GridColumnStyles.Add(colB);
myDataGridTableStyle.GridColumnStyles.Add(colC);







var query = from i in myList
            select new
            {
                i.FieldA,
                i.FieldB,
                i.FieldC
            };

myBindingSource.DataSource = query.ToList(); // Thanks Marc Gravell

// wasn't sure what else to pass in here, but null worked.
myDataGridTableStyle.MappingName = myBindingSource.GetListName(null); 

myDataGrid.TableStyles.Clear(); // Recommended on MSDN in the code examples.
myDataGrid.TablesStyles.Add(myDataGridTableStyle);
myDataGrid.DataSource = myBindingSource;






所以基本上,DataGridTableStyle.MappingName需要知道什么类型的对象是映射。由于我的对象是一个匿名类型(使用LINQ创建),我不知道它是什么,直到运行时。之后,我匿名类型的列表绑定到绑定源,我可以使用BindingSource.GetListName(空)来获得匿名类型的字符串表示。

有一点要注意。如果我只是约束myList中(这是输入的myType)直接向绑定源,我可以只用字符串的myType作为DataGridTableStyle.MappingName值。

One thing to note. If I just bound the myList (which is type "myType") directly to the binding source, I could have just used the string "myType" as the value for DataGridTableStyle.MappingName.

希望这是给别人有用!

这篇关于你如何从绑定到一个列表℃的绑定源的正确映射名称; T&GT ;,或匿名类型,在一个使用的DataGridTableStyle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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