简体   繁体   English

来自对象属性值的DataGridView列名称?

[英]DataGridView column names from object property values?

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. 我有一个带有标签条的winform。 Tabs can be added dynamically and are named the same as the TabName property. 可以动态添加选项卡,并且它们的名称与TabName属性相同。 Each tab contains a DataGridView that is also named the same as the TabName property. 每个选项卡都包含一个DataGridView,该名称也与TabName属性相同。

So, each tab/gridview should display only the data from the correct objects (the foo tab only shows foo people in the grid, etc). 因此,每个选项卡/网格视图应仅显示来自正确对象的数据(foo选项卡仅显示网格中的foo人,等等)。 I would like the column name to be the values for the ColumnName in each object (eg 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)中ColumnName的值,并且我希望RowOrder是该名称出现在其中的行号。所以我正在寻找看起来像的输出如下所示:

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. 对于Winforms编程,我相对较新-搜索给我留下了更多有关最佳方法的疑问。 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. 我设置了与对象列表的通用数据绑定,但这给我留下了以每个属性(ColumnName,RowOrder等)命名的列,并且我不会提及这不能解决我的每个tab / datagrid仅显示该数据的问题。

Is a linq expression a good way to go about putting together each DataGridView? linq表达式是将每个DataGridView放在一起的好方法吗? If so, how would I craft an expression that changes the column names to the values in ColumnName? 如果是这样,我将如何制作一个将列名更改为ColumnName中的值的表达式? Can I order by RowOrder to get each row correct leaving the blank spaces? 我可以按RowOrder排序以使每行正确保留空白吗?

Would I be better off if I just create additional object lists to populate each tab/grid and do the sorting/stacking manually? 如果我仅创建其他对象列表来填充每个选项卡/网格并手动进行排序/堆叠,我会更好吗?

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. 认为在选项卡包含表包含行包含名称。

private void AddRow(DataTable dt, params string[] names)
{
    // Expand columns as required
    while (dt.Columns.Count < names.Length)
    {
        DataColumn col = dt.Columns.Add();
        col.ColumnName = dt.Columns.Count.ToString();
    }
    // Add new row
    DataRow row = dt.NewRow();
    for (int i = 0; i < names.Length; i++)
        row[i] = names[i];
    dt.Rows.Add(row);
}
private void AddToColumn(DataTable dt, int rowIdx, int colIdx, string name)
{
    while (dt.Columns.Count < colIdx)
    {
        DataColumn col = dt.Columns.Add();
        col.ColumnName = dt.Columns.Count.ToString();
    }
    if (dt.Rows.Count > 0)
    {
        while (dt.Rows[0].ItemArray.Length < colIdx)
            dt.Rows[0][dt.Rows[0].ItemArray.Length] = "";
    }
    DataRow row;
    if (rowIdx < dt.Rows.Count)
    {
        row = dt.Rows[rowIdx];
        row[colIdx - 1] = name;
    }
    else
    {
        row = dt.NewRow();
        row[colIdx - 1] = name;
        dt.Rows.Add(row);
    }
}
private void buttonStart_Click(object sender, EventArgs e)
{
    Dictionary<string, DataTable> workers = new Dictionary<string, DataTable>();

    DataTable dt = new DataTable();
    AddRow(dt, "Bob Jones", "Jane Jones", "Jim Jones");
    AddRow(dt, "Joe Jones", "", "John Jones");
    workers.Add("Foo", dt);

    AddToColumn(dt, 0, 4, "Testing"); // Use this if you have to add by column

    dt = new DataTable();
    AddRow(dt, "Worker Bee1",  "Worker Bee2");
    workers.Add("Bar", dt);

    string tabName = "Foo";
    dataGridView1.DataSource = workers.FirstOrDefault(x => x.Key == tabName).Value;
}

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

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