?
Solved

Edit cell in datagrid - change not being retained

Posted on 2005-04-05
9
Medium Priority
?
265 Views
Last Modified: 2008-02-01
I am developing a WinForms application in VB.Net.  I have a datagrid containing several columns.  One of the columns is a decimal - eg. 3,000.00.

If the user clicks in the cell to edit the figure and changes just the last digit or two (to make 3,000.01, say) then as soon as he moves away from the cell, the change is lost.  BUT, if he instead clicks in the cell and overtypes the entire "3000.01" the change is retained.

I have spent hours re-programming stuff only to discover that this seems to be a datagrid issue as opposed to a logic error!!

Please help - TIA   :D
0
Comment
Question by:Sigh_Man
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13714124
seems to be a bug???
I tried quite a lot but could not get to the bottom of it....
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13714442
Hi Sigh_man, Just a question, I assume you are using SQL server, is the column in the table defined as decimal, or
as some other numeric type (like real).
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13714845
Thanks for the comments.

I have worked out what the problem is:
I have a Table Style with a column formatted to #,##0.00.  I have worked out that the "," is the problem.  If the user enters 3000.00 into the column, the system changes this to 3,000.00.  This is exactly what I want, except that if the user later tried to change the 3,000.00 to 3,000.10 he or she must remove the comma otherwise the change is not accepted.

How can I retain the current format whilst at the same time having the datagrid accept (and ignore) a comma in the entry?

Thanks again....
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 1600 total points
ID: 13715057
Hi sigh_man, Try this (it seems to work :)

Class CRTextBoxColumn
    Inherits DataGridTextBoxColumn

    Protected Overrides Function GetColumnValueAtRow(ByVal cm As CurrencyManager, ByVal RowNum As Integer) As Object
        Dim oVal As Object = MyBase.GetColumnValueAtRow(cm, RowNum)
        If oVal.GetType Is GetType(DBNull) Then
            Return ""                         
        Else
            Dim Temp As Decimal = CDec(oVal)
            Return Temp.ToString("#,##0.00")
        End If
    End Function

    Protected Overrides Function Commit(ByVal cm As CurrencyManager, ByVal RowNum As Integer) As Boolean
        Me.HideEditBox()   ' return focus to the DataGrid control
        Dim box As DataGridTextBox = CType(Me.TextBox, DataGridTextBox), Value As Decimal
        If box.IsInEditOrNavigateMode Then Return True
        If TextBox.Text = "" Then   ' in this example, "" maps to DBNull
            SetColumnValueAtRow(cm, RowNum, DBNull.Value)
        Else
            Try
                Value = Decimal.Parse(TextBox.Text)
            Catch
                Return False    
            End Try
            SetColumnValueAtRow(cm, RowNum, Value)   ' Write new value.
        End If
        Me.EndEdit()  
        Return True    
    End Function

End Class

use like

        SqlDataAdapter1.Fill(dt)

        Dim ts As New DataGridTableStyle
        ts.MappingName = dt.TableName

        Dim dgtbc3 As New CRTextBoxColumn
        dgtbc3.MappingName = "test"
        dgtbc3.Width = 100

        ts.GridColumnStyles.Add(dgtbc3)
        Me.DataGrid1.TableStyles.Add(ts)
        DataGrid1.DataSource = dt

0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13715099
Thanks
I'll get back to you shortly...  :D
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13715129
Ronald,
Sorry, my table style was set in the form designer/properties.  I take it I need to clear this and use your code to set the table style?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13715131
Yes :-)
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13715997
Ronald,
That worked perfectly - thanks mate!!!    :D
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13716189
Perfect RonaldBiemans
So that "," makes it fail to qulaify as a numeric value is it ???
I wonder if there could have been some more intelligence with the Grid after all
IsNumeric is able to parse ","

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question