GridView with source of unknown number of columns

38 views
Skip to first unread message

pub...@gmail.com

unread,
Sep 6, 2017, 10:23:17 PM9/6/17
to Eto.Forms
Hello,

Thank you for your time. I am currently working on a small personal project, and I am running into a bit of a road block that I feel probably has a simple solution that is just eluding me. I am attempting to show information from various CSV files in a grid view, however I do not know how many columns I will have prior to running. Thus far all of my attempts have come up short. I was originally attempting everything, even creating a ExpandoObject to attach attributes to then attempting to bind to those attributes without success, and attempting to convert everything to an array and use DelagateBindings to disastrous effect. It's leaving me feeling quite daft because it feels like something obvious I'm just plain missing. Since then I've tried everything I can think of and still do not have any real concrete idea of how to proceed, is there something I'm missing? Any input you can provide would be greatly appreciated.

pub...@gmail.com

unread,
Sep 7, 2017, 9:09:10 PM9/7/17
to Eto.Forms
For clarity, I thought I would include some of my early abortive attempts at this as well:
        public void UpdateResults(DataTable results)
        {
            //List<string[]> store = new List<string[]>();
            var cols = results.Columns;
            var rows = results.Rows;

            Columns.Clear();

            var store = results.Rows.Cast<DataRow>().ToList();

            for (var x = 0; x < cols.Count; x++)
            {
                Console.WriteLine("Adding column {0}", cols[x].ColumnName);
                var columnHeader = cols[x].ColumnName.ToString();
                var cell = new TextBoxCell();
                var columnItem = new GridColumn()
                {
                    //DataCell = new TextBoxCell(String.Format("[{0}]", x)),
                    DataCell = new TextBoxCell
                    {
                        Binding = Binding.Delegate<DataRow, string>(r => r[x].ToString())
                        //Binding = Binding.Property<List<DataRow>, string>(r => store[x].ToString())
                    },
                    HeaderText = cols[x].ColumnName
                };
                Columns.Add(columnItem);
            }
            Console.WriteLine(store[0][1]);
            DataStore = store;
        }

curtis

unread,
Sep 8, 2017, 12:52:16 PM9/8/17
to Eto.Forms
It looks like you're running into a lambda "issue" (see this for an example).  When you use a single variable 'x', then it will actually share that variable for each instance and usually have its final value instead of what you are thinking.

Try doing this instead:

var y = x;

.. then when setting up your binding, use 'y' instead:
Binding.Delegate<DataRow, string>(r => r[y].ToString())

Hope this helps!
Curtis.

pub...@gmail.com

unread,
Sep 8, 2017, 2:03:06 PM9/8/17
to Eto.Forms
Oh wow, that worked a trick. Thank you for pointing that out, that would have never occurred to me; making a locally scoped to the for loop variable holding the loop variable's value instantly corrected the issue.

Thank you again, you don't know how much I appreciate the assistance. Have a great day

curtis

unread,
Sep 8, 2017, 2:20:47 PM9/8/17
to Eto.Forms

You're very welcome!  I'm glad it worked out. (;
Reply all
Reply to author
Forward
0 new messages