Tuesday, September 10, 2013

Changing the Report Viewer Page Size and Lay Out in VB.net

Changing the Report Viewer Page Size and Lay Out in VB.net

1. Open the Report RDL.

2. Right click on the outside the Box Area.

3. Click On report Properties.
4. Change the details you wanted to change like Page Size, Orientation etc.

Sunday, September 8, 2013

Items collection cannot be modified when the DataSource property is set


I received the following error "Items collection cannot be modified when the DataSource property is set" when populating the combo box with data table.

Reason:

The Items.Clear method was called while the combo box's data source was assigned.

Solution:


Need to set the combo box's data source to nothing before calling Items.Clear method

The following sample code explains this scenario clearly.

Private Sub populatedata()
Dim SQLdta As SqlDataAdapter = Nothing

Try


Dim SQl As String = "SELECT AlertID, alertName FROM alert_Master"
SQLdta  = New SqlDataAdapter(SQl, SConnectionString)

Dim dt As New DataTable

SQLdta.Fill(dt)

cmbProducts.DataSource = Nothing // Need to set the data source nothing
// Comment this line to reproduce the error

cmbtype.Items.Clear()

cmbtype.DataSource = dt


cmbtype.DisplayMember = "Name"


cmbProducts.ValueMember = "ID"

Catch ex As Exception


msgbox(ex.Message)
End Try
End Sub

Tuesday, September 3, 2013

Code Conversion Sites.

Multi column Combo box Code


Public Class frmMainForm
    Private WithEvents bsData As New BindingSource()
    Public ReadOnly Property ComboBoxWidths() As String
        Get
            Return "0;80;80;80"
        End Get
    End Property
    Private Sub LoadData()
        Dim dt As New DataTable
        dt.TableName = "MockedData"

        dt.Columns.AddRange(New DataColumn() _
            {New DataColumn("Identifier", GetType(System.Int32)), New DataColumn("FirstName", GetType(System.String)), _
             New DataColumn("LastName", GetType(System.String)), New DataColumn("Account", GetType(System.String))})


        dt.Rows.Add(New Object() {10, "Bill", "Smith", "AB100"})
        dt.Rows.Add(New Object() {20, "John", "Doe", "BB120"})
        dt.Rows.Add(New Object() {30, "Mary", "Brown", "CC105"})
        dt.Rows.Add(New Object() {40, "Kay", "Clime", "AY001"})
        dt.AcceptChanges()


        bsData.DataSource = dt
        lblAccount.DataBindings.Add("Text", bsData, "Account")
    End Sub
    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()

        cboDemo.ColumnWidths = ComboBoxWidths
        cboDemo.DataSource = bsData

    End Sub
    Private Sub bsData_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsData.PositionChanged
        Dim View = bsData.CurrentRow

        lblFirstName.Text = View("FirstName").ToString
        lblLastName.Text = View("LastName").ToString

    End Sub
    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Close()
    End Sub
End Class

Monday, September 2, 2013

Multiple Column Combo box in VB .net.


Multiple Column Combo box in VB .net. 

Steps.
1 Create a form and add a combo box to it.
Simply copy the code to the forms code window.
Your multi combo box is ready.
here is the code.


Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D

Public Class Form1

    Private Sub  Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dtTest As DataTable = New DataTable()
        dtTest.Columns.Add("ID", GetType(Integer))
        dtTest.Columns.Add("Name", GetType(String))

        dtTest.Rows.Add(1, "John")
        dtTest.Rows.Add(2, "Amy")
        dtTest.Rows.Add(3, "Tony")
        dtTest.Rows.Add(4, "Bruce")
        dtTest.Rows.Add(5, "Allen")

        ' Bind the ComboBox to the DataTable
        Me.comboBox1.DataSource = dtTest
        Me.comboBox1.DisplayMember = "Name"
        Me.comboBox1.ValueMember = "ID"

        ' Enable the owner draw on the ComboBox.
        Me.comboBox1.DrawMode = DrawMode.OwnerDrawFixed
        ' Handle the DrawItem event to draw the items.
    End Sub

    Private Sub comboBox1_DrawItem(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.Forms.DrawItemEventArgs) _
                                   Handles comboBox1.DrawItem
        ' Draw the default background
        e.DrawBackground()

        ' The ComboBox is bound to a DataTable,
        ' so the items are DataRowView objects.
        Dim drv As DataRowView = CType(comboBox1.Items(e.Index), DataRowView)

        ' Retrieve the value of each column.
        Dim id As Integer = drv("ID").ToString()
        Dim name As String = drv("Name").ToString()

        ' Get the bounds for the first column
        Dim r1 As Rectangle = e.Bounds
        r1.Width = r1.Width / 2

        ' Draw the text on the first column
        Using sb As SolidBrush = New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(id, e.Font, sb, r1)
        End Using

        ' Draw a line to isolate the columns
        Using p As Pen = New Pen(Color.Black)
            e.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom)
        End Using

        ' Get the bounds for the second column
        Dim r2 As Rectangle = e.Bounds
        r2.X = e.Bounds.Width / 2
        r2.Width = r2.Width / 2

        ' Draw the text on the second column
        Using sb As SolidBrush = New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(name, e.Font, sb, r2)
        End Using
    End Sub
End Class