代码太乱,给整理了一下。
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
    if((this.dataGrid1.CurrentRowIndex>=0)&&(this.dataGrid1.CurrentRowIndex<this.ThisDataSet.Tables["CDResource_view"].Rows.Count))
   {
     if(!Convert.IsDBNull(this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0]))
    {
long SelectID=Convert.ToInt32(this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0]);
//MessageBox.Show(SelectID.ToString());
this.oleDbSelectCommand2.CommandText = "SELECT ID, RentAuthName, RentCDID, RentCDNumber, RentDayCount, RentDeposit, RentR" +
"e, RentTime, ReturnState, ReturnTime FROM RentAdmin where RentCDID="+SelectID; this.ThisDataSet.Tables["RentAdmin"].Clear();
this.oleDbDataAdapter2.Fill(this.ThisDataSet,0,0,"RentAdmin");
}
}

解决方案 »

  1.   

    你出问题时,this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0])的内容有没有错?
      

  2.   

    我用到了两个DataGrid:  DataGrid1  和DataGrid2。
          DataGrid1指向表“CDResource_view”   DataGrid2指向表:"RentAdmin"
          DataGrid1中的行改变时可以在DataGrid2中显示DataGrid1中的当前行的数据的详细数据(放在另外的RentAdmin表中)。   上面最后少写了个}   各位高手帮帮忙了,  谢谢。
      

  3.   

    可是,如果MessageBox.Show(SelectID.ToString());的注释去掉,就不会出现问题,如果加上注释就出现问题,那么事情就很诡异。
      

  4.   

    你出问题时,this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0])的内容有没有错?   内容没有错,内容是一个ID号,在数据库中是“长整型”,主键。
       MessageBox.Show(this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString())后其值是:11或2.
         如果:
        我在DataGrid1中选择的是第2行。(其中第2行和第6行有详细数据可以显示在DataGrid2中,其他行没有。)
        在我在DataGrid2中选中了某行(this.dataGrid2[dataGrid2.CurrentCell.RowNumber,1])的检测框后,在回来用鼠标点击DataGrid1中第2行不会有错误发生,但是如果直接选择的是第6行就会有上面的错误发生,如果选择其他的第1、3、4、5等行暂时没有错误,但是系统反应很忙,等在去选择第二和第6行时就有出现了上面的错误。
      

  5.   

    long SelectID=Convert.ToInt32(this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0]);
    问题出在这里,你的转换无效。
      

  6.   

    TO:回复人: trnbo(【没有蛀牙】→闭关修行)
    long SelectID=Convert.ToInt32(this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0]);
    问题出在这里,你的转换无效。
       
         可是我改成 ing SelectID=也不行啊,   是要Convert.ToInt64吗?
         可是我改成:
    long SelectID=Convert.ToInt64(this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0]);
    也不行啊,还是一样的错误。
      

  7.   

    long SelectID = long.Parse( this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0] == null? "0":this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString());
      

  8.   

    如果还不行那说明你的this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString() == ""或非数字
      

  9.   

    回复人: dreammaster(天涯)非常感谢你的帮助,真的。
        我按你的long SelectID = long.Parse( this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0] == null? "0":this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString());
    试了还是同样的问题。
         我在long SelectID = long.Parse( this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0] == null? "0":this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString());
    后面加了
    MessageBox.Show(SelectID.ToString());后看到SelectID的值是11没有错误的啊。
    而且还有一个奇怪的问题,当我把MessageBox.Show(SelectID.ToString());加在其后时程序运行正常,去掉后就有出现了同样的问题。
        真的很怪,我怎么也想不明白。
    是不是加上MessageBox.Show(SelectID.ToString());后让程序暂是缓了一下就不出现问题了。
    哎,不明白。
      

  10.   

    你应该修改一下你的SQL语句试试,把long 转换成string  -->SelectID.ToString();
    this.oleDbSelectCommand2.CommandText = "SELECT ID, RentAuthName, RentCDID, RentCDNumber, RentDayCount, RentDeposit, RentR" +
    "e, RentTime, ReturnState, ReturnTime FROM RentAdmin where RentCDID="+SelectID.ToString();
      

  11.   

    如果是上述转换的问题.那么我怀疑你是不是关闭了C#的编译警告部分功能.因为我觉得上面那段代码在C#里根本编译通不过.在vb.net里能通过,但是在运行时会报错,我是经常遇到这个问题.当然,如果不是这个错误,呵呵,怒我多嘴.
      

  12.   

    在调试状态下的输出窗口中是这样说的。“DefaultDomain”: 已加载“d:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll”,未加载符号。
    “CDZLAdmin”: 已加载“G:\prodction\CDAdmin\CDZLAdmin\bin\Debug\CDZLAdmin.exe”,符号已加载。
    “CDZLAdmin.exe”: 已加载“d:\windows\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll”,未加载符号。
    “CDZLAdmin.exe”: 已加载“d:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll”,未加载符号。
    “CDZLAdmin.exe”: 已加载“d:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll”,未加载符号。
    “CDZLAdmin.exe”: 已加载“d:\windows\assembly\gac\system.data\1.0.5000.0__b77a5c561934e089\system.data.dll”,未加载符号。
    “CDZLAdmin.exe”: 已加载“d:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll”,未加载符号。
    “CDZLAdmin.exe”: 已加载“d:\windows\assembly\gac\system.windows.forms.resources\1.0.5000.0_zh-chs_b77a5c561934e089\system.windows.forms.resources.dll”,未加载符号。
    “CDZLAdmin.exe”: 已加载“d:\windows\assembly\gac\mscorlib.resources\1.0.5000.0_zh-chs_b77a5c561934e089\mscorlib.resources.dll”,未加载符号。
    未处理的“System.InvalidCastException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 指定的转换无效。
    未处理的异常: System.InvalidCastException: 指定的转换无效。
       at System.Windows.Forms.DataGridBoolColumn.Paint(Graphics g, Rectangle bounds, CurrencyManager source, Int32 rowNum, Brush backBrush, Brush foreBrush, Boolean alignToRight)
       at System.Windows.Forms.DataGridRelationshipRow.PaintCellContents(Graphics g, Rectangle cellBounds, DataGridColumnStyle column, Brush backBr, Brush foreBrush, Boolean alignToRight)
       at System.Windows.Forms.DataGridRow.PaintData(Graphics g, Rectangle bounds, Int32 firstVisibleColumn, Int32 columnCount, Boolean alignToRight)
       at System.Windows.Forms.DataGridRelationshipRow.Paint(Graphics g, Rectangle bounds, Rectangle trueRowBounds, Int32 firstVisibleColumn, Int32 numVisibleColumns, Boolean alignToRight)
       at System.Windows.Forms.DataGrid.PaintRows(Graphics g, Rectangle& boundingRect)
       at System.Windows.Forms.DataGrid.PaintGrid(Graphics g, Rectangle gridBounds)
       at System.Windows.Forms.DataGrid.OnPaint(PaintEventArgs pe)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at CDZLAdmin.Login.Main() in g:\prodction\cdadmin\cdzladmin\login.cs:line 190程序“[2264] CDZLAdmin.exe”已退出,返回值为 0 (0x0)。
      

  13.   

    监视窗口上说:
    this.dataGrid1[dataGrid1.CurrentCell.RowNumber,0] 错误: 标识符“this”超出范围