none
An exception throws when DataGridView column header clicked to sort after the filtering applied to DataGridView RRS feed

  • Question

  • Hi team,

    I am facing an issue whereby when I click the DataGridView column header to sort, the application throws this exception: HResult=0x80070057 Message=Object must be of type String, plz be aware that there are blank or null values in the corresponding cells. However, there are blank or null values in the corresponding cells before filtering and there was no issues to sort by click the column header.

    I have a DataGridView and ComboBox controls on my form, and once the data loaded into the DataGridView (plz be aware that the data is loaded from database hence all the columns are auto binding to the DataGridView), the ComboBox control is epopulated and enabled and once a value selected in the ComboBox control, the DataGridView will be filtered based on the selected value in ComboBox.

    This is the code to filter the DataGridView based on the selected value in ComboBox:

    public static void FilerDataGridView(DataTable dt, string filterColumnName, string selectedItem, DataGridView dgv, int numberOfCells)
            {
                try
                {
                    dgv.DataSource = null;
                    dgv.Rows.Clear();
    
                    DataView dv = dt.DefaultView;
                    if (dgv.Columns.Count == 0)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            dgv.Columns.Add(dc.ColumnName, dc.ColumnName);
                        }
                    }
                    
                    if (selectedItem.ToLower() != "all")
                    {
                        dv.RowFilter = string.Format("" + filterColumnName + " = '{0}'", selectedItem);
                        if (dv.Count > 0)
                        {
                            dgv.Rows.Add(dv.Count);
                            for (int j = 0; j < numberOfCells; j++)
                            { 
                                for (int i = 0; i < dv.Count; i++)
                                {
                                    dgv.Rows[i].Cells[j].Value = dv[i].Row.ItemArray[j];
                                }
                            }
                        }
                    }
                    else
                    {
                        dgv.Rows.Add(dt.Rows.Count);
                        for (int j = 0; j < numberOfCells; j++)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                dgv.Rows[i].Cells[j].Value = dt.Rows[i][j];
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    The funny thing was i dont have any event handler of the DataGridView ColumnHeader click and i cant debug the code and the exception was directly thrown at the enter point of the application: 

    Application.Run(new frmMain());
     

    Could someone plz in here shed me a light on this.

    Many thanks.

    Andie.

    Friday, December 6, 2019 12:54 AM

Answers

  • issue has been fixed.

    as per my initially suspect the issue is in filtering code.

    there is empty/blank values when filtering applied in this piece of code:

    dgv.Rows[i].Cells[j].Value = dv[i].Row.ItemArray[j];
    because one of the right side (dataview) returns this value {}, and consequently it assigned the same value to the DataGridView, and as {} is not a blank string, hence when Data Grid View column header clicked, it thrown above exception, the fix is very straight forward, we just need to convert {} to a blank string, as per below code 
    dgv.Rows[i].Cells[j].Value = dv[i].Row.ItemArray[j].ToString();


     

    • Marked as answer by AndieDu Friday, December 6, 2019 4:32 AM
    Friday, December 6, 2019 4:32 AM

All replies

  • Hello,

    Can you sort properly without the filter?

    "dont have any event handler of the DataGridView ColumnHeader click", sorting is based in the data source of the DataGridView which leads me to see this as a data issue.

    You can subscribe to AppDomain.UnhandledException event to see if the error message is of assistance.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, December 6, 2019 3:50 AM
    Moderator
  • hi Karen, thanks to reply.

    there was no issues to sort without filtering. initially i thought it was the data issue, but  sorting is perfected fine without filtering, there must be something wrong when filter applied.

    Friday, December 6, 2019 4:11 AM
  • issue has been fixed.

    as per my initially suspect the issue is in filtering code.

    there is empty/blank values when filtering applied in this piece of code:

    dgv.Rows[i].Cells[j].Value = dv[i].Row.ItemArray[j];
    because one of the right side (dataview) returns this value {}, and consequently it assigned the same value to the DataGridView, and as {} is not a blank string, hence when Data Grid View column header clicked, it thrown above exception, the fix is very straight forward, we just need to convert {} to a blank string, as per below code 
    dgv.Rows[i].Cells[j].Value = dv[i].Row.ItemArray[j].ToString();


     

    • Marked as answer by AndieDu Friday, December 6, 2019 4:32 AM
    Friday, December 6, 2019 4:32 AM
  • Hi, I just disabled sorting of clicking headers. Things get easier with that being sacrificed.
    Friday, December 6, 2019 8:24 AM