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

发布于 2024-10-22 08:51:17 字数 1380 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-10-29 08:51:17

首先,您需要重新组织数据结构。带有行号和列号的名称列表没有帮助。考虑选项卡包含表包含行包含名称。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文