[Mono-winforms-list] problems with DataGridView and DataTable binding

Mark Traudt mtraudt at quantifisolutions.com
Sun Feb 25 19:04:18 EST 2007


The example below works with .NET but not with Mono (mono-1.2.3.2007023 on openSUSE 10.2).  I encountered the following issues when using Mono:

1- I do not see any column headers when the form is first displayed.

2- The view is not refreshed automatically when I add rows by clicking the button (I have to force a repaint).

3- When I switch data sources, it does not seem to clear the column headers from the previous data source.  Instead, it seems to add the columns for my current data source.  So I end up with these column headers to the right that have no data associated with them.

4- If I have more than one row, then the scrollbar at the bottom flickers.

I investigated the first problem in more detail.  In DataGridView.cs, the DataSource setter calls BindIList, which can only display column headers if the list is not empty.  I am not an expert in winforms, but it seems to me that this method needs specialized logic to bind DataTable values, in order to take advantage of the DataColumn metadata.


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

public class OneOffApp 
{
  static void Main() 
  {
    Application.Run( new OneOffForm() );
  }
}

public class OneOffForm : Form
{
  private DataTable dt1;
  private DataTable dt2;
  private DataGridView grid;
  private Button button;

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

    dt2 = new DataTable();
    dt2.Columns.Add("Date2", typeof(DateTime));
    dt2.Columns.Add("Event2", typeof(string));

    button = new Button();
    button.FlatStyle = FlatStyle.System;
    button.Click += new EventHandler(button_Click);
    button.Location = new Point(8, 8);
    button.Name = "Switch";
    button.Text = "Switch";
    Controls.Add(button);

    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(8, button.Height * 2);
    grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    grid.DataSource = dt1;
    Controls.Add(grid);

    Show();
  }

  private void button_Click(object sender, System.EventArgs e)
  {
    DataTable dt = (grid.DataSource == dt1)? dt2 : dt1;

    DataRow r = dt.NewRow();
    r[0] = DateTime.Now;
    r[1] = "Button.Click";
    dt.Rows.Add(r);

    grid.DataSource = dt;
  }
}



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-winforms-list/attachments/20070225/93fe4f8c/attachment.html 


More information about the Mono-winforms-list mailing list