我现在用datagridview绑定了数据库中的一张表
在datagridview中选中某行后,我要把这一行中的数据分别取出,然后赋值给几个textbox
可是除一开始赋值的某列以后的列都不能赋值,这是为什么呢比如说
txtItem.Text = dgrFilter.CurrentRow.Cells[1].Value.ToString();
txtPrice.Text =dgrFilter.CurrentRow.Cells[2].Value.tostring();
txtAmount.Text = dgrFilter.CurrentRow.Cells[3].Value.ToString();
这种情况下txtitem能够得到值
下面的textbox就出错了,应该怎么解决。

解决方案 »

  1.   

    报什么错呢.
    应该没问题的呀.
    txtPrice.Text =dgrFilter.CurrentRow.Cells[2].Value.tostring(); 
      

  2.   

    {"未将对象引用设置到对象的实例。"}出的就是这个错误,txtitem的值得到了
    如果,我只写txtItem.Text = dgrFilter.CurrentRow.Cells[1].Value.ToString(); 
    是没有错误的
    如果,后面再取其他列的值就出错了
      

  3.   

    dgrFilter.CurrentRow.Cells[1]
    那就说明 cell[2]
    ,没有!
      

  4.   

    cell 是从0开始,比如总共2列,就是0,1
    如果是3列就是0,1,2
      

  5.   

    首先要确认你取出来的表的列足够你所需要的(你的例子中最少要4列),再确定Cell[i].Value是不是null,如果是null就不用调用ToString()了,
    楼主最好把错误信息贴出来,看下报的是什么错。
      

  6.   

    cell[2]肯定是有的
    比如说
    txtItem.Text = dgrFilter.CurrentRow.Cells[1].Value.ToString(); 
    txtPrice.Text =dgrFilter.CurrentRow.Cells[2].Value.tostring(); 
    txtAmount.Text = dgrFilter.CurrentRow.Cells[3].Value.ToString(); 
    这三句,我顺便注释掉两句,这留一句,都能得到值,但是,如果只要是一句以上,就出这个错
      

  7.   

    未处理 System.NullReferenceException
      Message="未将对象引用设置到对象的实例。"
      Source="info"
      StackTrace:
           在 info.frmFee.dgrFilter_KeyPress(Object sender, KeyPressEventArgs e) 位置 E:\Soft Design\c#\info\info\frmFee.cs:行号 178
           在 System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e)
           在 System.Windows.Forms.DataGridView.OnKeyPress(KeyPressEventArgs e)
           在 System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
           在 System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
           在 System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
           在 System.Windows.Forms.Control.WmKeyChar(Message& m)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.DataGridView.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 info.Program.Main() 位置 E:\Soft Design\c#\info\info\Program.cs:行号 22
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      

  8.   

    你的列对应错了!从0开始的!
    还有你的第二个取值怎么是小写的“.tostring();”
      

  9.   

    .Cells[]是从0开始记数的。你要是就三个字段的话。cellS[3]是肯定不存在的,这样就出错了。
    三个字段的话应该是Cell[0] Cell[1] Cell[2]才对。
      

  10.   

     txtNumber.Text = this.dataGridView1["number", dataGridView1.CurrentRow.Index].Value.ToString();txtbednumber.Text = this.dataGridView1["bednumber", dataGridView1.CurrentRow.Index].Value.ToString();
    txtRe.Text = this.dataGridView1["Re", dataGridView1.CurrentRow.Index].Value.ToString();
      

  11.   


    我测试了一下
    问题很严重int intRowNumber;
    intRowNumber = dgrFilter.CurrentCell.RowIndex;
    txtItem.Text = dgrFilter[0, intRowNumber].Value.ToString();
    txtPrice.Text = dgrFilter[0, intRowNumber].Value.ToString();
    两个textbox都取用一个列值,这样应该不会出现列不存在的问题了吧
    如果像上面的代码这样,就出现如下的错误
    索引超出范围。必须为非负值并小于集合大小。
    参数名: index可是,如果我只要赋值这里随便注释掉一行,就是正常的
    为什么呢
    高手啊,告诉我啊
      

  12.   

    txtItem.Text = dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[1].Value.ToString();
    txtPrice.Text =dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[2].Value.tostring();
    txtAmount.Text = dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[3].Value.ToString(); 这样?
    是不是你读取的有空值?试试加上
    try
    {
    txtItem.Text = dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[1].Value.ToString();
    }
    cacth{}
      

  13.   

    找到一点原因了,我现在是用键盘选择datagridview的行
    比如说用上下箭头选定了以后按回车把值传递到textbox里面的如果,用鼠标,这些都是没有问题的
    用键盘就出现这个错误应该怎么处理呢