Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Custom button column style in datagrid causes error
 

Custom button column style in datagrid causes error

Hello All,

Newbie to C# (first post here as well), but I am familiar with java/perl/php.  

I was trying to get a handle on creating custom column styles in a datagrid.  So I downloaded and tried a simple example using the controls provided by this msdn article.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/datagridcolumnstyle2.asp

I used a very straight-forward test application with only a datagrid in an otherwise empty form.  I was trying to use the DataGridButtonColumn class.

The datagrid shows, however, when I scroll to the right to see the custom column style (the one with the buton) I get the following error message:

## START OF ERROR ##########################################

An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll

Additional information: Object reference not set to an instance of an object.

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at testcontrols.DataGridButtonColumn.DrawButton(Graphics g, Bitmap bm, Rectangle bounds, Int32 row) in c:\documents and settings\hp_administrator\my documents\visual studio projects\testapp\datagridbuttoncolumn.cs:line 81
   at testcontrols.DataGridButtonColumn.Paint(Graphics g, Rectangle bounds, CurrencyManager source, Int32 rowNum, Brush backBrush, Brush foreBrush, Boolean alignToRight) in c:\documents and settings\hp_administrator\my documents\visual studio projects\testapp\datagridbuttoncolumn.cs:line 159
   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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollBar.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)
  The program '[920] testApp.exe' has exited with code 0 (0x0).
 at testApp.Form1.Main() in c:\documents and settings\hp_administrator\my documents\visual studio projects\testapp\form1.cs:line 95

## END OF ERROR ##########################################

This is very strange because if I remove the code for the button column style, and refer to a date column and use the DataGridTimePicker class I do not get the same error it only seems to stem from the button column style.

Please except my apologies if this has been answered in another post.  (if so, please direct me to that post).  

I would be happy to paste the code.  But truly it is a just a Windows Form with a datagrid, and a datbase connection, with an applied column style - DataGridButtonColumn class.  At first I thought maybe it was the column type (it is an int) but then I tried it with a string column and got the same result.  

With no styles applied the datagrid populates fine - all columns, all rows.

Any information would be much appreciated.
MigrationUser 1  Sunday, June 12, 2005 3:40 PM
Never mind.  Looking more closely at the error I was able to figure out the issue.  Turns out it was the loading of the Bitmap that was causing this.  Once I entered the correct assembly name the code was able to determine the location of the bitmap and all is happy.
MigrationUser 1  Monday, June 13, 2005 7:11 AM

You can use google to search for other answers

Custom Search

More Threads

• endcurrentedit on currency manager clears textbox
• question about custom cell types in DataGridView
• How to Programmatically Secure Connnection String Password?
• Forms which use same gradient set up as MainForm in Project
• DataGridView MouseWheel: three separate scroll msgs per detent click causing very sluggish scrolling
• Problem detecting changes on a parent-child bindingsource
• trying to insert a row into a table
• Serial Port Read
• How to bind the textbox in windows forms with the column in dataset?
• Databindings