1

コンボボックスに 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());
                }
            }
         }
     }

5 System.Data.DataRow の代わりに、「TimeBlock」に格納されているものを表示したいと思います。「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() を呼び出しているときに、フィールド レベルに達していません。テーブルに行があることを条件付きでチェックする場合、次のようなことが機能します。

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