Results 1 to 6 of 6

Thread: Datagridview button not working when use databoundfilter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    122

    Datagridview button not working when use databoundfilter

    Hi,

    Appeciate some help please.

    I have a datagridview which I have added a button into.

    When the grid is unfiltered the button works fine but when I apply the filter it does not appear to work.

    Is there any way I can get the button to work whilst filtered!

    Code so far
    Code:
        Dim dt As DataTable
        Dim bndSourceGrid As New BindingSource()
    
        Private Sub frmUserMaint_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
         
    
            dt = Me.Tbl_UsersTableAdapter.GetUsers
         
            bndSourceGrid.DataSource = dt
            DataGridView1.DataSource = bndSourceGrid ' setup binding source
    
            With DataGridView1
    
              
                Dim btnEdit As New DataGridViewButtonColumn()
                .Columns.Add(btnEdit)
                btnEdit.HeaderText = "Edit"
                btnEdit.Text = "Edit"
                btnEdit.Name = "btnEdit"
                btnEdit.FlatStyle = FlatStyle.Flat
                btnEdit.UseColumnTextForButtonValue = True
    
            End With
    
         End Sub
    
    
     Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
            Dim User As Integer
            Dim GroupName As String
    
            Me.Tbl_UserGroupsTableAdapter.FillbyID(Me.DataSet1.tbl_UserGroups, CInt(DataGridView1.Rows(e.RowIndex).Cells("UserGroupID").Value))
    
            If Me.DataSet1.tbl_UserGroups.Count > 0 Then
                Dim row As DataRow = DataSet1.tbl_UserGroups.Rows(0)
                GroupName = row.Item("UserGroupName").ToString
            End If
    
            If e.RowIndex >= 0 Then
                User = CInt(DataGridView1.Rows(e.RowIndex).Cells("ID").Value)
    
                With DataGridView1
    
                    If colName = "btnEdit" Then
                      
                        txtName.Text = .Rows(e.RowIndex).Cells("UserName").Value
                      
                    End If
    
                End With
                
            End If
    
      End Sub
    
    
          Private Sub txtSearchID_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearchID.TextChanged
                 bndSourceGrid.Filter = String.Format("{0} LIKE '%{1}%'", "UserID", txtSearchID.Text)
          End Sub
    
          Private Sub txtSearchID_Validated(sender As System.Object, e As System.EventArgs) Handles txtSearchID.Validated
             If txtSearchID.Text = String.Empty Then
                 bndSourceGrid.RemoveFilter()
             Else
                bndSourceGrid.Filter = String.Format("{0} = '{1}'", "UserID", txtSearchID.Text)
             End If
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview button not working when use databoundfilter

    Quote Originally Posted by jpskiller View Post
    it does not appear to work.
    Perhaps you could explain what that actually means? What EXACTLY do you expect to happen and what EXACTLY does actually happen?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview button not working when use databoundfilter

    Also, have you actually debugged (i.e. set a breakpoint and stepped through the code) the CellContentClick event handler to see exactly where the path of execution and/or the application state (i.e. values of variables, etc.) isn't as expected? If not, you should do that now. If so, you should explain what you found.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    122

    Re: Datagridview button not working when use databoundfilter

    Hi, sorry.

    When the grid is unfiltered then it fires the DataGridView1_CellContentClick and executes the code as expected.

    If the grid is filtered then when I click on button, then DataGridView1_CellContentClick is not entered and does not execute any code but the datagrid just completely clears itself, until I remove the filter then grid fills back as normal.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview button not working when use databoundfilter

    Why are you filtering in both the TextChanged and Validated event handlers of that TextBox and doing it differently in each place? That doesn't make sense. I'm guessing that your issue is that Validated event handler, which will be executed when the TextBox loses focus, which would happen when clicking on a button in the grid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    122

    Re: Datagridview button not working when use databoundfilter

    Quote Originally Posted by jmcilhinney View Post
    Why are you filtering in both the TextChanged and Validated event handlers of that TextBox and doing it differently in each place? That doesn't make sense. I'm guessing that your issue is that Validated event handler, which will be executed when the TextBox loses focus, which would happen when clicking on a button in the grid.
    Your absolutely right it was the text box validate, I had forgotten to remove It, it was there because I was experimenting with text change / Validate methods.

    Thank you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width