GridView问题显示所有记录 [英] GridView problem displaying all records

查看:83
本文介绍了GridView问题显示所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个Gridview,由于某种原因我看不到查询的第一条记录

Gridview代码

I have a Gridview and for some reason I can not see the first record oF my query

Gridview code

<asp:GridView ID="GridView1"  CssClass="DG"  RowStyle-Wrap="false"

                     runat="server" AutoGenerateColumns="False" PageIndex="1" PageSize="1"   ><emptydatarowstyle backcolor="LightBlue"

                                     forecolor="Red"/>
                                   <emptydatatemplate>

                                      No Data Found.</emptydatatemplate><AlternatingRowStyle

                                BackColor="#99CCFF" Font-Size="Small" /><Columns>
                                <asp:TemplateField HeaderText="ID" Visible="false"><ItemTemplate><%# DataBinder.Eval(Container.DataItem, "ID") %>
                                </ItemTemplate></asp:TemplateField>
                                <asp:BoundField DataField="Employee_No"

                                    HeaderText="Employee No" SortExpression="Employee_No" /><asp:BoundField

                                    DataField="Employee_Name" HeaderText="Employee Name"

                                    SortExpression="Employee_Name" /><asp:BoundField DataField="Cost_Center"

                                    HeaderText="Cost Center" SortExpression="Cost_Center" /><asp:BoundField

                                    DataField="Lead_Id" HeaderText="Lead Id" SortExpression="Lead_Id" /><asp:BoundField

                                    DataField="WorkSchedule_Date" HeaderText="WorkSchedule Date"  HtmlEncode="false"

                                    SortExpression="WorkSchedule_Date" /><asp:BoundField

                                    DataField="Schedule_In" HeaderText="Schedule In" SortExpression="Schedule_In"  /><asp:BoundField

                                    DataField="Schedule_Out" HeaderText="Schedule Out"

                                    SortExpression="Schedule_Out" /><asp:BoundField DataField="Job_In_Time"

                                    HeaderText="Job In Time" SortExpression="Job_In_Time" /><asp:BoundField

                                    DataField="Job_Out_Time" HeaderText="Job Out Time"

                                    SortExpression="Job_Out_Time" /><asp:BoundField DataField="column1"

                                    HeaderText="ETC In (P10)" SortExpression="column1" /><asp:BoundField

                                    DataField="column2" HeaderText="ETC Out (P20)" SortExpression="column2" /><asp:BoundField

                                    DataField="ETBH" HeaderText="ETBH" SortExpression="ETBH" /><asp:BoundField

                                    DataField="ETAH" HeaderText="ETAH" SortExpression="ETAH" />
                                    <asp:TemplateField

                                    HeaderText="User Approval" SortExpression="User_Approval" >
                                    <HeaderTemplate>User Approval<br /><asp:LinkButton     ID="LnkUpdate"     runat="server"

                                    CommandName="update"     Text="Update All"     ForeColor="BlueViolet"

                                     OnClick="LnkUpdate_Click"></asp:LinkButton></HeaderTemplate><ItemTemplate>
                                    <asp:DropDownList

                                     ID="ddl_Approval" runat="server"><asp:ListItem Value="UNAPPROVED"></asp:ListItem><asp:ListItem

                                            Value="APPROVED"></asp:ListItem></asp:DropDownList><%# DataBinder.Eval(Container.DataItem, "User_Approval")%></ItemTemplate></asp:TemplateField></Columns><HeaderStyle

                                BackColor="WhiteSmoke" /><RowStyle Wrap="False" /></asp:GridView>




背后的代码:




Code behind:

protected void Button1_Click(object sender, EventArgs e)
        {

            if ((txtWSDate.Text == ""))
            {
                //Response.Write((txtCourseCreditDate.Text == "")  + " - " + (txtHours.Text == ""));
                //Response.End();
                Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
                Response.Write("alert('Schedule Date must have an entry!');");
                Response.Write("</script>");
                return;
            }

            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["OTTARConnectionString"].ConnectionString);
            myConnection.Open();

            try
            {
                GridView1.DataSourceID = "";
                GridView1.Visible = true;
                SqlDataReader reader = null;
                string LeadID = ddl_leader_list.SelectedItem.Value.ToString();
                string SkedDate = txtWSDate.Text.ToString();
                SqlCommand cmd = new SqlCommand();
                //  SqlCommand cmd = new SqlCommand(" " + serverIP, myConnection);
                string strData = "";
                strData = "SELECT ID,[Employee No] AS Employee_No, [Employee Name] AS Employee_Name, ";
                strData += " [Cost Center] AS Cost_Center, RTRIM(LTRIM([Lead Id])) AS Lead_Id, ";

                strData += " [WorkSchedule Date] AS WorkSchedule_Date, [Schedule In] AS Schedule_In, ";
                strData += " [Schedule Out] AS Schedule_Out, [Job In Time] AS Job_In_Time,  ";
                strData += "  [Job Out Time] AS Job_Out_Time, [ETC In (P10)] AS column1,  ";
                strData += "[ETC Out (P20)] AS column2, [ETBH], [ETAH], ";
                strData += "[Approved Disapproved] AS User_Approval FROM [SAA_MCS_Extended_Time2] ";
                strData += " WHERE  RTRIM(LTRIM([Lead Id])) = '" + ddl_leader_list.SelectedValue + "'";
                strData += " AND [WorkSchedule Date] = Cast ('" + txtWSDate.Text + "' as datetime)";


               Response.Write(strData);
            //    Response.End();

                cmd.Connection = myConnection;
                cmd.CommandText = strData;
                reader = cmd.ExecuteReader();
            
                while (reader.Read())
                {
                    GridView1.DataSource = reader;
                    GridView1.DataBind();
                    Response.Write(reader);
                }

            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            myConnection.Close();
        }

推荐答案

I am not certain on this. But I do not think the lines "GridView1.DataSource = reader;" and "GridView1.DataBind()" should be inside the "while (reader.Read())" block. Someone please correct me if I am wrong. But, for example, if you query ten thousand rows, you are telling GridView1 to update its data ten thousand times.
I am not certain on this. But I do not think the lines "GridView1.DataSource = reader;" and "GridView1.DataBind()" should be inside the "while (reader.Read())" block. Someone please correct me if I am wrong. But, for example, if you query ten thousand rows, you are telling GridView1 to update its data ten thousand times.


这篇关于GridView问题显示所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆