[Mono-bugs] [Bug 325979] New: NullReferenceException when looking up cell in DataGridViewCellCollection by column name

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Sep 18 08:11:33 EDT 2007


https://bugzilla.novell.com/show_bug.cgi?id=325979

           Summary: NullReferenceException when looking up cell in
                    DataGridViewCellCollection by column name
           Product: Mono: Class Libraries
           Version: unspecified
          Platform: x86
        OS/Version: openSUSE 10.2
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: Windows.Forms
        AssignedTo: mono-bugs at ximian.com
        ReportedBy: mtraudt at quantifisolutions.com
         QAContact: mono-bugs at ximian.com
          Found By: ---


If I populate a DataTable and then set it as the DataSource for a DataGridView,
and then attempt to lookup a cell in a DataGridViewCellCollection by column
name, I get a NullReferenceException.  This works fine with MS .NET.

I did some investigation and believe that the problem is that the OwningColumn
property of each DataGridViewCell is not being set when the DataTable is bound
to the DataGridView.

The following code sample can be used to reproduce:


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

public class SampleProgram
{
  static void Main() 
  {
    Application.Run( new TestForm() );
  }
}

public class TestForm : Form
{
  private DataGridView grid;
  private IContainer components;
  private ContextMenuStrip contextMenuStrip;
  private ToolStripMenuItem editMenuItem;

  public TestForm()
  {
    DataTable dt = new DataTable();
    dt.Columns.Add("Date", typeof(DateTime));
    dt.Columns.Add("Event", typeof(string));

    DataRow row;

    row = dt.NewRow();
    row["Date"] = DateTime.Now;
    row["Event"] = "one";
    dt.Rows.Add(row);

    row = dt.NewRow();
    row["Date"] = DateTime.Now;
    row["Event"] = "two";
    dt.Rows.Add(row);

    components = new Container();

    editMenuItem = new ToolStripMenuItem();
    editMenuItem.Enabled = false;
    editMenuItem.Name = "editMenuItem";
    editMenuItem.Size = new Size(162, 22);
    editMenuItem.Click += new EventHandler(this.editMenuItem_Click);
    editMenuItem.Text = "&Edit";

    contextMenuStrip = new ContextMenuStrip(this.components);
    contextMenuStrip.Items.AddRange(new ToolStripItem[] { this.editMenuItem }
);
    contextMenuStrip.Name = "contextMenuStrip";
    contextMenuStrip.Size = new Size(163, 170);
    contextMenuStrip.Opening += new
CancelEventHandler(this.contextMenuStrip_Opening);
    contextMenuStrip.ShowImageMargin = false;

    grid = new DataGridView();
    grid.MultiSelect = true;
    grid.RowHeadersVisible = false;
    grid.AllowUserToAddRows = false;
    grid.AllowUserToDeleteRows = false;
    grid.AllowUserToResizeRows = false;
    grid.ShowCellToolTips = false;
    grid.RowTemplate.Height = 18;
    grid.ReadOnly = true;
    grid.Location = new Point(0, 0);
    grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    grid.ContextMenuStrip = this.contextMenuStrip;
    grid.DataSource = dt;
    Controls.Add(grid);

    Show();
  }

  private void editMenuItem_Click(object sender, EventArgs e)
  {
    if (grid.SelectedRows.Count > 0)
    {
      DataGridViewCellCollection cells = grid.SelectedRows[0].Cells;

      MessageBox.Show( String.Format("{0}", cells["Event"].Value));
    }
  }

  private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
  {
    editMenuItem.Enabled = (grid.SelectedRows.Count > 0)? true: false;
  }
}


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list