Sorting a Data-Bound DGV by a non data bound column

Ok i have two problems to implement this

I lose my non-data-bound values if user clicks on a data-bound column to sort

example : (first column non-data-bound, second and third ones are data-bound)

ID Price Type

1 3000 $

2 2500 €

3 4500 $

If user sorts by Price or Type -i dont know why- all the values in my ID column becomes null (it has to do with my ID column being non-data-bound probably)

I'm looking for a solution to help me keep the data in my "ID" column when a user sorts other columns.

Also Sort doesn't work on my ID column too (probably because of the data getting deleted when i sort)




Answer this question

Sorting a Data-Bound DGV by a non data bound column

  • Dudets

  • Chronusus

    Do i have to write CellValueNeeded event only for filling unbounded columns or for whole DGV

  • Muhammad Masood

    Anyone

  • Jim Ward

    Just for the unbounded columns. In the event handler, you first need to judge whether the current cell belongs to the unbounded columns. if it does, provide the data, if not, return and do nothing.

  • UK_2006

    Hi KursatKonak,

    I believe in this situation, the virtual mode is needed. Mixing the binding column and unbinding column, data in unbinding column should be provided in CellValueNeeded event handler, otherwise the data will be lost because DGV do not take care of them. You also need to implement other event such as NewValuePushed,NewRowNeeded. Please refer to msdn about virtual mode.

    Hope this helps.



  • Paul Monaghan

    when mixing bound and unbound data, use virtual mode.

    http://windowssdk.msdn.microsoft.com/en-us/library/ms171622.aspx



  • douner001

    i dont have problem with sorting my data, i have problem with keeping my data on unbound columns thanks for help though.

    For Virtual Mode can i use a dataset instead of a dataretriever Meaning, getting data from DS and insert data into DGV myself row-by-row



  • syvers

    http://windowssdk.msdn.microsoft.com/en-us/library/0868ft3z.aspx

    If your DataGridView control contains both bound and unbound columns, you must implement virtual mode to maintain the values of the unbound columns when the control is sorted by a bound column. You can do this by setting the VirtualMode property to true and handling the CellValueNeeded event. If the column is editable, you should also handle the CellValuePushed event. For more information about virtual mode, see How to: Implement Virtual Mode in the Windows Forms DataGridView Control. Sorting by unbound columns when the control is data-bound is not supported.

    ...................

    Well i made my problem work out somehow

    Problem :

    Sort By an unbound column

    Solution :

    Make it data-bound -___________-

    God.

    I had to do this :

    Whenever user sorts by price column i will use real exchange rates to sort columns, meaning :

    Before Sort How i want them to be sorted How Automatic Sort Mode Would Sort

    1200 $ 1000 $ 1000 $

    1100 € -----> 1200 $ 1100 €

    1000 $ 1100 € 1200 $

    Because 1100 € > 1200 $ .

    If i had used automatic sort it would sort it wrong. By the way, There are two columns there "Price" and "MoneySymbol".

    Just because i couldn't sort by unbound column in virtual mode. I used an sql query like this to have a new column containing real values of prices based on the YTL (Turkish money unit)

    SELECT ...,Price AS [RPrice], ........ FROM InfoTable WHERE Sym = 'YTL'

    UNION

    SELECT ...,Price*DolarValue AS [RPrice], ........ FROM InfoTable WHERE Sym = '$'

    UNION

    SELECT ...,Price*EuroValue AS [RPrice], ........ FROM InfoTable WHERE Sym = '€'

    This solved my problem. Now i have a Data-Bound Real-price column which i use to sort my DGV.

    But.. this cant be called a generic solution. So i'm open for suggestions/ideas for future implementations.



  • Sorting a Data-Bound DGV by a non data bound column