Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2007

Tip: Looking for answers? Try searching our database.

OutOfMemoryException when using a datagridview in a panel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sorensen.christoffer@gmail.com - 14 Mar 2007 09:55 GMT
Hi,

I am using .NET 2.0 and a datagridview in panel.

The datagridview is bound to a datatable which is populated through a
database. The first column should be a combobox column. The combobox
column has a datasource which is another datatable so an integer is
mapped to a string (easier for the user to understand).

This works fine in a standalone project. Code follows here:

private void Form1_Load(object sender, EventArgs e)
   {
     DataTable table = new DataTable("test");
     AddColumns(table);
     DataSet set = new DataSet();
     set.Tables.Add(table);
     SetupDataGridView(dataGridView1);
     AddData(set);
     BindingSource source = new BindingSource();
     if(null != table && table.DataSet.Tables.Count > 0)
       source.DataSource = table.DataSet.Tables[0];
     dataGridView1.DataSource = source;
     dataGridView1.AutoResizeColumns();
     dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
   }
   private static void AddData(DataSet set)
   {
     Insert(set);
   }
   private static void Insert(DataSet set)
   {
     DataRow row = set.Tables[0].NewRow();
     row["Type"] = 0;
     row["Key"] = "test";
     row["Value"] = "val";
     set.Tables[0].Rows.Add(row);

   }
   public static void SetupDataGridView(DataGridView datagridview)
   {
     if(null == datagridview)
       return;
     datagridview.AutoGenerateColumns = false;
     datagridview.Columns.Clear();
     AddComboColumn(datagridview);

     DataGridViewColumn keycolumn = new DataGridViewTextBoxColumn();
     keycolumn.DataPropertyName = "Key";
     keycolumn.Name = "Key";
     datagridview.Columns.Add(keycolumn);

     DataGridViewColumn valuecolumn = new
DataGridViewTextBoxColumn();
     valuecolumn.DataPropertyName = "Value";
     valuecolumn.Name = "Value";
     datagridview.Columns.Add(valuecolumn);

   }
   private static void AddColumns(DataTable table)
   {
     DataColumn column = new DataColumn("Type", typeof(Int32));
     column.AllowDBNull = false;
     column.Caption = "Type";
     column.DefaultValue = 0;
     table.Columns.Add(column);
     column = new DataColumn("Key", typeof(String));
     column.AllowDBNull = false;
     column.Caption = "Key";
     column.DefaultValue = "Key";
     table.Columns.Add(column);
     column = new DataColumn("Value", typeof(String));
     column.AllowDBNull = false;
     column.Caption = "Value";
     column.DefaultValue = "Value";
     table.Columns.Add(column);
     table.PrimaryKey = new DataColumn[] { table.Columns["Key"] };

   }
   private static void AddComboColumn(DataGridView datagridview)
   {
     DataGridViewComboBoxColumn combo = new
DataGridViewComboBoxColumn();
     DataTable table = new DataTable("valuetable");
     table.Columns.Add("type",typeof(Int32));
     table.Columns.Add("stringtype",typeof(String));
     DataRow row = table.NewRow();
     row["type"] = 0;
     row["stringtype"] = "Integer";
     table.Rows.Add(row);
     row = table.NewRow();
     row["type"] = 1;
     row["stringtype"] = "String";
     table.Rows.Add(row);
     row = table.NewRow();
     row["type"] = 2;
     row["stringtype"] = "Binary";
     table.Rows.Add(row);
     combo.DataSource = table;
     combo.ValueMember = "type";
     combo.DisplayMember = "stringtype";
     combo.DataPropertyName = "Type";
     combo.Name = "Type";
     datagridview.Columns.Add(combo);
   }

However in my other project it throws a out of memory exception:

{"Error creating window handle."}

{"Object reference not set to an instance of an object."}

"   at
System.Windows.Forms.DataGridView.CanValidateDataBoundDataGridViewCell(DataGridViewCell
dataGridViewCurrentCell)\r\n   at
System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell&
dataGridViewCurrentCell, DataGridViewDataErrorContexts context,
DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave,
Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter,
Boolean fireLeave)\r\n   at
System.Windows.Forms.DataGridView.EndEdit(DataGridViewDataErrorContexts
context, DataGridViewValidateCellInternal validateCell, Boolean
fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean
fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean
resetCurrentCell, Boolean resetAnchorCell)\r\n   at
System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean
validateCurrentCell, Boolean throughMouseClick)\r\n   at
System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell
value)\r\n   at
System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)\r
\n   at
System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)\r
\n   at
System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)\r
\n   at
System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)\r
\n   at
System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)\r
\n   at
System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)\r
\n   at
System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
\r\n   at
System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)\r
\n   at
System.Windows.Forms.Control.set_BindingContextInternal(BindingContext
value)\r\n   at
System.Windows.Forms.ContainerControl.set_BindingContext(BindingContext
value)\r\n   at
System.Windows.Forms.ContainerControl.get_BindingContext()\r\n   at
System.Windows.Forms.Control.get_BindingContextInternal()\r\n   at
System.Windows.Forms.ContainerControl.get_BindingContext()\r\n   at
System.Windows.Forms.Control.get_BindingContextInternal()\r\n   at
System.Windows.Forms.Control.get_BindingContext()\r\n   at
System.Windows.Forms.Control.get_BindingContextInternal()\r\n   at
System.Windows.Forms.Control.get_BindingContext()\r\n   at
System.Windows.Forms.Control.get_BindingContextInternal()\r\n   at
System.Windows.Forms.Control.get_BindingContext()\r\n   at
System.Windows.Forms.Control.get_BindingContextInternal()\r\n   at
System.Windows.Forms.Control.get_BindingContext()\r\n   at
System.Windows.Forms.Control.get_BindingContextInternal()\r\n   at
System.Windows.Forms.Control.get_BindingContext()\r\n   at
System.Windows.Forms.Control.get_BindingContextInternal()\r\n   at
System.Windows.Forms.Control.get_BindingContext()\r\n   at
System.Windows.Forms.DataGridViewComboBoxCell.GetDataManager(DataGridView
dataGridView)\r\n   at
System.Windows.Forms.DataGridViewComboBoxCell.InitializeDisplayMemberPropertyDescriptor(String
displayMember)\r\n   at
System.Windows.Forms.DataGridViewComboBoxCell.OnDataGridViewChanged()\r
\n   at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32
index)\r\n   at
System.Windows.Forms.DataGridView.SetSelectedCellCore(Int32
columnIndex, Int32 rowIndex, Boolean selected)\r\n   at
System.Windows.Forms.DataGridView.SetSelectedElementCore(Int32
columnIndex, Int32 rowIndex, Boolean selected)\r\n   at
System.Windows.Forms.DataGridView.ClearSelection(Int32
columnIndexException, Int32 rowIndexException, Boolean
selectExceptionElement)\r\n   at
System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean
validateCurrentCell, Boolean throughMouseClick, Boolean
clearSelection, Boolean forceCurrentCellSelection)\r\n   at
System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean
includeNewRow)\r\n   at
System.Windows.Forms.DataGridView.OnHandleCreated(EventArgs e)\r\n
at System.Windows.Forms.Control.WmCreate(Message& m)\r\n   at
System.Windows.Forms.Control.WndProc(Message& m)\r\n   at
System.Windows.Forms.DataGridView.WndProc(Message& m)\r\n   at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
\r\n   at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r
\n   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n   at
System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32
dwExStyle, String lpszClassName, String lpszWindowName, Int32 style,
Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent,
HandleRef hMenu, HandleRef hInst, Object pvParam)\r\n   at
System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32
dwExStyle, String lpszClassName, String lpszWindowName, Int32 style,
Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent,
HandleRef hMenu, HandleRef hInst, Object pvParam)\r\n   at
System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)"

The panel with the datagridview is added to another panel
(panel.controls.add(panel2)).

Does anybody know what is going on here?

Regards,

Chris
sorensen.christoffer@gmail.com - 14 Mar 2007 10:06 GMT
On 14 Mar., 09:55, sorensen.christof...@gmail.com wrote:
> Hi,
>
[quoted text clipped - 5 lines]
> mapped to a string (easier for the user to understand).
> Does anybody know what is going on here?

I have found out that if I show the dialog which has the panel that
holds the datagridview, close it and add the panel to the another
form, it shows fine without any exception.

/Chris

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.