Sort a Datagridview with code.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Freddy Coal

    Sort a Datagridview with code.

    Hi, I'm learning to use the Datagridview; I would like sort a column of my
    DGV, but with code, not with user click over the column, how make that?; Mi
    other question is how
    search for a especific chain in my DGV?.

    Thanks in advance for any help, I'm using VB.Net in VS2005.

    Freddy Coal


  • RobinS

    #2
    Re: Sort a Datagridview with code.

    When you bind your DataGridView to your datasource, use a bindingsource.

    Dim nwData as CustomersDataSe t = CustomersDataSe t.GetCustomers( )
    m_CustomersGrid .DataSource = m_CustomersBind ingSource
    m_CustomersBind ingSource.DataS ource = nwData.Customer s

    Then you can sort using the BindingSource.

    CustomersBindin gSource.Sort = "ContactNam e ASC"

    And you can find using the BindingSource.

    Dim index as integer = _
    CustomersBindin gSource.Find("C ompanyName", CompanyNameText Box.Text)
    If index <-1 then 'it was found; move to that position
    CustomersBindin gSource.Positio n = index
    End If


    Robin S.
    -------------------------------------------
    "Freddy Coal" <freddycoal@gma il.comwrote in message
    news:OO87rFbWHH A.600@TK2MSFTNG P05.phx.gbl...
    Hi, I'm learning to use the Datagridview; I would like sort a column of
    my
    DGV, but with code, not with user click over the column, how make that?;
    Mi other question is how
    search for a especific chain in my DGV?.
    >
    Thanks in advance for any help, I'm using VB.Net in VS2005.
    >
    Freddy Coal
    >

    Comment

    • Freddy Coal

      #3
      Re: Sort a Datagridview with code.

      Hi Robin, Thanks for your answer, I solve the problem with this code:

      DGV.Sort(DGV.Co lumns(0), System.Componen tModel.ListSort Direction.Ascen ding)
      'Where DGV is a DataGridView

      Excuse me, but I don´t understand your second answer; I would like search a
      chain (form example "Claymore Street") inside of my DGV; I make that
      searching cell by cell with row and columns cycles, but I would like know if
      Vb.Net have any instruction for make this search.

      Thanks a lot again, your help is very appreciated.

      Freddy Coal

      "RobinS" <RobinS@NoSpam. yah.nonewrote in message
      news:DqudnRrPE9 4bQn7YnZ2dnUVZ_ s-rnZ2d@comcast.c om...
      When you bind your DataGridView to your datasource, use a bindingsource.
      >
      Dim nwData as CustomersDataSe t = CustomersDataSe t.GetCustomers( )
      m_CustomersGrid .DataSource = m_CustomersBind ingSource
      m_CustomersBind ingSource.DataS ource = nwData.Customer s
      >
      Then you can sort using the BindingSource.
      >
      CustomersBindin gSource.Sort = "ContactNam e ASC"
      >
      And you can find using the BindingSource.
      >
      Dim index as integer = _
      CustomersBindin gSource.Find("C ompanyName", CompanyNameText Box.Text)
      If index <-1 then 'it was found; move to that position
      CustomersBindin gSource.Positio n = index
      End If
      >
      >
      Robin S.
      -------------------------------------------
      "Freddy Coal" <freddycoal@gma il.comwrote in message
      news:OO87rFbWHH A.600@TK2MSFTNG P05.phx.gbl...
      >Hi, I'm learning to use the Datagridview; I would like sort a column of
      >my
      >DGV, but with code, not with user click over the column, how make that?;
      >Mi other question is how
      >search for a especific chain in my DGV?.
      >>
      >Thanks in advance for any help, I'm using VB.Net in VS2005.
      >>
      >Freddy Coal

      Comment

      • RobinS

        #4
        Re: Sort a Datagridview with code.

        I already answered that question in my previous post. If you want to know
        how to do it without using a BindingSource, maybe someone else will answer
        you.

        Robin S.
        ----------------------------------------
        "Freddy Coal" <freddycoal@gma il.comwrote in message
        news:uHt1$XoWHH A.3332@TK2MSFTN GP04.phx.gbl...
        Hi Robin, Thanks for your answer, I solve the problem with this code:
        >
        DGV.Sort(DGV.Co lumns(0),
        System.Componen tModel.ListSort Direction.Ascen ding) 'Where DGV is a
        DataGridView
        >
        Excuse me, but I don´t understand your second answer; I would like search
        a chain (form example "Claymore Street") inside of my DGV; I make that
        searching cell by cell with row and columns cycles, but I would like know
        if Vb.Net have any instruction for make this search.
        >
        Thanks a lot again, your help is very appreciated.
        >
        Freddy Coal
        >
        "RobinS" <RobinS@NoSpam. yah.nonewrote in message
        news:DqudnRrPE9 4bQn7YnZ2dnUVZ_ s-rnZ2d@comcast.c om...
        >When you bind your DataGridView to your datasource, use a bindingsource.
        >>
        >Dim nwData as CustomersDataSe t = CustomersDataSe t.GetCustomers( )
        >m_CustomersGri d.DataSource = m_CustomersBind ingSource
        >m_CustomersBin dingSource.Data Source = nwData.Customer s
        >>
        >Then you can sort using the BindingSource.
        >>
        >CustomersBindi ngSource.Sort = "ContactNam e ASC"
        >>
        >And you can find using the BindingSource.
        >>
        >Dim index as integer = _
        > CustomersBindin gSource.Find("C ompanyName", CompanyNameText Box.Text)
        >If index <-1 then 'it was found; move to that position
        > CustomersBindin gSource.Positio n = index
        >End If
        >>
        >>
        >Robin S.
        >-------------------------------------------
        >"Freddy Coal" <freddycoal@gma il.comwrote in message
        >news:OO87rFbWH HA.600@TK2MSFTN GP05.phx.gbl...
        >>Hi, I'm learning to use the Datagridview; I would like sort a column of
        >>my
        >>DGV, but with code, not with user click over the column, how make
        >>that?; Mi other question is how
        >>search for a especific chain in my DGV?.
        >>>
        >>Thanks in advance for any help, I'm using VB.Net in VS2005.
        >>>
        >>Freddy Coal
        >
        >

        Comment

        Working...