来自对象属性值的 DataGridView 列名称?
public class Employee
{
public int ColumnName {set; get;}
public int RowOrder {set; get;}
public string TabName {set; get;}
public string Names {set; get;}
}
BindingList<Employee> workers = new BindingList<Employee>();
workers.Add(new Employee(1, 0, "Foo", "Bob Jones"));
workers.Add(new Employee(2, 0, "Foo", "Jane Jones"));
workers.Add(new Employee(3, 0, "Foo", "Jim Jones"));
workers.Add(new Employee(1, 1, "Foo", "Joe Jones"));
workers.Add(new Employee(3, 1, "Foo", "John Jones"));
workers.Add(new Employee(1, 0, "Bar", "Worker Bee1"));
workers.Add(new Employee(2, 0, "Bar", "Worker Bee2"));
我有一个带有选项卡条的winform。选项卡可以动态添加,并且名称与 TabName 属性相同。每个选项卡都包含一个 DataGridView,其名称也与 TabName 属性相同。
因此,每个选项卡/gridview 应仅显示来自正确对象的数据(foo 选项卡仅显示网格中的 foo 人等)。我希望列名是每个对象中 ColumnName 的值(例如 1,2,3),并且我希望 RowOrder 是名称出现的行号。所以我正在寻找看起来像的输出如下所示:
1 2 3
==================================
Bob Jones Jane Jones Jim Jones
Joe Jones John Jones
如果网格坐标不包含值,则应将其留空。
我对 winforms 编程比较陌生——搜索给我留下了更多关于执行此操作的最佳方法的问题。我设置了到对象列表的通用数据绑定,但这给我留下了以每个属性(ColumnName、RowOrder 等)命名的列,我不会提到这并不能解决我的每个选项卡/数据网格仅显示数据。
linq 表达式是将每个 DataGridView 组合在一起的好方法吗?如果是这样,我将如何制作一个将列名称更改为 ColumnName 中的值的表达式?我可以按 RowOrder 排序以使每行正确并保留空格吗?
如果我只是创建额外的对象列表来填充每个选项卡/网格并手动进行排序/堆叠,效果会更好吗?
public class Employee
{
public int ColumnName {set; get;}
public int RowOrder {set; get;}
public string TabName {set; get;}
public string Names {set; get;}
}
BindingList<Employee> workers = new BindingList<Employee>();
workers.Add(new Employee(1, 0, "Foo", "Bob Jones"));
workers.Add(new Employee(2, 0, "Foo", "Jane Jones"));
workers.Add(new Employee(3, 0, "Foo", "Jim Jones"));
workers.Add(new Employee(1, 1, "Foo", "Joe Jones"));
workers.Add(new Employee(3, 1, "Foo", "John Jones"));
workers.Add(new Employee(1, 0, "Bar", "Worker Bee1"));
workers.Add(new Employee(2, 0, "Bar", "Worker Bee2"));
I have a winform with a tab strip. Tabs can be added dynamically and are named the same as the TabName property. Each tab contains a DataGridView that is also named the same as the TabName property.
So, each tab/gridview should display only the data from the correct objects (the foo tab only shows foo people in the grid, etc). I would like the column name to be the values for the ColumnName in each object (e.g. 1,2,3) and i want the RowOrder to be the row number that the name appears in. So I'm looking for output that would look like the following:
1 2 3
==================================
Bob Jones Jane Jones Jim Jones
Joe Jones John Jones
If the grid coordinates don't include a value, it should be left blank.
I'm relatively new to winforms programming -- searching left me with more questions about the best way to do this. I set up generic data binding to the object list, but that left me with columns named after each property (ColumnName, RowOrder, etc), and I won't mention that this doesn't solve my each tab/datagrid only shows that data.
Is a linq expression a good way to go about putting together each DataGridView? If so, how would I craft an expression that changes the column names to the values in ColumnName? Can I order by RowOrder to get each row correct leaving the blank spaces?
Would I be better off if I just create additional object lists to populate each tab/grid and do the sorting/stacking manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要重新组织数据结构。带有行号和列号的名称列表没有帮助。考虑选项卡包含表包含行包含名称。
In the first place, you will need to reorganize your data structures. A list of names with their row and column number don't help. Think in terms of tab contains table contains rows contains names.