如题VS2008 C# datagridview控件在用线程动态给它加一行数据后,点击一个空列或只有一条数据的列,会出现以下异常:异常:“对象必须是string。”或“对象必须是int32。”或 "缺少icompare对象" 例:
   datagridview1控件有以下数据
   
   编号     姓名    学号     //控件列标头
    1      无名1     
    2      无名2    
           无名3    
    4      无名4            //这一行是线程运行后加进来的
   datagridview1增加的数据,都是以一个String[]数组加进去的!
    string[] st = new string[3]   
    st[0] = "4";   
    st[1] = "无名4";   
    st[2] = "";
    datagridview1.Rows.Add(st);
在线程运行后添加了第4行数据时,点击学号列头排序,就会出现“缺少icompare对象。”异常,然后在点编号列头会出现“对象必须是string。”或“对象必须是int32。”我在网上找了很长时间,始终没有明确的解决方法!希望有哪位高人能够帮我解决,本人万分感谢!

解决方案 »

  1.   

    你的类型不匹配吧
    那你用强制转换啊,
    对象必须是String型就在你的对象后面加“ToString();”
    必须是int32 ---  Convter.ToInt32(你的对象);
      

  2.   

    对象必须是string。”或“对象必须是int32。”或 "缺少icompare对象"  
    这个异常信息已经很清楚了,你新加的列不能被排序。
      

  3.   


    using System;
    using System.ComponentModel;
    using System.Windows.Forms;class Form1 : Form
    {
        private Button sortButton = new Button();
        private DataGridView dataGridView1 = new DataGridView();    // Initializes the form.
        // You can replace this code with designer-generated code.
        public Form1()
        {
            dataGridView1.Dock = DockStyle.Fill;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.SelectionMode =
                DataGridViewSelectionMode.ColumnHeaderSelect;
            dataGridView1.MultiSelect = false;        sortButton.Dock = DockStyle.Bottom;
            sortButton.Text = "Sort";        Controls.Add(dataGridView1);
            Controls.Add(sortButton);
            Text = "DataGridView programmatic sort demo";
        }    // Establishes the main entry point for the application.
        [STAThreadAttribute()]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }    // Populates the DataGridView.
        // Replace this with your own code to populate the DataGridView.
        public void PopulateDataGridView()
        {
            // Add columns to the DataGridView.
            dataGridView1.ColumnCount = 2;
            dataGridView1.Columns[0].HeaderText = "Last Name";
            dataGridView1.Columns[1].HeaderText = "City";        // Populate the DataGridView.
            dataGridView1.Rows.Add(new string[] { "Parker", "Seattle" });
            dataGridView1.Rows.Add(new string[] { "Watson", "Seattle" });
            dataGridView1.Rows.Add(new string[] { "Osborn", "New York" });
            dataGridView1.Rows.Add(new string[] { "Jameson", "New York" });
            dataGridView1.Rows.Add(new string[] { "Brock", "New Jersey" });
        }    protected override void OnLoad(EventArgs e)
        {
            sortButton.Click += new EventHandler(sortButton_Click);        PopulateDataGridView();
            base.OnLoad(e);
        }    private void sortButton_Click(object sender, System.EventArgs e)
        {
            // Check which column is selected, otherwise set NewColumn to null.
            DataGridViewColumn newColumn =
                dataGridView1.Columns.GetColumnCount(
                DataGridViewElementStates.Selected) == 1 ?
                dataGridView1.SelectedColumns[0] : null;        DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
            ListSortDirection direction;        // If oldColumn is null, then the DataGridView is not currently sorted.
            if (oldColumn != null)
            {
                // Sort the same column again, reversing the SortOrder.
                if (oldColumn == newColumn &&
                    dataGridView1.SortOrder == SortOrder.Ascending)
                {
                    direction = ListSortDirection.Descending;
                }
                else
                {
                    // Sort a new column and remove the old SortGlyph.
                    direction = ListSortDirection.Ascending;
                    oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
                }
            }
            else
            {
                direction = ListSortDirection.Ascending;
            }        // If no column has been selected, display an error dialog  box.
            if (newColumn == null)
            {
                MessageBox.Show("Select a single column and try again.",
                    "Error: Invalid Selection", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            else
            {
                dataGridView1.Sort(newColumn, direction);
                newColumn.HeaderCell.SortGlyphDirection =
                    direction == ListSortDirection.Ascending ?
                    SortOrder.Ascending : SortOrder.Descending;
            }
        }
    }//http://msdn.microsoft.com/zh-cn/library/ms171608(VS.80).aspx
      

  4.   


    我的意思是,点击datagridview控件列标头的排序功能,控件自带的!
    就是2楼说的那个情况!
      

  5.   


    类型很清楚了,都是String类型的!
      

  6.   


    试了啊,按你给的那几个数据,没出错。
      string[] st = new string[3]   
      st[0] = "4";   
      st[1] = "无名4";   
      st[2] = "";
      datagridview1.Rows.Add(st);
      在这个基础上加了一个列头   没出错