2014-03-27 78 views
0

我有一個結構在一個類內定義的結構,填充結構後,我將它綁定到一個gridview。在調試gridview_OnRowDataBound(...)時,gridview單元格不顯示任何數據。但是,UI上的gridview顯示數據。不確定它是否回發,但我確認在回發時page_load沒有變化。任何指針都會有所幫助。Gridview的數據綁定與結構不工作

public partial Class1 
{ 
.... 
.... 
     [Serializable] 
     public struct StructSelectedSiteList 
     { 
      public string SiteName 
      { 
       get; 
       set; 
      } 

      public string RncName 
      { 
       get; 
       set; 
      } 

      public string Status 
      { 
       get; 
       set; 
      } 

      public int TaskId 
      { 
       get; 
       set; 
      } 

      public string IssueType 
      { 
       get; 
       set; 
      } 

      public string ReworkSource 
      { 
       get; 
       set; 
      } 

      public string ReworkReason 
      { 
       get; 
       set; 
      } 
     }; 
.... 

聲明當地人

var lstSites = new List<StructSelectedSiteList>(); 

填充結構

// populate the struct for selected sites 
         StructSelectedSiteList structSelectedSiteList = new StructSelectedSiteList(); 
         structSelectedSiteList.SiteName = lblSiteName.Text; 
         structSelectedSiteList.Status = ddSiteStatusUpdate.SelectedValue.Trim(); 
         structSelectedSiteList.TaskId = taskId; 
         structSelectedSiteList.IssueType = ddIssues.SelectedValue.Trim(); 
         structSelectedSiteList.ReworkSource = reworkGrp.SelectedValue; 
         structSelectedSiteList.ReworkReason = ddReworkReason.SelectedValue; 
         structSelectedSiteList.RncName = lblRNCName.Text; 

lstSites.Add(structSelectedSiteList);

在每一行的GridView數據綁定

 protected void gvSelectedSites_OnRowDataBound(object sender, GridViewRowEventArgs e) 
     { 

.... 
.... 
// check if task id is bound 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[0].Text); 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[1].Text); 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[2].Text); 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[3].Text); 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[4].Text); 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[5].Text); 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[6].Text); 
       System.Diagnostics.Debug.WriteLine("data contents - " + e.Row.Cells[7].Text); 
      } 
} 

UI設計的GridView

<asp:GridView ID="gvSelectedSites" runat="server" AutoGenerateColumns="False" AllowSorting="True" 
           CellPadding="4" EmptyDataText="There are no data records to display." Font-Names="Segoe UI" 
           Width="605px" ForeColor="#333333" GridLines="None" RowStyle-HorizontalAlign="Center" 
           EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-ForeColor="Red" OnRowDataBound="gvSelectedSites_OnRowDataBound"> 
           <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
           <Columns>           
            <asp:TemplateField HeaderText="Site Name"> 
             <ItemTemplate> 
              <asp:Label ID="lblSiteName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SiteName")%>'> 
              </asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="RNC Name"> 
             <ItemTemplate> 
              <asp:Label ID="lblRNCName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "RncName")%>'> 
              </asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="Status"> 
             <ItemTemplate> 
              <asp:Label ID="lblStatus" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Status")%>'> 
              </asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="Issue Type"> 
             <ItemTemplate> 
              <asp:Label ID="lblIssueType" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "IssueType")%>'> 
              </asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="Task" Visible="false"> 
             <ItemTemplate> 
              <asp:Label ID="lblTaskId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "TaskId")%>'></asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField>           
            <asp:TemplateField HeaderText="Rework Source"> 
             <ItemTemplate> 
              <asp:Label ID="lblReworkSource" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ReworkSource")%>'> 
              </asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="Rework Reason"> 
             <ItemTemplate> 
              <asp:Label ID="lblReworkReason" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ReworkReason")%>'> 
              </asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="Site Comments"> 
             <ItemTemplate> 
              <asp:TextBox ID="txtSiteComments" runat="server" Width="350px" Height="50px" TextMode="MultiLine" 
               Rows="5"></asp:TextBox> 
             </ItemTemplate> 
            </asp:TemplateField> 
           </Columns> 
           <EditRowStyle BackColor="#999999" /> 
           <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
           <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
           <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
           <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
           <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
           <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
           <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
           <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
           <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
          </asp:GridView> 

回答

2

我相信格有數據,你只是在錯誤的地方。請注意,對於使用模板的每個字段,這意味着單元格本身確實具有文本,但是有一些內部控件。嘗試訪問這些控件,最有可能你會看到的數據:

// example for site name 
Label label = (Label)e.Row.FindControl("lblSiteName"); 
System.Diagnostics.Debug.WriteLine("data contents - " + label.Text); 
+0

謝謝!從內部控件訪問後,我可以看到數據。 –

+0

你提出了一個非常imp(我錯過了一個!)概念... field是一個模板,因此不能直接訪問值:) –