DataGrid Error when typing F2 followed by ESC

  • Thread starter Thread starter CA
  • Start date Start date
C

CA

I am having a problem with a datagrid. Basically I have created a new
class derived from DataGridTextBoxColumn which only accepts numeric
values. When I enter a cell, type the F2 key followed by ESC, I get
the following 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.

I have posted the code below and have indicated where the error occurs
in the code, but I do not know DataGrid well enough to know why the
error occurs.

Thanks for taking the time to read this post. I would be grateful for
any help.

Thanks

Chris


---------------------------------------------------
// Code begins

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestGrid
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dg1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}


#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dg1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dg1)).BeginInit();
this.SuspendLayout();
//
// dg1
//
this.dg1.DataMember = "";
this.dg1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dg1.Location = new System.Drawing.Point(32, 24);
this.dg1.Name = "dg1";
this.dg1.Size = new System.Drawing.Size(176, 144);
this.dg1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.dg1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dg1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());

// Here is where the error occurs
}

private void InitDataGrid()
{
try
{
DataTable dt = new DataTable("TestTable");
dt.Columns.Add(new DataColumn("Column1", typeof(double)));
for (int i=0; i<5; i++)
dt.Rows.Add(new object[] {i});
dg1.DataSource = dt;

DataGridTableStyle styles = new DataGridTableStyle();
styles.MappingName = "TestTable";

DataGridNumericColumn FirstCol = new DataGridNumericColumn();
FirstCol.MappingName = "Column1";
FirstCol.Alignment = HorizontalAlignment.Right;

styles.GridColumnStyles.Add(FirstCol);
dg1.TableStyles.Add(styles);
}
catch
{
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
InitDataGrid();
}
}

public class DataGridNumericColumn : DataGridTextBoxColumn
{

protected override object GetColumnValueAtRow(CurrencyManager cm,
int RowNum)
{
object oVal = base.GetColumnValueAtRow(cm, RowNum);
try
{
double Temp = Convert.ToDouble(oVal);
return Temp.ToString("0.00%");
}
catch
{
return "";
}
}

protected override bool Commit(CurrencyManager cm, int RowNum)
{
this.HideEditBox();
DataGridTextBox box = (DataGridTextBox) this.TextBox;
double Value;
if (box.IsInEditOrNavigateMode)
{
return true;
}
if (TextBox.Text == this.NullText.ToString())
{
SetColumnValueAtRow(cm, RowNum, this.NullText.ToString());
}
try
{
Value = double.Parse(TextBox.Text);
}
catch
{
return false;
}
SetColumnValueAtRow(cm, RowNum, Value);
this.EndEdit();
return true;
}
}
}
 
I'm now performing research on the issue. I will update you as soon as
possible.

Best regards,

Duke Sun
Microsoft Online Partner Support
<MCSE/MCDBA/MCSD>

Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi,

http://www.i-syn.gmxhome.de/devcom/colstyles/textbox.htm

has a textbox implementation that can handle F2 ESC.
HTH,
Ulrich

CA said:
I am having a problem with a datagrid. Basically I have created a new
class derived from DataGridTextBoxColumn which only accepts numeric
values. When I enter a cell, type the F2 key followed by ESC, I get
the following 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.

I have posted the code below and have indicated where the error occurs
in the code, but I do not know DataGrid well enough to know why the
error occurs.

Thanks for taking the time to read this post. I would be grateful for
any help.

Thanks

Chris
....
 
Back
Top