locked
Multiples Rows Gridview per record Rowdatabound (vb) asp.net RRS feed

  • Question

  • User-629624826 posted

    Good day, I have a detail with my GridView, which filled it with all the information I want, the detail I have is that at run time I add rows to my grid, sometimes it is not necessary but in others if, when insert rows I have buttons in my main row and when I click them, the event they have defined does not enter. When rows are not added dynamically all the buttons work correctly. To keep the dynamic information if it is generated, what I realize is that in the action of the buttons, re-fill my grid to load it again. But they can guide me about the problem that I have, I leave some code that I use to generate my rows and an example of what I want to do.

    This is a example grid:

    Example Grid

    Use BoundField and TemplateField for fixed columns.
    
    Every time I generate a dynamic row I do it in the following way in the RowDataBound:
    Dim gvr1 As New GridViewRow(0, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
    
    Dim thc5 As New TableCell
    thc5.ColumnSpan = 2
    thc5.Text = "Comments:"
    gvr1.Cells.Add(thc5)
    
    Dim thc6 As New TableCell
    thc6.ColumnSpan = 24
    thc6.Style("font-weight") = "bold"
    thc6.Text = "textBox Commentarios"
    gvr1.Cells.Add(thc6)
    
    grdPrueba.Controls(0).Controls.Add(gvr1)

    The grid loads well the first time, and my buttons declare them as follows:

                <asp:TemplateField ItemStyle-CssClass="saveBtn">
                    <ItemTemplate>
                        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSve"  />
                    </ItemTemplate>
                </asp:TemplateField>
    The event in the code is as follows:
      Protected Sub btnSve(ByVal sender As Object, ByVal e As System.EventArgs)
    
      response.write("Test")
    
      llenarGrid()
    
        End Sub
    Can you guide me on how I can solve this part? or what do you recommend, thank you.

    Monday, April 29, 2019 8:37 PM

All replies

  • User288213138 posted

     
    Hi  Pollokulos,
     
    According to your description, I tried to reproduce your question. The OnRowDatabound () method is not able to meet your requirement, because your new rows will crowd out the original rows, but you can implement it in DataBound ().
     
    The code:

    Class SurroundingClass
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                GridView1.DataSource = New Integer() {1, 2, 3, 4}
                GridView1.DataBind()
            End If
        End Sub
    
        Protected Sub btnSve(ByVal sender As Object, ByVal e As System.EventArgs)
            Response.Write(DateTime.Now.ToString())
            GridView1.DataSource = New Integer() {1, 2, 3, 4}
            GridView1.DataBind()
        End Sub
    
        Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
            Dim count As Integer = GridView1.Controls(0).Controls.Count
    
            For i As Integer = 1 To count - 1
                Dim gvr1 As GridViewRow = New GridViewRow(0, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
                Dim thc5 As TableCell = New TableCell()
                thc5.Text = "Comments:"
                gvr1.Cells.Add(thc5)
                Dim thc6 As TableCell = New TableCell()
                thc6.Style("font-weight") = "bold"
                thc6.Text = "textBox Commentarios"
                gvr1.Cells.Add(thc6)
                GridView1.Controls(0).Controls.AddAt(System.Threading.Interlocked.Increment(i), gvr1)
            Next
        End Sub
    End Class

    The Result:

    Best Regards,

    Sam

    Tuesday, April 30, 2019 7:49 AM