Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This code works
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: 

How to solve?

What I have tried:

Tried to extend win form gridview class with rad grid view but doesn't work.
Posted
Updated 24-Sep-18 20:26pm

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900