2014-04-18 133 views
0

我需要使用for循环将'datagridview'访问数据库中的单个列添加到'listbox'。到目前为止,我没有返回任何错误,但没有检索任何数据。C# - 使用for循环将Datagridview列添加到列表框中

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

我会遍历DataTable而不是DataGridView。

喜欢的东西:

for (int i=0; i< datatable.Rows.Count; i++){ 
    this.lstUniqueInstructor.Items.Add(datatable.Rows[i][4].ToString()); 
} 

的4列索引,但你也可以使用列名的字符串。