C#datagridviewrow抛出异常,但是当我将checkbox列值转换为boolean时,datagridviewrow会起作用 [英] C# datagridviewrow throws exception but datagridviewrow works when I convert checkbox column value to boolean

查看:83
本文介绍了C#datagridviewrow抛出异常,但是当我将checkbox列值转换为boolean时,datagridviewrow会起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码工作

foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
               if (isSelected)
               {
                   dt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value);
               }
           }
           dataGridView2.DataSource = dt;





但这不起作用并给出例外



but this doesn't work and gives exception

foreach (DataGridViewRow row in pGridView.Rows)
           {
               bool isSelected = Convert.ToBoolean(row.Cells["s"].Value);

               if (isSelected)
               {
                   dt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value);
               }
           }
           sGridView.DataSource = dt;



此行给出例外情况


Exception is given at this line

bool isSelected = Convert.ToBoolean(row.Cells["s"].Value);



这是例外




This is exception

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Ricardo
  StackTrace:
       at Ricardo.Books.radButton1_Click(Object sender, EventArgs e) in c:\Users\Suleman\Documents\Visual Studio 2012\Projects\Ricardo\Ricardo\POS.cs:line 308
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at Telerik.WinControls.RadControl.OnClick(EventArgs e)
       at Telerik.WinControls.UI.RadButtonBase.buttonElement_Click(Object sender, EventArgs e)
       at Telerik.WinControls.RadElement.OnClick(EventArgs e)
       at Telerik.WinControls.UI.RadButtonItem.OnClick(EventArgs e)
       at Telerik.WinControls.UI.RadButtonElement.OnClick(EventArgs e)
       at Telerik.WinControls.RadElement.DoClick(EventArgs e)
       at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.DoMouseUp(MouseEventArgs e)
       at Telerik.WinControls.RadElement.CallDoMouseUp(MouseEventArgs e)
       at Telerik.WinControls.ComponentInputBehavior.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.RadControl.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at Telerik.WinControls.RadControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.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.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Ricardo.Program.Main() in c:\Users\Suleman\Documents\Visual Studio 2012\Projects\Ricardo\Ricardo\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 



如何解决?



我的尝试:



尝试使用rad网格视图扩展win form gridview类但不起作用。


How to solve?

What I have tried:

Tried to extend win form gridview class with rad grid view but doesn't work.

推荐答案

您可以调试以检查 row.Cells [s] 为空。另外,检查您提供的列名是否有效。
You can debug to check if row and row.Cells["s"] are null. Also, check if the column name you are providing is a valid one.


这篇关于C#datagridviewrow抛出异常,但是当我将checkbox列值转换为boolean时,datagridviewrow会起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆