Thursday, 28 November 2013

how to merge cells in datagridview using vb.net

'e.g. Vertically merge the same cells of the fourth column

provide column index in below code to merge the cells

 
  Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

        If e.ColumnIndex = 3 AndAlso e.RowIndex <> -1 Then

            Using gridBrush As Brush = New SolidBrush(Me.DataGridView1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)

                Using gridLinePen As Pen = New Pen(gridBrush)
                    ' Clear cell
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

                    ' Draw line (bottom border and right border of current cell)
                    'If next row cell has different content, only draw bottom border line of current cell
                    If e.RowIndex < DataGridView1.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                    End If

                    ' Draw right border line of current cell
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)

                    ' draw/fill content in current cell, and fill only one cell of multiple same cells
                    If Not e.Value Is Nothing Then
                        If e.RowIndex > 0 AndAlso DataGridView1.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
                        Else
                            e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
                        End If
                    End If

                    e.Handled = True
                End Using
            End Using
        End If
    End Sub 

Wednesday, 27 November 2013

Calculate time difference between two dates

Public Function TimeDifference(ByVal fd As String, ByVal ld As String) As String

fd=first date
ld=last date
simply pass fd and ld in datediff function given below

Public Function TimeDifference(ByVal fd As String, ByVal ld As String) As String
        Dim lastcomctonday As String = DateDiff("d", fd, ld)
        Dim lastcomcton As String = DateDiff("n", fd, ld)
        'Dim lastcomsec As String = DateDiff("s", fd, ld)
        If Val(lastcomcton) < 60 Then
            timedif = "After Complaint: " & "0 Hours " & lastcomcton & " Minutes Later"   '"Same Day " &
        ElseIf Val(lastcomcton) / 60 < 24 Then
            timedif = "After Complaint: " & CStr(Math.Round(Val(lastcomcton) / 60, 0)) & " Hour(s) Later"  '"Same Day " &
        Else
            timedif = "After Complaint: " & lastcomctonday & " Days Later"
        End If
        Return timedif
    End Function

System.Data.SQLClient ,System.Data.OledbClient ,System.Data.ODBCClient

System.Data.SQLClient

Can only be used for connecting to SQL Server 2000 and later. But you will get optimal performance when connecting to those databases.
System.Data.OledbClient

Can be used to connected to SQL 6.5

OLEDBClient gives you ability to connect to other database like ORALCE or Access. But for working with SQL Server you will get better performance using SQLClient.

Note: For connecting to ORALCE Microsoft also has ORACLEClient.

System.Data.ODBCClient- provides connection to legacy databases ( e.g. MS Access 97) using ODBC drivers.

How do I open SQL Server Management Studio?

For SQL Server 2005, click Start -> Run then type sqlwb

This will open SQL Server Management Studio

How to send email

Use these namespace and add function to your event on which you want to send email.
*****************************************************************
Imports System.Web
Imports System.Net
Imports System.Net.Mail

Public Function sndmail()
Try

 Dim client As New SmtpClient()
               Dim sendTo As New MailAddress("email of other person")
                Dim from As MailAddress = New MailAddress("youremail")
                Dim message As New MailMessage(from, sendTo)
                message.IsBodyHtml = True
                message.Subject = "Mail subject"
                message.IsBodyHtml = True
                Dim st As String = "Your Mail Body "               
                message.Body = st
                message.IsBodyHtml = True
                Dim basicAuthenticationInfo As New System.Net.NetworkCredential("yourfullemail", "password")
                client.Host = "smtp.gmail.com"
                client.UseDefaultCredentials = False
                client.Credentials = basicAuthenticationInfo
                client.EnableSsl = True
                client.Send(message)

         Catch ex As Exception
            Response.Write("")
        End Try
End Function