Monday, 27 July 2015

How to insert Only Numeric Value in Datagridview in vb.net

 Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As         System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
 End Sub
 Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True
        If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then e.Handled = True
        If e.KeyChar = Convert.ToChar(Keys.Back) Then e.Handled = False ' Check back space key
  End Sub

how to find number from string in vb.net

'add name space in vb.net code
Imports System.Text.RegularExpressions  

' Code for find number from string
 Dim x As String = "123a123&*^*&^*&^*&^   a sdsdfsdf"
        MsgBox(Integer.Parse(Regex.Replace(x, "[^\d]", "")))

How to find text change in data grid view and calculate in data grid view in vb.net

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim table As New DataTable

        With table.Columns
            .Add("Column1", GetType(Integer))
            .Add("Column2", GetType(Integer))
            .Add("Sum", GetType(Integer))
        End With
        Me.DataGridView1.DataSource = table
    End Sub
    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler e.Control.TextChanged, AddressOf CellTextChanged
    End Sub

    Private Sub CellTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim txt As DataGridViewTextBoxEditingControl
        Dim col1 As Integer
        Dim col2 As Integer

        Try
            If DataGridView1.CurrentCell.ColumnIndex = 0 Then
                txt = DirectCast(sender, DataGridViewTextBoxEditingControl)
                If txt.Text.Length > 0 Then
                    col1 = Integer.Parse(txt.Text)
                End If
                If DataGridView1.CurrentRow.Cells("Column2").Value & "" <> "" Then
                    col2 = Integer.Parse(DataGridView1.CurrentRow.Cells("Column2").Value)
                End If
            ElseIf DataGridView1.CurrentCell.ColumnIndex = 1 Then
                txt = DirectCast(sender, DataGridViewTextBoxEditingControl)
                If DataGridView1.CurrentRow.Cells("Column1").Value & "" <> "" Then
                    col1 = Integer.Parse(DataGridView1.CurrentRow.Cells("Column1").Value)
                End If
                If txt.Text.Length > 0 Then
                    col2 = Integer.Parse(txt.Text)
                End If
            End If
            DataGridView1.CurrentRow.Cells("sum").Value = col1 + col2
        Catch ex As Exception
            MessageBox.Show("Input string was not in a correct format.")
        End Try
    End Sub

Saturday, 25 July 2015

Count Sunday between two dates in sql and Count days in a month in sql

declare @d1 datetime, @d2 datetime select @d1 = '9/1/2014',@d2= '9/30/2014';with dates (date )as(select @d1 union all select dateadd(d,1,date) from dates where date < @d2 )select date from dates where  datename(dw,date) = 'Sunday'

SELECT DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,'2/4/2015'),0)))