开发者

DataGridView column names from object property values?

开发者 https://www.devze.com 2023-02-17 06:08 出处:网络
public class Employee { public int ColumnName {set; get;} public int RowOrder {set; get;} public string TabName {set; get;}
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 o开发者_Python百科ff 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号