Tuesday, 14 May 2013

WPF - Tab Control - Selected Tab Item Header To Bold


private void tabControl1_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            try
            {
                for (int i = 0; i <= 3; i++)
                {
                    System.Windows.Controls.TabItem tab1 = tabControl1.Items.GetItemAt(i) as System.Windows.Controls.TabItem;
                    tab1.FontWeight = FontWeights.Normal;
                }

                System.Windows.Controls.TabItem tab = tabControl1.SelectedItem as System.Windows.Controls.TabItem;

                tab.FontWeight = FontWeights.Bold;
            }
            catch (Exception)
            {
            }
        }

Wednesday, 1 May 2013

Print Datagridview in WinForms - VB.Net,Multiple page,Landscape.

Result 

Overview
  1.  This post help to print data from datagridview.
  2.  Landscape,A4,Multiple page,no of copies are available.
  3.  'dgDatagrid' is datagridview name.
Code With Explanation

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        'Creating PrintDocument
        Dim gridViewDoc As New Printing.PrintDocument
        'Adding Custom Print event
        AddHandler gridViewDoc.PrintPage, AddressOf GridView_Print
        'Setting Number of copies
        gridViewDoc.PrinterSettings.Copies = 2
        'Setting Landscape mode
        gridViewDoc.DefaultPageSettings.Landscape = True
        'Invoke event 
        gridViewDoc.Print()
  End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=sa; Password=password"
        cnn = New SqlConnection(connectionString)
        cnn.Open()
        sqlAdp = New SqlDataAdapter("SELECT *  FROM [RamTables].[dbo].[StudentDetails]", cnn)
        cnn.Close()

        sqlAdp.Fill(ds)

        dgDatagrid.DataSource = ds.Tables(0)
    End Sub


    Private Sub GridView_Print(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        'Calling the methodw with graphic to draw lines and string which looks like table
        PrintGridView(e.Graphics)
        'For Printing more pages if e.Morepages = true this method will start creating new pages
        If printPending = True Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub


    Private Sub PrintGridView(ByVal print As Graphics)
        'To fill backgroud of header
        FillHeaders(print)
        'To create header row
        DataGridViewHeader(print)
        'To create all row in datagrid
        DataGridViewRow(print)
        'To draw a border for the table
        DataGridViewBox(print)
    End Sub

    Private Sub FillHeaders(ByVal print As Graphics)
        'draw a rectangle with gray background to attach in header row
        Dim rectangle As New Rectangle(CInt(12.5), dgDatagrid.Top + 50, CInt(dgDatagrid.Width * 1.367),   CInt(dgDatagrid.Rows(0).Height - 4.3))
        Dim brush As New SolidBrush(Color.LightGray)
        print.FillRectangle(brush, rectangle)
    End Sub

    Private Sub DataGridViewHeader(ByVal print As Graphics)
        Dim cellText As String = String.Empty
        Dim startTop As Integer = dgDatagrid.Top + 50
        Dim title As String = String.Empty
        Dim printfont As New Font(New FontFamily("Arial"), 7, FontStyle.Bold)
        Dim titlefont As New Font(New FontFamily("Arial"), 12, FontStyle.Bold)
        Dim colIndex As Integer = 0
        Dim startLeft As Integer = 12
        Dim center As Integer
        Dim size As SizeF
        Dim headerWidth As Integer
        title = "List Of Subscribers"
        print.DrawString(title, titlefont, Brushes.Black, 310, 8)
        For Each printcolumn As DataGridViewColumn In dgDatagrid.Columns
            'Read the header column text and calculate the centre position of teh cell and write it
            Using measure As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New Bitmap(1, 1))
                size = measure.MeasureString(dgDatagrid.Columns(colIndex).HeaderText, New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point))
            End Using
            headerWidth = CInt(Math.Floor(Convert.ToDecimal(CType(size.Width, String), CultureInfo.CurrentCulture)))
            For calculate As Integer = 0 To dgDatagrid.Columns(colIndex).Width
                Dim firsthalf As Integer = calculate
                Dim secondhalf As Integer = dgDatagrid.Columns(colIndex).Width - (calculate + headerWidth)
                If firsthalf = secondhalf Then
                    center = calculate
                    Exit For
                ElseIf (firsthalf = secondhalf - 1) Then
                    center = calculate
                    Exit For
                End If
            Next
            startLeft = CInt(startLeft * 1.1)
            cellText = printcolumn.HeaderText
            'Write the header text
            print.DrawString(cellText, printfont, Brushes.Black, startLeft + center, startTop + 5)
            'Draw line that seprate each column
            print.DrawLine(Pens.Black, startLeft, startTop, startLeft, startTop + CInt(dgDatagrid.Rows(0).Height - 4.3))
            startLeft += dgDatagrid.Columns(colIndex).Width - 20
            colIndex += 1
        Next
        'Draw line that seprate each row
        print.DrawLine(Pens.Black, 13, startTop + CInt(dgDatagrid.Rows(0).Height - 4.3), CInt(dgDatagrid.Width * 1.4), CInt(startTop + dgDatagrid.Rows(0).Height - 4.3))
    End Sub



    Private Sub DataGridViewRow(ByVal print As Graphics)
        Dim rowIndex As Integer = 1
        Dim printFont As New Font(New FontFamily("Arial"), 7, FontStyle.Bold)
        While printPending = True
            'Get each cell text in row and write it as in grid
            For pointedIndex = pointedIndex To dgDatagrid.Rows.Count - 1
                Dim starttop As Integer = dgDatagrid.Top + CInt(rowIndex * (dgDatagrid.Rows(pointedIndex).Height - 4.3)) + 50
                Dim colIndex As Integer = 0
                Dim startLeft As Integer = 12

                For Each printCell As DataGridViewCell In dgDatagrid.Rows(pointedIndex).Cells
                    startLeft = CInt(startLeft * 1.1)
                    Dim cellText As String = String.Empty
                    If (Not IsDBNull(printCell.Value)) Then cellText = CStr(printCell.Value)
                    print.DrawString(cellText, printFont, Brushes.Black, startLeft, starttop + 5)
                    print.DrawLine(Pens.Black, startLeft, starttop, startLeft, starttop + CInt(dgDatagrid.Rows(pointedIndex).Height - 4.3))
                    startLeft += dgDatagrid.Columns(colIndex).Width - 20
                    colIndex += 1
                Next
                print.DrawLine(Pens.Black, 12, starttop + CInt(dgDatagrid.Rows(pointedIndex).Height - 4.3), CInt(dgDatagrid.Width * 1.4), starttop + CInt(dgDatagrid.Rows(pointedIndex).Height - 4.3))
                rowIndex += 1
                rowInPage = rowIndex
                'Exit when 20 rows is written, so that we can have 20 row in each page  
                If rowIndex = 20 Then
                    pointedIndex = pointedIndex + 1
                    Exit While
                End If
                'If still data is pending to print keep printPrinting variable as false so that other data can come in next page
                If pointedIndex = dgDatagrid.Rows.Count - 1 Then
                    printPending = False
                End If
            Next
        End While
    End Sub

    Private Sub DataGridViewBox(ByVal print As Graphics)
        'Draw a rectangle border for the  lines so that it forms a table
        Dim gridviewRect As New Rectangle(CInt(12.5), dgDatagrid.Top + 50, CInt(dgDatagrid.Width * 1.368), CInt((dgDatagrid.Rows(0).Height - 4.3) * rowInPage))
        print.DrawRectangle(Pens.Black, gridviewRect)
    End Sub