OutOfMemoryException when using a datagridview in a panel

  • Thread starter sorensen.christoffer
  • Start date
S

sorensen.christoffer

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
 
S

sorensen.christoffer

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).
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top