我有2个数据网格视图(ValidationRun是master,Results是详细信息)。我将它绑定到一个我知道包含数据的数据集 - 我可以在调试时看到它。但是,我没有得到网格中的数据。
有什么想法吗?
/class level objects
private DataGridView dgvValidationRun = new DataGridView();
private BindingSource bsValidationRun = new BindingSource();
private DataGridView dgvResults = new DataGridView();
private BindingSource bsResults = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
// Bind the DataGridView controls to the BindingSource
// components and load the data from the database.
dgvValidationRun.DataSource = bsValidationRun;
dgvResults.DataSource = bsResults;
GetData();
// Resize the master DataGridView columns to fit the newly loaded data.
dgvValidationRun.AutoResizeColumns();
// Configure the details DataGridView so that its columns automatically
// adjust their widths when the data changes.
dgvResults.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
}
private void GetData()
{
try
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["XMLValidate.Properties.Settings.ValidationResultsConnectionString"].ConnectionString);
// Create a DataSet.
DataSet data = new DataSet();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
// Add data from the Customers table to the DataSet.
SqlDataAdapter daValidationRun = new SqlDataAdapter("select * from ValidationRun", connection);
daValidationRun.Fill(data, "ValidationRun");
// Add data from the Orders table to the DataSet.
SqlDataAdapter daResults = new SqlDataAdapter("select * from Result", connection);
daResults.Fill(data, "Result");
// Establish a relationship between the two tables.
DataRelation relRunResult = new DataRelation("RunResult",
data.Tables["ValidationRun"].Columns["RunId"],
data.Tables["Result"].Columns["RunId"]);
data.Relations.Add(relRunResult);
// Bind the run data connector to the Run table.
bsValidationRun.DataSource = data;
bsValidationRun.DataMember = "ValidationRun";
// Bind the results data connector to the results data connector,
// using the DataRelation name to filter the information in the
// details table based on the current row in the master table.
bsResults.DataSource = bsValidationRun;
bsResults.DataMember = "RunResult";
dgvValidationRun.AutoGenerateColumns = true;
dgvValidationRun.Refresh();
}
catch (Exception ex)
{
int i = 1;
}
}
答案 0 :(得分:0)
非常感谢nawfal,这就是问题所在。我有几个数据网格,我已经通过设计师添加到表单中,但忘了给它们与代码隐藏相同的名字 - doh!