ComboBox显示System.Data.DataRow(MVC)

时间:2014-02-18 13:16:33

标签: sql-server asp.net-mvc combobox datatable three-tier

我的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());
                }
            }
         }
     }

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

我该如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:1)

当你在CBTime上调用Add()时,你没有进入Field级别。在条件检查中你的表有行的东西会起作用:

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