Results 1 to 13 of 13

Thread: Another thread on Datagridview sort

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Another thread on Datagridview sort

    Hi everyone,
    this is another thread about how to sort the Datagridview. In my scenario I've a datagridview and this is the code I use to fill it:
    vb Code:
    1. ...declaration of cmdStoredProc...
    2.         cn.Open()
    3.         cmdStoredProc.ExecuteNonQuery()
    4.  
    5.     Dim da As OracleDataAdapter = New OracleDataAdapter(cmdStoredProc)
    6.     Dim ds As DataSet = New DataSet
    7.     da.Fill(ds, cNomeSP)
    8.     MyFillDGV(dgvGriglia, ds)
    9.     cn.Close()
    and then, this is MyFillDGV:

    vb Code:
    1. dgvGriglia.AutoGenerateColumns = False
    2.     dgvGriglia.AllowUserToAddRows = False
    3.     dgvGriglia.AllowUserToDeleteRows = False
    4.     dgvGriglia.AllowUserToResizeRows = False
    5.     dgvGriglia.AllowUserToOrderColumns = True
    6.     dgvGriglia.MultiSelect = False
    7.     dgvGriglia.ReadOnly = True
    8.     dgvGriglia.RowHeadersVisible = False
    9.     dgvGriglia.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
    10.     dgvGriglia.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    11.  
    12.     Dim oCol As New DataGridViewTextBoxColumn
    13.     oCol.Name = cColName
    14.     oCol.DefaultCellStyle.Alignment = oAlignment
    15.     oCol.Width = VB6.TwipsToPixelsX(nTwWidth)
    16.     oCol.HeaderText = cHeaderText
    17.     oCol.DataPropertyName = cDataPropertyName
    18.     dgvGriglia.columns.add(oCol)
    19.     dgvGriglia.Columns(0).SortMode = DataGridViewColumnSortMode.Automatic
    20.  
    21.     dgvGriglia.DataSource = ds.Tables(0).Select(cFilter)

    So it works very well only if i set AutoGenerateColumns = True.
    Why this code doesn't work with the autogeneratecolumns property set to false and the runtime binding of each column?
    Thank you in advance for your reply.

    Best regards

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Another thread on Datagridview sort

    try this:

    vb Code:
    1. dgvGriglia.AutoGenerateColumns = False
    2. dgvGriglia.AllowUserToAddRows = False
    3. dgvGriglia.AllowUserToDeleteRows = False
    4. dgvGriglia.AllowUserToResizeRows = False
    5. dgvGriglia.AllowUserToOrderColumns = True
    6. dgvGriglia.MultiSelect = False
    7. dgvGriglia.ReadOnly = True
    8. dgvGriglia.RowHeadersVisible = False
    9. dgvGriglia.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
    10. dgvGriglia.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    11.  
    12. Dim oCol As DataGridViewTextBoxColumn = DirectCast(dgvGriglia.Columns(0), DataGridViewTextBoxColumn)
    13. oCol.Name = cColName
    14. oCol.DefaultCellStyle.Alignment = oAlignment
    15. oCol.Width = VB6.TwipsToPixelsX(nTwWidth)
    16. oCol.HeaderText = cHeaderText
    17. oCol.DataPropertyName = cDataPropertyName
    18. dgvGriglia.Columns(0).SortMode = DataGridViewColumnSortMode.Automatic
    19.  
    20. dgvGriglia.DataSource = ds.Tables(0).Select(cFilter)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Another thread on Datagridview sort

    Hi Paul, thank you for your reply.
    These set of instructions:
    vb Code:
    1. Dim oCol As New DataGridViewTextBoxColumn
    2.     oCol.Name = cColName
    3.     oCol.DefaultCellStyle.Alignment = oAlignment
    4.     oCol.Width = VB6.TwipsToPixelsX(nTwWidth)
    5.     oCol.HeaderText = cHeaderText
    6.     oCol.DataPropertyName = cDataPropertyName
    7.     dgvGriglia.columns.add(oCol)
    is in another function which returns a DataGridViewTextBoxColumn. So I can't call
    vb Code:
    1. DirectCast(dgvGriglia.Columns(0), DataGridViewTextBoxColumn
    becaus dgvGriglia has no columns.
    So finally the code is this:
    vb Code:
    1. what.Columns.Add(Add_TextBoxColToDGV("ColNome", 3000, "Nome"))
    where:
    -the first parameter ColName is the cColName;
    -the second parameter 3000 is the nWidth;
    -the third parameter Nome is the cHeaderText.
    oAlignment and cDataPropertyName are optional parameters of my function.
    So how can solve?
    Thank you.
    Last edited by motogpdesmo16; Aug 3rd, 2010 at 08:47 AM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Another thread on Datagridview sort

    try adding your columns at design time if you don't want to autogenerate them, then try the code i posted in post #2

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Another thread on Datagridview sort

    does it have to be a function that returns a DataGridViewTextBoxColumn? can't it be a sub where you pass the DataGridViewColumn as a parameter?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Another thread on Datagridview sort

    Quote Originally Posted by .paul. View Post
    does it have to be a function that returns a DataGridViewTextBoxColumn? can't it be a sub where you pass the DataGridViewColumn as a parameter?
    Sorry but the explanation It's not enough clear (for me)
    If I convert it from Function to Sub, and pass dgvGriglia as parameter, it has ever 0 columns, so how can i call
    vb Code:
    1. DirectCast(dgvGriglia.Columns(0), DataGridViewTextBoxColumn)
    if my grid has no columns??

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Another thread on Datagridview sort

    well, the problem is that my columns are not databound columns, because, after the datagridview datasource property assignment, I loop on each datarow to custom each cell content:
    vb Code:
    1. For Each oRiga As DataRow In ds.Tables(0).Select(cFilter)
    2.         dgvGriglia.Item(0, nRiga).Value = oRiga.Item("MYCOLUMN0").ToString
    3.         dgvGriglia.Item(1, nRiga).Value = oRiga.Item("MYCOLUMN1").ToString & " _  " &oRiga.Item("MYCOLUMN2").ToString
    4. ...
    5. ...
    6.         nRiga = nRiga + 1
    7.       Next
    So, it's possible to call the sort method on non bound columns??
    Last edited by motogpdesmo16; Aug 4th, 2010 at 01:55 AM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Another thread on Datagridview sort

    any help about my problem??
    Thank you in advance.

  9. #9
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Another thread on Datagridview sort

    If you want to sort, you could re-sort the datatable you have in memory, then redo your looping process to add the rows. if nothing else, add your rows, run your program and see if when you click on the column headers if it re-sorts..

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Another thread on Datagridview sort

    Personally I would setup my data in a BindingSource and make that the DataGridView source which then can be sorted using the BindingSource sort method. To keep the current row current you can work that out in the BindingSource position changed event.

    Another possibility is to use code as shown below
    Code:
    DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Another thread on Datagridview sort

    Sorry for the late reply.
    I can't call the sort method of the DataGridView, because the column are all unbounded.
    So the error is DataGridView control must be bound to an IBindingList object to be sorted.
    Moreover I can't pass the orderby parameter at the Select method of the dataset when I've this instruction:
    vb Code:
    1. dgvGriglia.DataSource = ds.Tables(0).Select(cFilter, cOrderBy)
    because the order clause is not depending on the database column name, but depending on datagridview column (remember that in a datagridview column I can merge two or more database column)...
    It's very hard now...

  12. #12
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Another thread on Datagridview sort

    Here is a suggestion, retrieve your data into a DataTable and manipulate it there rather than directly in the DataGridView columns then assign the DataTable to a BindingSource followed by using the BindingSource as the DataSource for the DataGridView. In short now any changes made to the DataGridView will be made to the underlying data table.

    The attached demo is in VS2008.
    Create a new Windows project, on form1 place a DataGridView and three buttons cmdTest, cmdSort, cmdClose. Next copy all the code in my Form1 into your form1. Copy Customer.xml from my project to the same location in your project.

    Build and run.


    Note in Sub LoadData I load the xml file into a DataTable, manually setup the columns for the DataGridView, assign data to the BindingSource which in turn is the data source for the DataGridView. Next I use your settings (I commented this section), sort on column 0 and set focus to the Grid.

    cmdSort button flip flops the sort on column zero between ascending/descending.

    cmdTest replaces
    Code:
    dgvGriglia.DataSource = ds.Tables(0).Select(cFilter, cOrderBy)
    with
    Code:
          If mCustomersBindingSource.Filter Is Nothing Then
             mCustomersBindingSource.Filter = "City='London'"
          Else
             mCustomersBindingSource.Filter = ""
          End If
    which would have been the select then executes code in cmdSort to do the sorting/order by portion of the select statement.


    The only thing not included is manipulation of data as you mentioned in the part where you change data in columns of the DataGridView instead of changing it in the DataTable which is easy enough to do.

    Hope this helps.
    Last edited by kareninstructor; Aug 11th, 2011 at 07:56 AM.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Another thread on Datagridview sort

    Doh, I've just resolved the problem a few hours ago in this way:

    vb Code:
    1. dgvGriglia.AutoGenerateColumns = True
    2. Dim dt As New DataTable
    3. dt.Columns.Add(xAdd_ColToDT("Column0", "Header of column0"))
    4. dt.Columns.Add(xAdd_ColToDT("Column1", "Header of column1"))
    5. ...
    6. dt.Columns.Add(xAdd_ColToDT("ColumnN", "Header of columnN."))
    7.     For Each origa As DataRow In ds.Tables(0).Select(cFilter)
    8.       Dim oNewRiga As DataRow = dt.NewRow
    9.       oNewRiga.Item(0) = origa.Item("FIELD0")
    10.       oNewRiga.Item(1) = origa.Item("FIELD1")
    11.       dt.Rows.Add(oNewRiga)
    12.     Next
    13.     ds.Tables.Add(dt)
    14.     dgvGriglia.DataSource = ds.Tables(1)
    attention: dgvGriglia.DataSource = ds.Tables(1) is not an error because the table 0 is the source table which I manipulate in this procedure.
    I resolved anyway, but do you think could be a good solution??
    Thank you!

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