Q
QSIDeveloper
I am trying to do what I think is a very basic binding operation. I have a
DataGridView with 2 columns. The first column is bound to Northwind db
Region and the second to Territories. When the form loads I get a Value not
valid exception on the Territories column. The error only occurs if I have
assigned a data source to the DataGridView. The data grid view is a simple
class populated with the ID’s of what should be the selected in the grid
combo boxes.
Code for the data class and the form load routine is included.
void FilterByCell_Load(object sender, EventArgs e)
{
DataSet regions = new DataSet("Regions");
DataSet territories = new DataSet("Territories");
regions = SelectRows(regions, m_connectionString, "Select
RegionID, RegionDescription from Region");
territories = SelectRows(territories, m_connectionString,
"Select TerritoryID, TerritoryDescription, RegionID from territories");
this.RegionsColumn.DataSource = regions.Tables[0];
this.RegionsColumn.DisplayMember = "RegionDescription";
this.RegionsColumn.ValueMember = "RegionID";
this.TerritoriesColumn.DataSource = territories.Tables[0];
this.TerritoriesColumn.DisplayMember = "TerritoryDescription";
this.TerritoriesColumn.ValueMember = "TerritoryID";
m_DataList = new List<ViewData>();
m_DataList.Add(new ViewData(3, 44122));
m_DataList.Add(new ViewData(3, 53404));
m_DataList.Add(new ViewData(2, 60601));
m_DataList.Add(new ViewData(2, 94105));
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = m_DataList;
dataGridView1.Columns[0].DataPropertyName = "RegionID";
dataGridView1.Columns[1].DataPropertyName = "TerritoryID";
}
public class ViewData
{
private int m_Region;
private int m_Territory;
public ViewData(int Region, int Territory)
{
m_Region = Region;
m_Territory = Territory;
}
public int RegionID
{
get { return m_Region; }
set { m_Region = value; }
}
public int TerritoryID
{
get { return m_Territory; }
set { m_Territory = value; }
}
}
DataGridView with 2 columns. The first column is bound to Northwind db
Region and the second to Territories. When the form loads I get a Value not
valid exception on the Territories column. The error only occurs if I have
assigned a data source to the DataGridView. The data grid view is a simple
class populated with the ID’s of what should be the selected in the grid
combo boxes.
Code for the data class and the form load routine is included.
void FilterByCell_Load(object sender, EventArgs e)
{
DataSet regions = new DataSet("Regions");
DataSet territories = new DataSet("Territories");
regions = SelectRows(regions, m_connectionString, "Select
RegionID, RegionDescription from Region");
territories = SelectRows(territories, m_connectionString,
"Select TerritoryID, TerritoryDescription, RegionID from territories");
this.RegionsColumn.DataSource = regions.Tables[0];
this.RegionsColumn.DisplayMember = "RegionDescription";
this.RegionsColumn.ValueMember = "RegionID";
this.TerritoriesColumn.DataSource = territories.Tables[0];
this.TerritoriesColumn.DisplayMember = "TerritoryDescription";
this.TerritoriesColumn.ValueMember = "TerritoryID";
m_DataList = new List<ViewData>();
m_DataList.Add(new ViewData(3, 44122));
m_DataList.Add(new ViewData(3, 53404));
m_DataList.Add(new ViewData(2, 60601));
m_DataList.Add(new ViewData(2, 94105));
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = m_DataList;
dataGridView1.Columns[0].DataPropertyName = "RegionID";
dataGridView1.Columns[1].DataPropertyName = "TerritoryID";
}
public class ViewData
{
private int m_Region;
private int m_Territory;
public ViewData(int Region, int Territory)
{
m_Region = Region;
m_Territory = Territory;
}
public int RegionID
{
get { return m_Region; }
set { m_Region = value; }
}
public int TerritoryID
{
get { return m_Territory; }
set { m_Territory = value; }
}
}