2010-04-26 76 views

回答

2

this article

讀或者,您也可以通過使用EmptyDataTemplate property定義空的數據行自己的自定義用戶界面。

+0

其實我更喜歡'EmptyDataTemplate'比綁定更多的空'DataSet'到'GridView',我一直是這樣做,它有很多的問題和爲什麼他們居然把'EmptyDataTemplate'。我指責微軟沒有實現這樣的功能,但是將空DataSet綁定到GridView並不是實現這一功能的最佳方式。 – 2012-05-07 19:16:19

1

可以通過分配一個虛擬行網格的數據源也做到這一點。 默認情況下,數據源將填充虛擬行。所以網格也會顯示頁腳&。 要隱藏行,您可以添加一個虛擬字段以顯示是否顯示控件。並將該字段綁定到控件的Visible屬性。 最後,當您將實際行添加到數據源時...刪除您創建的虛擬行。

實例 - 來源:

<asp:GridView ID="grdQualify" runat="server" Width="100%" AutoGenerateColumns="False" ShowFooter="true" 
         EnableModelValidation="True"> 
         <Columns> 
          <asp:TemplateField HeaderText="Qualifying Exam"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtQualifyingExam" runat="server" Text='<%# Eval("QualifyingExam") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:TextBox ID="txtQualifyingExamF" runat="server"></asp:TextBox> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Roll Number"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtRollNumber" runat="server" Text='<%# Eval("RollNumber") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:TextBox ID="txtRollNumberF" runat="server"></asp:TextBox> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Admission Round"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtAdmissionRound" runat="server" Text='<%# Eval("AdmissionRound") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:TextBox ID="txtAdmissionRoundF" runat="server"></asp:TextBox> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Marks Obtained"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtMarksObtained" runat="server" Text='<%# Eval("MarksObtained") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:TextBox ID="txtMarksObtainedF" runat="server"></asp:TextBox> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Marks Out of"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtMarksOutOf" runat="server" Text='<%# Eval("MarksOutOf") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:TextBox ID="txtMarksOutOfF" runat="server"></asp:TextBox> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Percentage"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtPercentage" runat="server" Text='<%# Eval("Percentage") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:TextBox ID="txtPercentageF" runat="server"></asp:TextBox> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Remarks"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtRemarks" runat="server" Text='<%# Eval("Remarks") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:TextBox ID="txtRemarksF" runat="server"></asp:TextBox> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField> 
           <ItemTemplate> 
            <asp:Button ID="btnDeleteQualify" runat="server" Text="Delete" 
             Visible='<%# Eval("ShowVisible") %>' onclick="btnDeleteQualify_Click"></asp:Button> 
           </ItemTemplate> 
           <FooterTemplate> 
            <asp:Button ID="btnAddQualify" runat="server" Text="Add" 
             onclick="btnAddQualify_Click"></asp:Button> 
           </FooterTemplate> 
          </asp:TemplateField> 
         </Columns> 
        </asp:GridView> 

實例 - 代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 

      if (ViewState["QualifyDetails"] == null) 
       ViewState["QualifyDetails"] = new List<Entity_StudentAcademicQualify>(); 
      GridDataBindQualify((List<Entity_StudentAcademicQualify>)ViewState["QualifyDetails"]); 
     } 
    } 


    private void GridDataBindQualify(List<Entity_StudentAcademicQualify> list) 
    { 
     if (list.Count == 0) 
     { 
      Entity_StudentAcademicQualify studentQualify = new Entity_StudentAcademicQualify(); 

      studentQualify.ShowVisible = false; 
      studentQualify.QualifyingExam = string.Empty; 
      studentQualify.RollNumber = string.Empty; 
      studentQualify.AdmissionRound = string.Empty; 
      studentQualify.MarksObtained = 0; 
      studentQualify.MarksOutOf = 0; 
      studentQualify.Percentage = 0; 
      studentQualify.Remarks = string.Empty; 

      list.Add(studentQualify); 
     } 
     else 
     { 
      list.RemoveAt(0); 
     } 

     grdQualify.DataSource = list; 
     grdQualify.DataBind(); 

     ViewState["QualifyDetails"] = list; 
    } 

    protected void btnAddQualify_Click(object sender, EventArgs e) 
    { 
     GridViewRow row = (GridViewRow)((Button)sender).Parent.Parent; 

     List<Entity_StudentAcademicQualify> studentQualifys = 
      (List<Entity_StudentAcademicQualify>)ViewState["QualifyDetails"]; 

     Entity_StudentAcademicQualify studentQualify = new Entity_StudentAcademicQualify(); 

     studentQualify.ShowVisible = true; 
     studentQualify.QualifyingExam = ((TextBox)row.FindControl("txtQualifyingExamF")).Text; 
     studentQualify.RollNumber = ((TextBox)row.FindControl("txtRollNumberF")).Text; 
     studentQualify.AdmissionRound = ((TextBox)row.FindControl("txtAdmissionRoundF")).Text; 
     studentQualify.MarksObtained = Convert.ToInt32(((TextBox)row.FindControl("txtMarksObtainedF")).Text); 
     studentQualify.MarksOutOf = Convert.ToInt32(((TextBox)row.FindControl("txtMarksOutOfF")).Text); 
     studentQualify.Percentage = Convert.ToInt32(((TextBox)row.FindControl("txtPercentageF")).Text); 
     studentQualify.Remarks = ((TextBox)row.FindControl("txtRemarksF")).Text; 

     studentQualifys.Add(studentQualify); 

     GridDataBindQualify(studentQualifys); 
    } 

    protected void btnDeleteQualify_Click(object sender, EventArgs e) 
    { 
     List<Entity_StudentAcademicQualify> studentQualifys = 
      (List<Entity_StudentAcademicQualify>)ViewState["QualifyDetails"]; 

     studentQualifys.RemoveAt(((GridViewRow)((Button)sender).Parent.Parent).RowIndex); 

     GridDataBindQualify(studentQualifys); 
    } 
1

我看了很多帖子在網上關於這一點,在它結束時,我想到了我自己的解決方案,可以滿足我的需求,希望包括這個問題在內的其他人的需求。

需要顯示頁眉/頁腳可能是因爲 1)我想讓用戶看到網格的結構,並且 2)我想使用頁腳向表中添加新數據。

因此,簡單地示出了「無可用數據」是不夠的。

所以我的解決方法是測試數據是否可用。如果不是,則創建數據源的結構並使用默認值填充一行所需的字段。這很重要,因爲如果當布爾和日期等字段爲空時將數據源綁定到網格,則會出現'DBNull'錯誤。然後將這個新的數據源綁定到網格。然後按此順序隱藏該行。

Protected Sub BindGrid() 
    con = New SqlConnection("mainLocal") 
    Dim da As New SqlDataAdapter("SELECT * FROM Test", con) 
    Dim dt As New DataTable() 
    da.Fill(dt) 

    dim rows as integer = dt.Rows.Count 
    If rows = 0 Then 
     Dim dr As DataRow = Nothing 

     dr = dt.NewRow() 
     dr("isActive") = False 
     dr("Dated") = Date.Now() 
     dt.Rows.Add(dr) 
    End If 

    Me.TestGridView.DataSource = dt 
    Me.TestGridView.DataBind() 
    If rows = 0 Then Me.TestGridView.Rows(0).Visible = False 
End Sub 

我想更簡單,更短,整潔!