Jon Skeet [C# MVP] wrote:
Adie <ar*******@h-o-t-m-a-i-l.com> wrote: Ok, this is the error "Cannot create a child list for field [DataSetName]"
which I called "dimensions". I was trying to bind that dataset to the grid,
like this:
DataSet ds = product.getDataSetDimensions();
_DataGrid = new DataGrid();
_Form.Controls.Add(_DataGrid);
// this commented out code works
//// DataTable dt = ds.Tables[0];
//// _DataGrid.DataSource = dt;
// this causes the error
_DataGrid.SetDataBinding(ds,"dimensions");
I think I need to spend more time reading up on the controls and ADO.
Hmm... not sure then, I'm afraid. The table in the dataset is
definitely called dimensions?
Right, well this's where I may be confused. When I created the DataSet I
just called it "dimensions" instead of the actual table name. Maybe this is
why referring to the table via the index works. I thought I could use the
name as a sort of tag or alias being a query can span more than one table.
Here's the jist of what I did:
class GenericData
{
// helper class that allows me to query and add
// any name
public DataSet getDataSetByString(string sql, string dataSetName)
{
SqlConnection mySQLCon = new SqlConnection(@"####");
SqlDataAdapter myAdapter = new SqlDataAdapter();
mySQLCon.Open();
SqlCommand myCommand = new SqlCommand(sql, mySQLCon);
myCommand.CommandType = CommandType.Text;
myAdapter.SelectCommand = myCommand;
DataSet ds = new DataSet(dataSetName); // name the dataset here
myAdapter.Fill(ds);
return ds;
}
}
class ProductFamily
{
public DataSet getDataSetDimensions()
{
GenericData sqlGD = new GenericData();
if (_productType != "")
{
return sqlGD.getDataSetByString(@"SELECT "
+ dimensionArrayToString() + // holds an array of fields for the product
" FROM products WHERE [Product Line] = '"
+ _productType + "' ORDER BY [Product Id];",
"dimensions"); // <-- DataSetName named here on the function call
}
else
{
return sqlGD.returnDataSetNull(); // jiggery pokery, returns a null
// to test against
}
}
}
class DataFormatter
{
public DataFormatter (Form form)
{
_Form = form;
}
public formatProductData(ProductFamily product)
{
DataSet ds = product.getDataSetDimensions();
_DataGrid = new DataGrid();
_Form.Controls.Add(_DataGrid);
DataTable dt = ds.Tables[0];
_DataGrid.DataSource = dt;
}
}
It's basically the fallout from a conversation I had with you a while back
on how to treat products in a database which have a variety of different
attributes, such as width, height, lenght, etc x 100.
I opted for the "wide table" you said you had seperate tables for each
product type/family -- now I spend my time working out how to dynamically
update and display the product data.