Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Changing dataview does not reflect in datagridview

$
0
0

If this is in the wrong section, please move. Language C#.

I'm reading data from a csv file into a datatable. Next I base a dataview based on that table and use the dataview as a datasource for a datagridview.

This works (see setupAndShow() in the second code block below); distinct flag set to true) in the second code block.

Next I create a new dataview via setupAndShow() based on the same table; the number of columns in this dataview is larger as the distinct flag is now false. And this dataview is then used as the datasource of the datagridview.

        /// <summary>
        /// sort and filter the data in the datatable
        /// </summary>
        /// <param name="tbl">datatable with missing assets</param>
        /// <param name="strSort">sort sstring</param>
        /// <param name="strFilter">filter string</param>
        /// <param name="fDistinct">distinct flag</param>
        /// <returns>sorted/filtered data view</returns>
        private DataView sortAndFilter(DataTable tbl, string strSort, string strFilter, bool fDistinct)
        {
            try
            {
                DataView dv = new DataView(tbl);
                dv.Sort = strSort;
                dv.RowFilter = strFilter;

                if (fDistinct == true)
                {
                    DataTable dt = dv.ToTable(true, new String[] { "Asset", "Channel", "Date", "Comment" });
                    dv = new DataView(dt);
                }

                return dv;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sort/filter error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

void setupAndShow(DataTable tbl) { //dgvMissing.Columns.Clear(); //dgvMissing.DataSource = null; DataView dv; // create dataview dv = sortAndFilter(tbl, g_strSort, g_strFilter + g_strIgnoreFilter, g_fDistinct); if (dv == null) return; dgvMissing.DataSource = dv; <---- INSPECTED AFTER THIS STATEMENT //dgvMissing.Update(); //dgvMissing.Refresh();

...
...

On inspection (quickwatch), dv in the last codeblock reflects the new data. However dgvMissing does not after assigning dv to its datasource.

The lines that are commented out are attempts to get is working and did not seem to make a difference (except that they prevented an exception later on in the code).

What am I missing?

Thanks for reading and advise.





Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>