需要帮助找出 gridview 我做错了什么?

发布于 2024-09-07 04:46:21 字数 1773 浏览 2 评论 0原文

我怎样才能让数据显示在网格视图上,有人看到我做错了什么吗?

<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();

How can I get the data to show on the gridview, anybody see what I'm doing wrong?

<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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-09-14 04:46:21

哦,你这里有问题。

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

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

尝试这样的事情,一定要稍后添加你的 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();

Oh, you've got a problem here.

Just so that you understand what's going on, in your code, you are trying to open up the data that you get, and loop through it all; I'm not sure but I think you are effectively re-binding your GridView to each line of data individually.

You don't want to iterate through all the data and "read" it, what you want to do is store your query's results in a bindable object (like a DataSet) so that you can glue your gridview to it.

Try something like this instead, be sure to add your try/catch's later:

        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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文