ComboBox Shows System.Data.DataRow (MVC)



My Combobox 不会显示我的 SQL-Attribute "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());
                }
            }
         }
     }

而不是5 System.Data.DataRow,我想显示存储在"TimeBlock"中的内容。"从时间段中选择时间块 按时间块分组"显示:"08-00 - 10:00""10:00 - 12:00""12:00 - 14:00""14:00 - 16:00"16:00 - 18:00"

我该如何解决这个问题?

谢谢

当你在CBTime上调用Add()时,你没有进入字段级别。 在有条件检查表中,类似这样的事情可以工作:

foreach (DataRow dRow in DTable.Rows)
{
     CBTime.Items.Add(dRow["TimeBlock"]);
}

最新更新