Link to home
Start Free TrialLog in
Avatar of David Svedarsky
David SvedarskyFlag for United States of America

asked on

Column2 = Column1 if Column1 is numerical value

Databound datagridview:

I want Column2 value to = Column1 value only if Column1 is a numerical value.

If column1 is a punctuation symbol, such as ")" or (>)  Column2 value would = "0.00"

Thanks!
Avatar of PagodNaUtak
PagodNaUtak
Flag of Philippines image


If decimal.isnumeric(column1) AndAlso Column2 = Column1 then

End if

Open in new window

Avatar of Nasir Razzaq
Want a SQL approach?

Select Column1, Column2 = Case udf_IsNumeric(Column1)
                                             When 1 Then Column1
                                             Else 0.00
                                             End
From Table1


Now you can use the built in IsNumeric function but it returns 1 for +, -, $ as well so I used a custom function found in below link

http://blog.sqlauthority.com/2007/08/11/sql-server-udf-validate-integer-function/
Avatar of David Svedarsky

ASKER

Is this what you had in mind....I am getting error messages.

        If Decimal.isnumeric(TblBasicPlumbingBidDetailDataGridView.Columns("ItemNumber")) AndAlso TblBasicPlumbingBidDetailDataGridView.Columns("DataGridViewTextBoxColumn4") = TblBasicPlumbingBidDetailDataGridView.Columns("ItemNumber") Then

        End If

Open in new window

Hi!

Are you looking for the result as mention in the image ??

If yes then you have to do one thing:

Here row(0) have column1 values:
            DataGridView1.Rows(i).Cells(0).Value = row(0) 

Open in new window


and here VAL() will see that if column1 value is numeric, then take numeric value else if string make it zero. ( which i hope so required to you) and Format () will make it like 6.00,10.00 etc...
            DataGridView1.Rows(i).Cells(1).Value =Format(Val(row(0)), "0.00")

Open in new window


Here is the full code: ( I used Form_Load Event you can change it)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connstr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TestDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" '"Data Source=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\TestDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        Dim conn As New SqlConnection(connstr)
        Dim cmd As New SqlCommand("select column1,column2 from tbl_test", conn)
        Dim da As New SqlDataAdapter(cmd)
        conn.Open()
        Dim dt As New DataTable
        da.Fill(dt)
        conn.Close()
        Dim i As Integer = 0
        DataGridView1.AllowUserToAddRows = False
        For Each row As DataRow In dt.Rows
            DataGridView1.Rows.Add()
            DataGridView1.Rows(i).Cells(0).Value = row(0)
            DataGridView1.Rows(i).Cells(1).Value =Format(Val(row(0)), "0.00")
            i += 1
        Next
    End Sub

Open in new window

User generated image
I have taken image before FORMAT() function used in my code if you will test it you will get 0.00, 6.00 20.00 etc....
For your consistency i have attached the image again. User generated image
Shahan_Developer:

I created a sample application and am having problems with your detailed code above.
Could I please ask you to apply your code to the following.

Public Class Form1

    Private Sub Tbl_testBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tbl_testBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Tbl_testBindingSource.EndEdit()
        Me.Tbl_testTableAdapter.Update(Me.LivePersonSQLDataSet.tbl_test)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'LivePersonSQLDataSet.tbl_test' table. You can move, or remove it, as needed.
        Me.Tbl_testTableAdapter.Fill(Me.LivePersonSQLDataSet.tbl_test)
End Sub
End Class

Open in new window


Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What should column1 and Column2 data type be?
i 've used varchar for both but you can used varchar for column1 and int or decimal etc  for column2.
Have you ruled out the possibility of achieving this using SQL? http:#36151676
CodeCruiser,

I am not that familiar with using SQL and making it work with a click event. Ideally I can make this work with a ColumnDoubleClick event.

Thanks for following up.