M
Mark Olbert
I have a GridView as a child control in a custom composite control which is stubornly refusing to databind at design time. I'm
convinced I must be missing something about how the databinding process works differently at design time.
Here is the code that constructs the GridView in CreateChildControls():
gridSearch = new GridView();
gridSearch.AutoGenerateColumns = true;
CommandField cmdField = new CommandField();
cmdField.ShowSelectButton = false;
cmdField.SelectText = "select";
gridSearch.Columns.Add(cmdField);
FieldInfoCollection fieldColl = DesignMode ? DummyData.Fields : SearchFields;
for( int idx = 0; idx < fieldColl.Count; idx++ )
{
BoundField curField = new BoundField();
curField.HeaderText = fieldColl[idx].Friendly;
curField.DataField = fieldColl[idx].FieldName;
curField.ReadOnly = true;
curField.ShowHeader = true;
curField.Visible = true;
gridSearch.Columns.Add(curField);
}
At design time DummyData.Fields is composed of five "rows" of data in a List<>, where each row is a simple object:
public class DummyDataItem
{
private int row = 0;
public DummyDataItem( int row )
{
this.row = row;
}
protected string FieldValue( int row, int col )
{
return String.Format("Field{0}_Row{1}", col, row);
}
public string Field1
{
get { return FieldValue(row, 1); }
}
public string Field2
{
get { return FieldValue(row, 2); }
}
public string Field3
{
get { return FieldValue(row, 3); }
}
}
DummyData.Fields simply returns a list that has FieldNames of Field1, Field2 and Field3, corresponding to the properties in
DummyDataItem.
Later on the composite control that contains the GridView has its DataSource property set to that List<> of DummyDataItems, and then
the following code gets called in an event handler:
gridSearch.DataSource = DataSource;
gridSearch.DataBind();
No rows are created. No exceptions are thrown. I can walk over the code in the debugger, so I know the event handler is being
triggered. I verified that the gridSearch, at the point of the DataBind() call, has columns with DataField properties equal to
Field1, Field2 and Field3.
Yet it doesn't work. How could this be any simpler? Do GridViews not work with List<> of objects exposing public string properties?
I would appreciate some advice or suggestions.
- Mark
convinced I must be missing something about how the databinding process works differently at design time.
Here is the code that constructs the GridView in CreateChildControls():
gridSearch = new GridView();
gridSearch.AutoGenerateColumns = true;
CommandField cmdField = new CommandField();
cmdField.ShowSelectButton = false;
cmdField.SelectText = "select";
gridSearch.Columns.Add(cmdField);
FieldInfoCollection fieldColl = DesignMode ? DummyData.Fields : SearchFields;
for( int idx = 0; idx < fieldColl.Count; idx++ )
{
BoundField curField = new BoundField();
curField.HeaderText = fieldColl[idx].Friendly;
curField.DataField = fieldColl[idx].FieldName;
curField.ReadOnly = true;
curField.ShowHeader = true;
curField.Visible = true;
gridSearch.Columns.Add(curField);
}
At design time DummyData.Fields is composed of five "rows" of data in a List<>, where each row is a simple object:
public class DummyDataItem
{
private int row = 0;
public DummyDataItem( int row )
{
this.row = row;
}
protected string FieldValue( int row, int col )
{
return String.Format("Field{0}_Row{1}", col, row);
}
public string Field1
{
get { return FieldValue(row, 1); }
}
public string Field2
{
get { return FieldValue(row, 2); }
}
public string Field3
{
get { return FieldValue(row, 3); }
}
}
DummyData.Fields simply returns a list that has FieldNames of Field1, Field2 and Field3, corresponding to the properties in
DummyDataItem.
Later on the composite control that contains the GridView has its DataSource property set to that List<> of DummyDataItems, and then
the following code gets called in an event handler:
gridSearch.DataSource = DataSource;
gridSearch.DataBind();
No rows are created. No exceptions are thrown. I can walk over the code in the debugger, so I know the event handler is being
triggered. I verified that the gridSearch, at the point of the DataBind() call, has columns with DataField properties equal to
Field1, Field2 and Field3.
Yet it doesn't work. How could this be any simpler? Do GridViews not work with List<> of objects exposing public string properties?
I would appreciate some advice or suggestions.
- Mark