0

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

Community
  • 1
  • 1
Skinner
  • 1,461
  • 4
  • 17
  • 27
  • Did you initialize your list of lists correctly? Because I just tested your code and got it worked on me. – Rizki Pratama Jan 31 '17 at 01:47
  • Thanks. I'm not sure. I started C# about a week ago and still getting the hang of it. I've added the entire class. – Skinner Jan 31 '17 at 01:56
  • I think the problem is from your list of lists initialization. I'm not sure what does `AllDrawingLists` contain. Check it to make sure you got them right. You can try by `Console.Write()` every item in your list, like `foreach (List listDat in dat) { foreach (string strDat in listDat) { Console.Write(strDat + " "); } Console.WriteLine(); }` – Rizki Pratama Jan 31 '17 at 02:05
  • 1
    I see that the problem occured because your file name in the header has a dot in it. See here why: http://stackoverflow.com/questions/2940618/what-is-it-about-datatable-column-names-with-dots-that-makes-them-unsuitable-for – Rizki Pratama Jan 31 '17 at 02:27
  • HA!!! Ill strip it out and see what happens. Thanks!!! – Skinner Jan 31 '17 at 02:34
  • You're welcome! Have you come up with a solution? – Rizki Pratama Jan 31 '17 at 03:06
  • Yes. It was the period. I've been drilling this for about 4 nights. I guess I learned a bit about datagrids and datatables in the process. Regards!!! – Skinner Jan 31 '17 at 03:17

1 Answers1

0

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;
    }
}
Rizki Pratama
  • 551
  • 4
  • 23