1

我的 Combobox 没有显示我的 SQL 属性“TimeBlock”中的值,而是显示 System.Data.DataRow 5 次。我的代码有什么问题?

代码:

    //DAL:

    public class DAL{

    string ConnectionString = "server=ICSSQL13\\Grupp28,1528; Trusted_Connection=yes; database=Yoloswag";

    public DataTable StoreSqlDataInComboBoxTP()
            {

                SqlConnection Conn = new SqlConnection(ConnectionString);

                Conn.Open();

                string StoreSqlDataInComboBoxTP = "SELECT TimeBlock FROM TimePeriod GROUP BY TimeBlock";

                SqlCommand Cmd = new SqlCommand(StoreSqlDataInComboBoxTP, Conn);

                SqlDataAdapter Adapter = new SqlDataAdapter(Cmd);

                DataSet DSet = new DataSet();

                Adapter.Fill(DSet);

                Adapter.Dispose();
                Cmd.Dispose();
                Conn.Close();
                Conn.Close();

                return DSet.Tables[0];
            }
    }

    //Controller:

    public class Controller
    { 
    DAL Dal = new DAL();

    public DataTable storesqldataincomboboxtp()
        {
           return Dal.StoreSqlDataInComboBoxTP();
        }
    }

//View:
public partial class Booking : Form
    {
        Controller controller = new Controller();
        DataTable DTable = new DataTable();
        DataSet DSet = new DataSet();

        //Ignore string UserName
        public Booking(string UserName){
            DTable = controller.storesqldataincomboboxtp();

            if (DTable.Rows.Count > 0)
            {
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    CBTime.Items.Add(DTable.Rows[i].ToString());
                }
            }
         }
     }

我想显示“TimeBlock”中存储的内容,而不是 5 System.Data.DataRow。“SELECT TimeBlock From TimePeriod GROUP BY TimeBlock”显示:“08-00 - 10:00”“10:00 - 12:00”“12:00 - 14:00”“14:00 - 16:00”“16: 00 - 18:00"

我该如何解决这个问题?

谢谢

4

1 回答 1

1

当您在 CBTime 上调用 Add() 时,您没有到达 Field 级别。在有条件地检查表是否有行中这样的事情会起作用:

foreach (DataRow dRow in DTable.Rows)
{
     CBTime.Items.Add(dRow["TimeBlock"]);
}
于 2014-02-18T13:26:03.227 回答