?
Solved

Edit cell in datagrid - change not being retained

Posted on 2005-04-05
9
Medium Priority
?
273 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

615 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