Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

DataGridView Sorting Issue!

Posted on 2006-05-30
9
Medium Priority
?
2,107 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:salan_alani
  • 4
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16792154
1) I am not totally understanding your problem.

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

Bob

0
 
LVL 2

Author Comment

by:salan_alani
ID: 16792903
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.Source_CI,
            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
0
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 16795998
Hello Dear

Try this code
MyDataGridView.Rows.Add(new String[] {"1","2" });
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:salan_alani
ID: 16796160
MuhammedAdil,
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.

Thanks
0
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 16796334
Hello Dear

Show integer Array code
0
 
LVL 2

Author Comment

by:salan_alani
ID: 16796692
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
0
 
LVL 6

Accepted Solution

by:
MuhammadAdil earned 750 total points
ID: 16796950
Hello Dear

try to typecast int array to string array and then pass string array to datagridview
0
 
LVL 2

Author Comment

by:salan_alani
ID: 16797997
I got the following error:
Unable to cast object of type 'System.Object[]' to type 'System.String[]'.

Code:
object[] x = new object[] { 1, 2 };      // in fact, object[] x = GetSystemInfo(string FilePath);
string[] y;
y = (string[])x;      // Error goes in this line
MyDataGridView.Rows.Add(y);
0
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 16805017
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);
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question