需要帮助搞清楚gridview我做错了什么?

时间:2010-06-28 20:18:03

标签: asp.net c#-3.0 c#-4.0 c#-2.0

如何在gridview上显示数据,有人看到我做错了吗?

<asp:GridView ID="xTimeGridView"
 runat="server" AllowSorting="True" 
          AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="CLOCK_IN_TIME"
 HeaderText="CLOCK_IN_TIME" 
                    SortExpression="CLOCK_IN_TIME" />
                <asp:BoundField DataField="CLOCK_OUT_TIME"
 HeaderText="CLOCK_OUT_TIME" 
                    SortExpression="CLOCK_OUT_TIME" />
            </Columns>

    string cmdquery = "SELECT * FROM EMPLOYEES WHERE BADGE ='" + Badge + "'";



            OracleCommand cmd = new OracleCommand(cmdquery);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            conn.Open();
            using (OracleDataReader reader = cmd.ExecuteReader())
            {

                while (reader.Read())
                {
                    this.xUserNameLabel.Text += reader["EMPLOYEE_NAME"];
                    this.xDepartmentLabel.Text += reader["REPORT_DEPARTMENT"];


                }




            }
            conn.Close();

            string hrquery = "SELECT CLOCK_IN_TIME, CLOCK_OUT_TIME FROM CLOCK_HISTORY WHERE BADGE='" + Badge + "'";

            OracleCommand time = new OracleCommand(hrquery);
            time.Connection = conn;
            time.CommandType = CommandType.Text;
            conn.Open();

            using (OracleDataReader readers = time.ExecuteReader())
            {
                while (readers.Read())
                {

                    xTimeGridView.DataSource = readers;
                    xTimeGridView.DataBind();

                }


            }
            conn.Close();

1 个答案:

答案 0 :(得分:2)

哦,你这里有问题。

为了让您了解正在发生的事情,在您的代码中,您正在尝试打开所获得的数据,然后循环遍历所有数据;我不确定,但我认为你有效地将GridView重新绑定到每个数据行。

您不希望迭代所有数据并“读取”它,您要做的是将查询结果存储在可绑定对象(如DataSet)中,以便您可以将gridview粘贴到它。 / p>

尝试这样的事情,请务必稍后添加你的try / catch:

        OracleCommand time = new OracleCommand(hrquery);
        time.Connection = conn;
        time.CommandType = CommandType.Text;
        conn.Open();
        // new code starts below
        DataSet data = new DataSet("my data");
        OracleDataAdapter adapter = new OracleDataAdapter(time);
        adapter.Fill(data);
        conn.Close();

        xTimeGridView.DataSource = data;
        xTimeGridView.DataBind();