我需要使用for循环将“datagridview”访问数据库中的单个列添加到“listbox”。到目前为止,我没有返回错误,但没有检索任何数据。
private void frmProject6_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'enrollmentsDataSet.Enrollments' table. You can move, or remove it, as needed.
this.enrollmentsTableAdapter.Fill(this.enrollmentsDataSet.Enrollments);
}
private void lstUniqueInstructor_SelectedIndexChanged(object sender, EventArgs e)
{
this.lstUniqueInstructor.Items.Clear();
for(int i = 0; i < this.dgvEnrollments.Rows.Count; i++)
{
this.lstUniqueInstructor.Items.Add(this.dgvEnrollments.Columns[4].HeaderText);
}
}
答案 0 :(得分:0)
我遍历DataTable而不是DataGridView。
类似的东西:
for (int i=0; i< datatable.Rows.Count; i++){
this.lstUniqueInstructor.Items.Add(datatable.Rows[i][4].ToString());
}
4是列索引,但您也可以使用列名称的字符串。