C# datagridviewrow выдает исключение, но datagridviewrow работает, когда я преобразую значение столбца checkbox в логическое значение
Этот код работает
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;
но это не работает и дает исключение
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;
Исключение дается в этой строке
bool isSelected = Convert.ToBoolean(row.Cells["s"].Value);
Это исключение
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:
Как решить эту проблему?
Что я уже пробовал:
Попытался расширить класс win form gridview с помощью rad grid view, но это не сработало.