简体   繁体   中英

WPF Datagrid C#

I know there are a bunch of questions on this in here and tons of information elsewhere. I cannot, for some reason, get this to work. Here is one of my starting points... Add entire row to DataTable at once using list

This is for a List of Lists. The very first List should be the column headers.

dat is a List<List<string>> that looks like:

{"index", "filename0", "filename1"},
{"A-100", "yes", "no"},
{"A-200", "no", "yes"}
etc...

Code:

    /// Dictionary containing as Key => FileName 
    /// as Value => All drawing numbers found in FileName
    Dictionary<string, List<string>> AllDrawingLists = new Dictionary<string, List<string>>();

    private void processGrid()
    {
        List<string> index = new List<string>();

        /// Build a comprehensive INDEX from the dictionary - A list
        /// of all the drawing numbers found in all the FIlenames
        foreach (KeyValuePair<string, List<string>> item in AllDrawingLists)
        {
            foreach (string dwg in item.Value)
            {
                if (index.Contains(dwg) == false)
                {
                    index.Add(dwg);                    }
            }
        }

        List<List<string>> dat = new List<List<string>>();
        List<String> headers = new List<string>();
        headers.Add("Index");

        foreach (KeyValuePair<string, List<string>> item in AllDrawingLists)
        {
            headers.Add(item.Key);
        }
        dat.Add(headers);


        foreach(string i in index)
        {
            List<string> row = new List<string>();
            row.Add(i);
            foreach(KeyValuePair<string, List<string>> item in AllDrawingLists)
            {
                string cell = "no";
                if (item.Value.Contains(i))
                {
                    cell = "yes";
                }
                row.Add(cell);
            }
            dat.Add(row);
        }

        dataGrid.Columns.Clear();
        DataTable dt = new DataTable();
        int ii = 0;
        foreach (List<string> row in dat)
        {

            if (ii == 0)
            {
                foreach(string t in row)
                {
                    dt.Columns.Add(t);
                }
                ii++;
            } else
            {
                dt.Rows.Add(row.ToArray<string>());
            }
        }
        dataGrid.ItemsSource = dt.AsDataView();
    }

My expected result would be :

|       |       |       |
| index | file1 | file2 |
-------------------------
| A-100 | yes   |  no   |
-------------------------
| A-200 | no    |  yes  |
-------------------------
| A-300 | yes   |  yes  |

but instead I get :

|       |       |       |
| index | file1 | file2 |
-------------------------
| A-100 |       |       |
-------------------------
| A-200 |       |       |
-------------------------
| A-300 |       |       |

The List of Lists is what I would expect, clearly its working for the definition of columns. I'm not sure why nothing goes into the DataGrid after the first column

Here is the output of dat. It is what I think Im looking for. All rows and column accounted for. Index C:\\py\\narrver2\\lists.txt C:\\py\\narrver2\\list2.docx A-1001 yes yes A-1002 yes yes A-1003 yes yes A-1004 no yes A-1005 no yes A-1006 no yes A-1007 no yes

In case you want to keep the dot in the header name, you can set the header column binding path with the name of the header surrounded by square brackets to make the special character (in this case, the dot notation) escaped.

You can do that inside an event handler that subscribes to event AutoGeneratingColumn of your DataGrid .

private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName.Contains('.') && e.Column is DataGridBoundColumn)
    {
        DataGridBoundColumn dataGridBoundColumn = e.Column as DataGridBoundColumn;
        dataGridBoundColumn.Binding = new Binding("[" + e.PropertyName + "]");
        dataGridBoundColumn.SortMemberPath = e.PropertyName;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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