DataGridView Sorting Issue!

Within my DataGridView control, the user could edit and fill the cells with any value and with any type (e.g. one cell is integer the other is string and so on). However, when the user click the header cell of that column that containing multiple types of object trying to sort it, I get the following exception:

Object must be of type Int16, or
Object must be of type string, .. (depends on the first cell value)

How to avoid this problem?
Who is Participating?
Hello Dear

try to typecast int array to string array and then pass string array to datagridview
Bob LearnedCommented:
1) I am not totally understanding your problem.

2) How are you adding values to the DataGridView?  Data-bound or unbound?  


salan_alaniAuthor Commented:
It is unbound DataGridView, here is the designer code of the DataGridView:

this.MyDataGridView = new System.Windows.Forms.DataGridView();
this.Source_CI = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Target_CI = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.MyDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Target_CI} );

but in the Form1_Load event, I am getting some data from a file and putting them in the DataGridView. So, assume the following code in the Form1_Load event:

private void Form1_Load(object sender, EventArgs e)
    MyDataGridView.AutoGenerateColumns = false;
    MyDataGridView.Rows.Add(new object[] { 1, 2 });

I think in this case DataGridView will assume that the columns type is Int16

Now run the application and add new row with string values, then try to sort, you will get an error
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Hello Dear

Try this code
MyDataGridView.Rows.Add(new String[] {"1","2" });
salan_alaniAuthor Commented:
In my previous comment, I said ASSUME that I have the below code:
MyDataGridView.Rows.Add(new object[] {1,2 });

Because actually, I don't use the above  command in my application, instead I am importing data from a file and get data by means of integer array.

Hello Dear

Show integer Array code
salan_alaniAuthor Commented:
It is delivered from 3rd parity control:

object[] GetSystemInfo(string FilePath);

and when I get the object array, I get it as an Integer Array, then in the Form1_Load event I paste this array in the first row of the DataGridView. But however, the user can then fill up the rest of the rows for whatever he/she likes, and this will cause a problem if the user fill a string value (for instance) and tried to sort that DataGridView control.

I think we should play around with the events that handle the sorting of the DataGridView control
salan_alaniAuthor Commented:
I got the following error:
Unable to cast object of type 'System.Object[]' to type 'System.String[]'.

object[] x = new object[] { 1, 2 };      // in fact, object[] x = GetSystemInfo(string FilePath);
string[] y;
y = (string[])x;      // Error goes in this line
Hello Dear

Initiate Object array in this way

Code snippet

Object[] obj= new String[2];    
                obj[0] = "1";
                obj[1] = "2";

// in fact, object[] obj = GetSystemInfo(string FilePath);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.