locked
If I let the user delete rows from a gridview, how do I know when the gridview is empty? RRS feed

  • Question

  • User297437924 posted

    I put the following code in a rowDeleted event of a gridview.   I want to put up a message if the user deletes all his rows.   But the code consistently overestimates the number of data rows:

     Private Sub GridView1_RowDeleted(sender As Object, e As GridViewDeletedEventArgs) Handles GridView1.RowDeleted
            Dim rowsLeft As Integer
            Dim dataRowsLeft As Integer = 0
            Dim i As Integer
    
            rowsLeft = GridView1.Rows.Count
            For i = 0 To rowsLeft - 1
                Select Case GridView1.Rows(i).RowType
                    Case DataControlRowType.EmptyDataRow, DataControlRowType.Footer, DataControlRowType.Header
                        Continue For
                    Case DataControlRowType.DataRow
                        dataRowsLeft += 1
                End Select
    
            Next
            If dataRowsLeft = 0 Then
                    Session("GlobalErrorMessage") = "You have deleted all your rows!  The world will now end in 5 seconds!!!!"
                    Response.Redirect("/regularmessage.aspx")
                End With
            End If
        End Sub

    Sunday, October 27, 2019 2:15 PM

All replies

  • User475983607 posted

    The GridView has an empty data template that you can use to display an alert if the GridView data source is empty.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.emptydatatemplate?view=netframework-4.8

    Sunday, October 27, 2019 3:21 PM
  • User297437924 posted

    I also need to execute some code-behind code if the grid is empty, though.

    Sunday, October 27, 2019 4:08 PM
  • User475983607 posted

    I also need to execute some code-behind code if the grid is empty, though.

    Okay, what is stopping you from checking if the data source is empty?  Clearly, the user cannot delete a row if at least one row exists.  Check the data source for rows when you rebind the GridView after the delete.

    Sunday, October 27, 2019 4:21 PM
  • User288213138 posted

    Hi RateFor,

    I also need to execute some code-behind code if the grid is empty, though.

    You can check if the gridview is empty when the datasource of the gridview is empty.

    I made demo for you as a reference.

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
    OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
    <Columns>
    <asp:TemplateField HeaderText="ContactName" ItemStyle-Width="150">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
    <ItemTemplate>
    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ButtonType="Link" ShowDeleteButton="true" ItemStyle-Width="150" />
    </Columns>
    </asp:GridView>


    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; string query = "SELECT * FROM Customers"; using (SqlConnection con = new SqlConnection(constr)) { using (SqlDataAdapter sda = new SqlDataAdapter(query, con)) { using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); if (dt.Rows.Count == 0) { ShowEmptyData(); } } } } } public void ShowEmptyData() { Response.Write("The Gridview is empty"); }

    The result:

    Best regards,

    Sam

    Monday, October 28, 2019 2:47 AM
  • User-1716253493 posted

    You need else part to set another errormessage

    Because you use session variable, you need to change or clear the message. So, you will not see previous message.

    Get it from the datasource

            Dim dt As DataTable = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView).Table
            If dt.Rows.Count = 0 Then
                'empty
            Else
                'not empty
            End If

    Friday, November 1, 2019 12:31 AM