for(int i=0;i<dataGridName.Items.Count;i++)改成foreach()循环试一下

解决方案 »

  1.   

    你可以试着把DataGrid中的行号设置为不显示,看一下。
    因为,我以前也遇到过这种情况。
    在设为不显示的情况下,它应该不会出问题,但我也知道,这并不是最根本的解决方法。
      

  2.   

    after a deletion, all the DataRows behind i will move up by one, tryfor(int i=dataGridName.Items.Count-1;i>=0;i--)
      

  3.   

    cocosoft(pengyun):DataGrid中的行号?指的是那一个?
      

  4.   

    删除后,你的Items.Count就减少了一,所以,你的循环应该重新开始吧!在rc[i].Delete()之后,应该再加上一句:--i;
    因为你是在for循环里面取的Items.Count,所以就不用count-1了。不过,这样效率很慢。我想应该这样:int count = dataGridName.Items.Count;for(int i=0;i<count;i++){
        //...
        rc[i].Delete();
        count--;
        --i;
    }
      

  5.   

    #region 得到数据行在当前绑定的数据源中的索引号
    /// <summary>
    /// 得到数据行在当前绑定的数据源中的索引号
    /// </summary>
    public int GetRowIndex(object PrimarID,DataSet ds,string TableName)
    {
    try
    {
    DataRowCollection rc = ds.Tables[TableName].Rows;
    DataRow row = rc.Find(PrimarID);
    if (row == null) return -1; DataViewManager dvm = new DataViewManager(ds);
    DataView dataView = dvm.CreateDataView(ds.Tables[TableName]);

    for(int i = 0; i < dataView.Count ; i++)
    {
    if (dataView[i].Row[1].ToString() == row[1].ToString()) return i;
    } return -1;
    }
    catch(Exception Ex)
    {
    MessageBox.Show("错误原因: "+Ex.Message.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
    return -1;
    }
    }
    #endregion