需要帮助找出 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();
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哦,你这里有问题。
为了让您了解代码中发生的情况,您尝试打开获得的数据,并循环遍历所有数据;我不确定,但我认为您有效地将 GridView 单独重新绑定到每行数据。
您不想迭代所有数据并“读取”它,您想要做的是将查询结果存储在可绑定对象(如 DataSet)中,以便您可以将 gridview 粘合到它。
尝试这样的事情,一定要稍后添加你的 try/catch :
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: