locked
Gridview inside repeater,-Trying to remove gridview column - not being deleted RRS feed

  • Question

  • User646364117 posted

    Code is being executed but column is  not deleted. Code is in the Repeater ItemDataBound event

    Protected Sub rptBlock_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBlock.ItemDataBound
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim lbl As Label = CType(e.Item.FindControl("lblBlockID"), Label)
                Dim blockId As Integer
                blockId = Convert.ToInt16(lbl.Text)
    
                Dim lbl1 As Label = CType(e.Item.FindControl("lblBlockType"), Label)
                Dim blocktype As String
                blocktype = lbl1.Text
    
                Dim grid As GridView = CType(e.Item.FindControl("gvContent"), GridView)
                Dim dtGrid As New DataTable
    
                Dim query As String = "SELECT 
                                        block.id,
    	                                block.blocktype_id AS BlockTypeID, 
    	                                block.name AS BlockName,
    	                                block.color,
    	                                block.included,
    	                                blocktype.blocktype,
    	                                content.shorttext as shorttext,
    	                                content.longtext AS longtext
                                    FROM 
    	                                block 
                                    JOIN
    	                                blocktype
    	                                ON block.blocktype_id =blocktype.id 
                                    JOIN 
    	                                content
    	                                ON block.id=dbo.Content.block_id
                                    WHERE 
    	                                block.id=@blockid"
    
                Dim sqlConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB_A54278_steve48ConnectionString").ConnectionString)
                sqlConn.Open()
    
                Dim da As SqlDataAdapter = New SqlDataAdapter()
                da.SelectCommand = New SqlCommand(query, sqlConn)
                da.SelectCommand.Parameters.Add("@blockid", SqlDbType.Int)
                da.SelectCommand.Parameters("@blockid").Value = blockId
                da.Fill(dtGrid)
                sqlConn.Close()
                grid.DataSource = dtGrid
                grid.DataBind()
    
                If blocktype <> "Long Text" Then
                    dtGrid.Columns.RemoveAt(1)
                    grid.DataBind()
                End If
            End If

    Friday, February 7, 2020 2:48 PM

All replies

  • User409696431 posted

    What does the debugger show for the lines where you are testing the condition and removing the column?  Is the condition met?  Are you getting any errors?

    Saturday, February 8, 2020 8:25 AM
  • User646364117 posted

    Hi Kathy. Thanks for responding. The debugger shows that the condition is met and there were no errors.

    I changed the code to make the column not visible instead of deleting it. That worked. That function is ok for me. Not sure why removing didn't work.

    If blocktype <> "Long Text" Then
                    grid.Columns(1).Visible = False
                    grid.DataBind()
                End If

    Saturday, February 8, 2020 11:24 AM
  • User409696431 posted

    It's rather strange, since I ran a test and removing the column from the datatable and rebinding the gridview did work.  (This assumes the gridview has no columns set declaratively in the page, since declaring a column that is not in the data throws an error.)

    Regardless, I'm glad you have a work-around.

    Saturday, February 8, 2020 6:50 PM
  • User288213138 posted

    Hi sg48asp,

    Protected Sub rptBlock_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBlock.ItemDataBound
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim lbl As Label = CType(e.Item.FindControl("lblBlockID"), Label)
                Dim blockId As Integer
                blockId = Convert.ToInt16(lbl.Text)
    
                Dim lbl1 As Label = CType(e.Item.FindControl("lblBlockType"), Label)
                Dim blocktype As String
                blocktype = lbl1.Text
    
                Dim grid As GridView = CType(e.Item.FindControl("gvContent"), GridView)
                Dim dtGrid As New DataTable
    
                Dim query As String = "SELECT 
                                        block.id,
    	                                block.blocktype_id AS BlockTypeID, 
    	                                block.name AS BlockName,
    	                                block.color,
    	                                block.included,
    	                                blocktype.blocktype,
    	                                content.shorttext as shorttext,
    	                                content.longtext AS longtext
                                    FROM 
    	                                block 
                                    JOIN
    	                                blocktype
    	                                ON block.blocktype_id =blocktype.id 
                                    JOIN 
    	                                content
    	                                ON block.id=dbo.Content.block_id
                                    WHERE 
    	                                block.id=@blockid"
    
                Dim sqlConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB_A54278_steve48ConnectionString").ConnectionString)
                sqlConn.Open()
    
                Dim da As SqlDataAdapter = New SqlDataAdapter()
                da.SelectCommand = New SqlCommand(query, sqlConn)
                da.SelectCommand.Parameters.Add("@blockid", SqlDbType.Int)
                da.SelectCommand.Parameters("@blockid").Value = blockId
                da.Fill(dtGrid)
                sqlConn.Close()
                grid.DataSource = dtGrid
                grid.DataBind()
    
                If blocktype <> "Long Text" Then
                    dtGrid.Columns.RemoveAt(1)
                    grid.DataBind()
                End If
            End If

    In your code, I didn't find out how you delete the column, and your query is select, not delete. Can you show me the code you are having problems with?

    And if you want to implement delete in ASP.Net Repeater control you can refer to this demo:

    Protected Sub DeleteCustomer(sender As Object, e As EventArgs)
        Dim customerId As Integer = Integer.Parse(TryCast(TryCast(sender, LinkButton).NamingContainer.FindControl("lblCustomerId"), Label).Text)
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId", con)
                cmd.Parameters.AddWithValue("@CustomerId", customerId)
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
     
        Me.BindRepeater()
    End Sub
    
    
    Private Sub BindRepeater()
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
                Using sda As New SqlDataAdapter(cmd)
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        Repeater1.DataSource = dt
                        Repeater1.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End Sub

    https://www.aspsnippets.com/Articles/Implement-Delete-with-Confirmation-in-ASPNet-Repeater-control.aspx

    Best regards,

    Sam

    Monday, February 10, 2020 2:41 AM
  • User409696431 posted

    samwu,

    "In your code, I didn't find out how you delete the column, and your query is select, not delete. Can you show me the code you are having problems with?"

    The original poster's code binds a GridView (grid) to a DataTable (dtGrid) that is populated with a Select query.  It is optionally removing a column in the DataTable, based on a condition, then rebinding the GridView with that DataTable.

    dtGrid.Columns.RemoveAt(1)
    grid.DataBind()

    In my own test, that works.  Something was different in the OP's code, since it did not work, but the OP found an alternative method.  The question is closed.

    Tuesday, February 11, 2020 5:22 PM