locked
how manage the click on column header name? RRS feed

  • Question

  • i want when i make select query from database

    the headercell reorder like i put it in database ("column name numseq")

    my problem when i click the name of headercolumn it looks like it freez in same order not changing at all

        Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
            Dim col_name = DataGridView1.Columns(e.ColumnIndex).Name
     If Label11.Text = "Enregistrée" Then
                    If cn.State = ConnectionState.Open Then
                        cn.Close()
                    End If
                    cn.Open()
                Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by '" & Trim(col_name) & "'", cn)
                Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
                dt2.Columns.Add("Codearticle")
                dt2.Columns.Add("Designation")
                dt2.Columns.Add("Qte")
                dt2.Columns.Add("PrixUnitaire")
                dt2.Columns.Add("Remise")
                dt2.Columns.Add("ReferenceFournisseur")
                dt2.Columns.Add("NumeroEnsemble")
                dt2.Columns.Add("Marge")
    
                MessageBox.Show(col_name)
                While da1.Read
                    dt2.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
    
                    ' MessageBox.Show(da1(0) & " ! " & da1(1) & " ! " & da1(2) & " ! " & da1(3) & " ! " & da1(4) & " ! " & da1(5) & " ! " & da1(6) & " ! " & da1(7))
                    tabNum2.Rows.Add(da1(8))
                End While
    
                ' da1.Fill(dt1)
                If cn.State = ConnectionState.Open Then
                        cn.Close()
                    End If
                cn.Open()
                Dim colnameDLligne As String
                If col_name = "Codearticle" Then
                    colnameDLligne = "AR_REF"
                Else
                    colnameDLligne = col_name
    
                End If
                Dim cmd2453 As New SqlCommand("select do_piece from dl_ligne_table where Numero='" & str1 & "' order by '" & colnameDLligne & "'", cn)
                Dim dab As SqlDataReader = cmd2453.ExecuteReader
                    Dim i As Integer
                While dab.Read And i <= dt2.Rows.Count - 1
    
                    dt2.Rows(i).Item("Numero") = dab("do_piece")
                    i += 1
                    End While
    
                While i <= dt2.Rows.Count - 1
                    dt2.Rows(i).Item("Numero") = str1
                    i += 1
                    End While
                End If
    
    
            DataGridView1.DataSource = dt2
    
            For i = 0 To tabNum2.Rows.Count - 1
                DataGridView1.Rows(i).HeaderCell.Value = tabNum2.Rows(i).Item(0)
    
            Next

    Monday, June 22, 2020 10:36 AM

Answers

  • i make it work

     If (e.ColumnIndex) = 0 Then
    
    
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by numero", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
    
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                ElseIf (e.ColumnIndex) = 1 Then
    
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by Codearticle", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
    
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                ElseIf (e.ColumnIndex) = 2 Then
    
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by Designation", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                Else
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by numseq", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                End If

    works fine but i don't like this type of code


    • Edited by Houssem12 Tuesday, June 23, 2020 9:51 AM
    • Marked as answer by Houssem12 Tuesday, June 23, 2020 2:28 PM
    Tuesday, June 23, 2020 9:51 AM

All replies

  • Did you try using HeaderText rather than HeaderCell.Value?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, June 22, 2020 11:43 AM
  • i will try that

    but how i manage the click of the header

    Monday, June 22, 2020 12:17 PM
  • i will try that

    but how i manage the click of the header

    Well I would not be doing what you have in ColumnHeaderMouseClick in regards to loading data, you should do that in another event.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, June 22, 2020 1:03 PM
  • when i write the name of the column manually it work

    when i put it in variable it didn't work

    i don't understand why

     Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by '" & colname & "'", cn)
            Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
    
                While da1.Read
                    dt2.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                    tabNum2.Rows.Add(da1(8))
                End While
     Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by [Designation]", cn)
            Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
    
                While da1.Read
                    dt2.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                    tabNum2.Rows.Add(da1(8))
                End While

    Monday, June 22, 2020 1:53 PM
  • same result with columnIndex please help
    Monday, June 22, 2020 3:59 PM
  • Hi Houssem,
    try this and post result from Immediate Window:

    Dim sqlString = $"select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero=[{str1}] order by [{colname}]"
    
    Debug.Print(sqlString)
       
    Dim cmd2 As New SqlCommand(sqlString, cn)
       


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Monday, June 22, 2020 4:21 PM
  • i make it work

     If (e.ColumnIndex) = 0 Then
    
    
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by numero", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
    
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                ElseIf (e.ColumnIndex) = 1 Then
    
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by Codearticle", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
    
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                ElseIf (e.ColumnIndex) = 2 Then
    
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by Designation", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                Else
                    Dim cmd2 As New SqlCommand("select Codearticle,Designation,Qte,PrixUnitaire,Remise,ReferenceFournisseur,NumeroEnsemble,Marge,numseq from DetailReceptionFrs where Numero='" & str1 & "' order by numseq", cn)
                    Dim da1 As SqlDataReader = (cmd2.ExecuteReader)
                    While da1.Read
                        dt5.Rows.Add("", da1(0), da1(1), da1(2), da1(3), da1(4), da1(5), da1(6), da1(7))
                        tabNum.Rows.Add(da1(8))
                    End While
                End If

    works fine but i don't like this type of code


    • Edited by Houssem12 Tuesday, June 23, 2020 9:51 AM
    • Marked as answer by Houssem12 Tuesday, June 23, 2020 2:28 PM
    Tuesday, June 23, 2020 9:51 AM