+2008-05-20 Jonathan Pobst <monkey@jpobst.com>
+
+ * DataGridViewCellCollection.cs: Add a method to find the cell
+ with the given DataPropertyName.
+ * DataGridViewColumn.cs: Track if the column was autogenerated or not.
+ * DataGridViewColumnCollection.cs: Add a method to clear all
+ autogenerated columns.
+ * DataGridView.cs: If AutoGenerateColumns is false, don't autogenerate
+ columns.
+ [Fixes bug #348082]
+
2008-05-20 Jonathan Pobst <monkey@jpobst.com>
* DataGridView.cs: Don't try to update the RowTemplate with
}
private void BindIList (IList list) {
- // Stuff from a DataSet
- if (list is DataView) {
- DataView dataView = (DataView) list;
- DataTable table = dataView.Table;
-
- foreach (DataColumn dataColumn in table.Columns) {
- DataGridViewColumn col = CreateColumnByType (dataColumn.DataType);
-
- col.Name = dataColumn.ColumnName;
- col.DataPropertyName = dataColumn.ColumnName;
- col.SetIsDataBound (true);
- col.ValueType = dataColumn.DataType;
+ if (autoGenerateColumns) {
+ // Stuff from a DataSet
+ if (list is DataView) {
+ DataView dataView = (DataView) list;
+ DataTable table = dataView.Table;
+
+ foreach (DataColumn dataColumn in table.Columns) {
+ DataGridViewColumn col = CreateColumnByType (dataColumn.DataType);
+
+ col.Name = dataColumn.ColumnName;
+ col.DataPropertyName = dataColumn.ColumnName;
+ col.SetIsDataBound (true);
+ col.ValueType = dataColumn.DataType;
+ col.AutoGenerated = true;
+
+ columns.Add (col);
+ }
- columns.Add (col);
+ dataView.ListChanged += OnListChanged;
}
- dataView.ListChanged += OnListChanged;
- }
- else if (list.Count > 0) {
- DataGridViewCell template = new DataGridViewTextBoxCell();
- foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(list[0])) {
- DataGridViewColumn col = new DataGridViewColumn(template);
- col.Name = property.DisplayName;
- col.ReadOnly = property.IsReadOnly;
- columns.Add(col);
+ else if (list.Count > 0) {
+ DataGridViewCell template = new DataGridViewTextBoxCell();
+ foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(list[0])) {
+ DataGridViewColumn col = new DataGridViewColumn(template);
+ col.Name = property.DisplayName;
+ col.ReadOnly = property.IsReadOnly;
+ col.AutoGenerated = true;
+ columns.Add (col);
+ }
}
}
+
foreach (object element in list) {
+ // Don't add rows if there are no columns
+ if (ColumnCount == 0)
+ break;
+
DataGridViewRow row = (DataGridViewRow)RowTemplate.Clone ();
rows.InternalAdd (row);
// We do it this way because there may not be a column
// for every cell, ignore cells with no column
- DataGridViewCell cell = row.Cells[property.Name];
+ DataGridViewCell cell = row.Cells.GetBoundCell (property.Name);
if (cell == null)
continue;
private void ClearBinding ()
{
if (dataSource != null) {
- columns.Clear ();
+ columns.ClearAutoGeneratedColumns ();
rows.Clear ();
if (dataSource is DataView)
}
}
+ internal DataGridViewCell GetBoundCell (string dataPropertyName)
+ {
+ foreach (DataGridViewCell cell in base.List) {
+ if (string.Compare (cell.OwningColumn.DataPropertyName, dataPropertyName, true) == 0)
+ return cell;
+ }
+
+ return null;
+ }
+
public event CollectionChangeEventHandler CollectionChanged;
int IList.Add (object value)
[ToolboxItem ("")]
[DesignTimeVisible (false)]
public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
-
+ private bool auto_generated;
private DataGridViewAutoSizeColumnMode autoSizeMode;
private DataGridViewCell cellTemplate;
private ContextMenuStrip contextMenuStrip;
}
}
+ internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
internal bool HeaderTextSet { get { return headerTextSet; } }
[Browsable (false)]
List<DataGridViewColumn> result = new List<DataGridViewColumn> (array);
result.Sort (new ColumnDisplayIndexComparator ());
+
+ for (int i = 0; i < result.Count; i++)
+ result[i].DisplayIndex = i;
+
display_index_sorted = result;
}
+ internal void ClearAutoGeneratedColumns ()
+ {
+ for (int i = list.Count - 1; i >= 0; i--)
+ if ((list[i] as DataGridViewColumn).AutoGenerated)
+ list.RemoveAt (i);
+ }
+
private class ColumnDisplayIndexComparator : IComparer<DataGridViewColumn>
{
public int Compare (DataGridViewColumn o1, DataGridViewColumn o2)
+2008-05-20 Jonathan Pobst <monkey@jpobst.com>
+
+ * DataGridViewDataBindingTest.cs: Add test for AutoGenerateColumns.
+
2008-05-20 Jonathan Pobst <monkey@jpobst.com>
* DataGridViewColumnCollectionTest.cs: Change Add() test to not
f.Dispose ();
}
+ [Test]
+ public void TestAutoGenerateColumns ()
+ {
+ // Binding when AutoGenerateColumns is false
+ Form f = new Form ();
+ f.ShowInTaskbar = false;
+
+ DataSet ds = new DataSet ();
+
+ DataTable dt = ds.Tables.Add ("Muppets");
+
+ dt.Columns.Add ("ID");
+ dt.Columns.Add ("Name");
+
+ dt.Rows.Add (1, "Kermit");
+ dt.Rows.Add (2, "Miss Piggy");
+ dt.Rows.Add (3, "Gonzo");
+
+ DataGridView dgv = new DataGridView ();
+ dgv.AutoGenerateColumns = false;
+ dgv.DataSource = dt;
+
+ f.Controls.Add (dgv);
+ f.Show ();
+
+ Assert.AreEqual (0, dgv.ColumnCount, "A1");
+ Assert.AreEqual (0, dgv.RowCount, "A2");
+
+ dgv.DataSource = null;
+
+ DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn ();
+ col1.DataPropertyName = "Name";
+ dgv.Columns.Add (col1);
+
+ dgv.DataSource = dt;
+
+ Assert.AreEqual (1, dgv.ColumnCount, "A3");
+ Assert.AreEqual (4, dgv.RowCount, "A4");
+
+ Assert.AreEqual ("Kermit", dgv.Rows[0].Cells[0].Value, "A5");
+
+ dgv.DataSource = null;
+
+ DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn ();
+ col2.DataPropertyName = "id";
+ dgv.Columns.Add (col2);
+
+ dgv.DataSource = dt;
+
+ Assert.AreEqual (2, dgv.ColumnCount, "A6");
+ Assert.AreEqual (4, dgv.RowCount, "A7");
+
+ Assert.AreEqual ("Kermit", dgv.Rows[0].Cells[0].Value, "A8");
+ Assert.AreEqual ("1", dgv.Rows[0].Cells[1].Value, "A9");
+
+ f.Dispose ();
+ }
}
}
#endif
\ No newline at end of file