-1

这就是我现在所拥有的,这段代码只是将表名添加到组合框中,而不是 customerID。假设 CustomerID 有 1,2,3,4,5 我希望能够将每个 ID 添加到组合框中我该怎么做?

我现在拥有的:

private void Form2_Load(object sender, EventArgs e)
{
    ds = new DataSet();
    dc = new DataService();
    ds.Tables.Add(dc.GetData("Select * from Customers", "CustomerID"));

    foreach (DataTable dt in ds.Tables)
    {
        this.comboBox1.Items.Add(dt.TableName);
    }
}
4

3 回答 3

0

代替:

this.comboBox1.Items.Add(dt.TableName);

和:

        foreach (DataRow row in dt.Rows)
        {
            this.comboBox1.Items.Add(row[0].ToString());
        }
于 2013-11-12T10:49:07.257 回答
0

你可以使用这个

DataTable dt = new DataTable();
    dt = ds.Tables[0];
    foreach (DataRow item in dt.Rows)
    {

        // do what you want here
          this.comboBox1.Items.Add(item["CustomerID"]);
    }
于 2013-11-12T10:45:12.433 回答
0

这应该可以解决问题:

private void Form2_Load(object sender, EventArgs e)
{
  ds = new DataSet();
  dc = new DataService();
  DataTable td = dc.GetData("Select * from Customers", "CustomerID");
  foreach (DataRow dr in td.Rows)
  {
    this.comboBox1.Items.Add(dr["CustomerID"]);
  }
}
于 2013-11-12T10:46:08.063 回答