在post back上绑定LINQ查询有行,但不显示在屏幕上

本文关键字:屏幕 显示 查询 post back 绑定 LINQ | 更新日期: 2023-09-27 18:01:45

我有一个gridview,我使用LINQ查询绑定,最初它绑定完美,但在post back, post backs触发,LINQ查询代码运行完美并生成行。我显式地将结果分配为数据源并绑定gridview(用于初始绑定的相同查询),。在调试期间检查gridview中的行,有行,但不显示在屏幕上。请帮忙好吗?

下面是

后面的代码
var tl = (from i in dbcontex.SelectionProcesses
                  where i.VA.Position.Section.Contains( cmbSecs.SelectedValue) &&
                  i.VA.Position.DutyStation.Contains(cmbDutyS.SelectedValue) &&
                  i.VA.Position.Status != "Filled"
                  select new
                  {
                      i.VANo,
                      i.VA.Position.Level,
                      i.VA.ClosingDate,
                      i.TimeLine.NumAllottedDays,
                      DateOfferGiven=i.DateOfferGiven.Value==null?DateTime.MinValue:i.DateOfferGiven.Value,
                      DateApproved= i.DateApproved.Value==null?DateTime.MinValue:i.DateApproved.Value,
                      DateApprovedByRep = i.DateApprovedByRep.Value == null ? DateTime.MinValue : i.DateApprovedByRep.Value,
                      DateCRBConducted = i.DateCRBConducted.Value == null ? DateTime.MinValue : i.DateCRBConducted.Value,
                      DateCBIConducted = i.DateCBIConducted.Value == null ? DateTime.MinValue : i.DateCBIConducted.Value,
                      DateFinalShortReceived = i.DateFinalShortReceived.Value == null ? DateTime.MinValue : i.DateFinalShortReceived.Value,
                      DatePrescreenedCVSent = i.DatePrescreenedCVSent.Value == null ? DateTime.MinValue : i.DatePrescreenedCVSent.Value

                  }).ToList().
                 Select(l => new
                         {
                             l.VANo,
                             l.Level,
                             ClosingDate = l.ClosingDate.ToString("dd-MMM-yyyy"),
                             PreScreen =l.DatePrescreenedCVSent>l.ClosingDate?(l.DatePrescreenedCVSent - l.ClosingDate).Duration().Days:0,
                             ShortListing = l.DateFinalShortReceived>l.DatePrescreenedCVSent?(l.DateFinalShortReceived - l.DatePrescreenedCVSent).Duration().Days:0,
                             CBI = l.DateCBIConducted>l.DateFinalShortReceived?(l.DateCBIConducted - l.DateFinalShortReceived).Duration().Days:0,
                             RepApproval = l.DateApprovedByRep>l.DateCRBConducted?(l.DateApprovedByRep - l.DateCRBConducted).Duration().Days:0,
                             ESARDHR = !l.Level.Contains("GS") || l.DateApprovedByRep>l.DateApproved ? 0  : (l.DateApproved - l.DateApprovedByRep).Duration().Days,
                             Offer = l.DateOfferGiven>l.DateApproved ?(l.DateOfferGiven - l.DateApproved).Duration().Days:0,
                             TotalAllowed = l.NumAllottedDays
                         }).ToList().
                         Select(i => new
                         {
                             i.VANo,
                             i.Level,
                             i.ClosingDate,
                             i.PreScreen,
                             i.ShortListing,
                             i.CBI,
                             i.RepApproval,
                             i.ESARDHR,
                             i.Offer,
                             i.TotalAllowed,
                             TotalDaysTaken=i.PreScreen+i.ShortListing+i.CBI+i.RepApproval+i.Offer+i.ESARDHR
                         }
                         ).OrderBy(l => l.Level);
        var x = tl;
        gvTimeLine.DataSource = x;
        gvTimeLine.DataBind();
        if (gvTimeLine.Rows.Count > 0)
        {
            gvTimeLine.SelectedIndex = 0;
            gvTimeLine_SelectedIndexChanged(this, GridViewSelectEventArgs.Empty);
        }
html

      <asp:GridView ID="gvTimeLine" runat="server"  AutoGenerateColumns="false"
                Caption="List of Recruitment Time Lines Showing days taken" 
                CellPadding="4" 
                ForeColor="#333333"  Width="100%" 
                onrowdatabound="gvTimeLine_RowDataBound"  GridLines="Vertical"
                EmptyDataText="No Recruitment is underway" 
                AutoGenerateSelectButton="true" ShowHeaderWhenEmpty="True" 
                DataKeyNames="VANo" 
                onselectedindexchanged="gvTimeLine_SelectedIndexChanged" 
                style="margin-top: 0px">
                <Columns>
                    <asp:TemplateField HeaderText="VA/ReqNo">
                        <ItemTemplate>
                            <asp:HyperLink ID="hplk" runat="server" NavigateUrl="#"  
                         Text='<%# Bind("VANo") %>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                <asp:BoundField DataField="Level" HeaderText ="Level" />
                <asp:BoundField DataField="ClosingDate" HeaderText ="Closing Date" />
                <asp:BoundField DataField="PreScreen" HeaderText ="Prescreen" />
                <asp:BoundField DataField="ShortListing" HeaderText ="Short Listing" />
                <asp:BoundField DataField="CBI" HeaderText ="CBI" />
                <asp:BoundField DataField="RepApproval" HeaderText ="Rep Approval" />
                <asp:BoundField DataField="ESARDHR" HeaderText ="ESAR/DHR" />
                <asp:BoundField DataField="Offer" HeaderText ="Offer" />
                <asp:BoundField DataField="TotalAllowed" HeaderText ="Total Days 
                  Allowed" />
                <asp:BoundField DataField="TotalDaysTaken" HeaderText ="Total Days 
                 Taken" />
                <asp:TemplateField HeaderText ="TimeLine">
                <ItemTemplate>
                <eo:ProgressBar ID="prgStatus" ControlSkinID="None" Width="68px" 
                        runat="server" Height="16px" BackColor="#C7C7C7">
                </eo:ProgressBar>
                </ItemTemplate>
                </asp:TemplateField>
                </Columns>
          </Asp:Gridview>

在post back上绑定LINQ查询有行,但不显示在屏幕上

尝试在绑定任何新数据之前清除数据源。下面的代码将清除所有现有的数据。当然,除非问题是你正在分配的数据源有重复的数据。

gridview.DataSource = null;
gridview.DataBind();

希望这对你有帮助。如果没有,请评论。