create pdf from datagridview without database - vb.net

I have the below code and and I want to create a PDF using iTextSharp.
How do I do that without creating a database (because the code for every row in the datagridview is different)?
Option Strict On
Option Explicit On
Public Class Form1
Dim counter As Integer
Dim Total As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim List = New List(Of Test)
Try
'
' Fill in the data grid with a List
'
'
' code for First Row
'
Dim Bonus = (CDbl(ComboBox2.SelectedItem) * CDbl(TextBox2.Text) * 0.001)
Dim SumAssured = TextBox2.Text
Dim NormalCover = CDbl(TextBox2.Text) + Bonus
Dim DAB = (CDbl(CStr(TextBox2.Text)) * 2)
Dim Premium = TextBox4.Text
Dim Tax = (CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01)
Dim Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Dim AccBenefit = DAB + Bonus
Dim Return_From_LIC = CStr(0)
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
'
' code for Second to second Last Row
'
For Me.counter = 0 To CInt((CDbl(TextBox3.Text) - 2))
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = TextBox4.Text
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01
Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Return_From_LIC = CStr(0)
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
Next
'
' Code of Last column
'
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = CStr(0)
Tax = CDbl(CStr(0))
Net_outgo = CDbl(CStr(0))
Return_From_LIC = CStr(CDbl(TextBox2.Text) + Bonus * CDbl(TextBox3.Text))
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
'
' Code of Summary column
'
TextBox1.Text = "Total"
NormalCover = Nothing
AccBenefit = Nothing
Premium = CStr(CDbl(TextBox4.Text) * CDbl(TextBox3.Text))
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01 * CDbl(TextBox3.Text)
Net_outgo = CDbl(CDbl(CDbl(TextBox4.Text) - (CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01)) * CDbl(TextBox3.Text))
Return_From_LIC = CStr("-")
List.Add(New Test(TextBox1.Text, CStr(NormalCover), CStr(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)))
DataGridView1.DataSource = List
TextBox1.Text = Nothing
TextBox2.Text = Nothing
TextBox3.Text = Nothing
TextBox4.Text = Nothing
ComboBox1.SelectedItem = Nothing
ComboBox2.SelectedItem = Nothing
Catch ex As Exception
MsgBox("Check Your Input Values")
End Try
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'
' Fill in the data grid on form load.
'
Me.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End Sub
End Class
''' <summary>
''' This class contains Seven properties.
''' </summary>
Public Class Test
Public Sub New(ByVal Age As String, ByVal Normal_Risk_Cover As String, _
ByVal Accidental_Risk_Cover As String, ByVal Annual_Premium As String, _
ByVal Tax_Rebate As String, ByVal Net_Outgo As String, ByVal Return_From_LIC As String)
_Age = Age
_Normal_Risk_Cover = Normal_Risk_Cover
_Accidental_Risk_Cover = Accidental_Risk_Cover
_Annual_Premium = Annual_Premium
_Tax_Rebate = Tax_Rebate
_Net_Outgo = Net_Outgo
_Return_From_LIC = Return_From_LIC
End Sub
Private _Age As String
Public Property Age() As String
Get
Return _Age
End Get
Set(ByVal value As String)
_Age = value
End Set
End Property
Private _Normal_Risk_Cover As String
Public Property Normal_Risk_Cover() As String
Get
Return _Normal_Risk_Cover
End Get
Set(ByVal value As String)
_Normal_Risk_Cover = value
End Set
End Property
Private _Accidental_Risk_Cover As String
Public Property Accidental_Risk_Cover() As String
Get
Return _Accidental_Risk_Cover
End Get
Set(ByVal value As String)
_Accidental_Risk_Cover = value
End Set
End Property
Private _Annual_Premium As String
Public Property Annual_Premium() As String
Get
Return _Annual_Premium
End Get
Set(ByVal value As String)
_Annual_Premium = value
End Set
End Property
Private _Tax_Rebate As String
Public Property Tax_Rebate() As String
Get
Return _Tax_Rebate
End Get
Set(ByVal value As String)
_Tax_Rebate = value
End Set
End Property
Private _Net_Outgo As String
Public Property Net_Outgo() As String
Get
Return _Net_Outgo
End Get
Set(ByVal value As String)
_Net_Outgo = value
End Set
End Property
Private _Return_From_LIC As String
Public Property Return_From_LIC() As String
Get
Return _Return_From_LIC
End Get
Set(ByVal value As String)
_Return_From_LIC = value
End Set
End Property
End Class

I could able to sort out my problem I make little changes in my codes and now without creating database I could create pdf file for others to know I am submitting my codes below for others to study and for more improvements if needed
Option Strict On
Option Explicit On
Imports System
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Public Class Form1
Dim dsPeople As New DataSet("People")
Dim dt As New DataTable
Dim counter As Integer
Dim Total As String
Dim TrueTypeFont As Object
Private Sub Button1_Click(sender As System.Object, e As
System.EventArgs) Handles Button1.Click
Try
'
' code for First Row
'
Dim Bonus = (CDbl(ComboBox2.SelectedItem) * CDbl(TextBox2.Text) *
0.001)
Dim SumAssured = TextBox2.Text
Dim NormalCover = CDbl(TextBox2.Text) + Bonus
Dim DAB = (CDbl(CStr(TextBox2.Text)) * 2)
Dim Premium = TextBox4.Text
Dim Tax = (CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) *
0.01)
Dim Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Dim AccBenefit = DAB + Bonus
Dim Return_From_LIC = CStr(0)
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
'
' code for Second to second Last Row
'
For Me.counter = 0 To CInt((CDbl(TextBox3.Text) - 2))
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = TextBox4.Text
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01
Net_outgo = CDbl(CDbl(TextBox4.Text) - Tax)
Return_From_LIC = CStr(0)
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
Next
'
' Code of Last column
'
TextBox1.Text = CStr(CDbl(TextBox1.Text) + 1)
NormalCover = CDbl(CStr(NormalCover + Bonus))
AccBenefit = CDbl(CStr(AccBenefit + Bonus))
Premium = CStr(0)
Tax = CDbl(CStr(0))
Net_outgo = CDbl(CStr(0))
Return_From_LIC = CStr(CDbl(TextBox2.Text) + Bonus * CDbl
(TextBox3.Text))
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
'
' Code of Summary column
'
TextBox1.Text = "Total"
NormalCover = Nothing
AccBenefit = Nothing
Premium = CStr(CDbl(TextBox4.Text) * CDbl(TextBox3.Text))
Tax = CDbl(TextBox4.Text) * CDec(ComboBox1.SelectedItem) * 0.01 *
CDbl(TextBox3.Text)
Net_outgo = CDbl(CDbl(CDbl(TextBox4.Text) - (CDbl(TextBox4.Text)*
CDec(ComboBox1.SelectedItem) * 0.01)) * CDbl(TextBox3.Text))
Return_From_LIC = CStr("-")
dt.Rows.Add(New Object() {TextBox1.Text, CStr(NormalCover), CStr
(AccBenefit), Premium, CStr(Tax), CStr(Net_outgo), CStr(Return_From_LIC)})
DataGridView1.DataSource = dt
TextBox1.Text = Nothing
TextBox2.Text = Nothing
TextBox3.Text = Nothing
TextBox4.Text = Nothing
ComboBox1.SelectedItem = Nothing
ComboBox2.SelectedItem = Nothing
Catch ex As Exception
MsgBox("Check Your Input Values")
End Try
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
Handles MyBase.Load
'
' Fill in the data grid on form load.
'
'GetCustomers()
dt = dsPeople.Tables.Add("dtPeople")
'create table columns
dt.Columns.Add("Age", GetType(String))
dt.Columns.Add("Natural Cover", GetType(String))
dt.Columns.Add("Accidental Cover", GetType(String))
dt.Columns.Add("Premium", GetType(String))
dt.Columns.Add("Tax Rebate", GetType(String))
dt.Columns.Add("Net Outgo", GetType(String))
dt.Columns.Add("Return From LIC", GetType(String))
Me.DataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs)
Handles Button2.Click
Dim bHasErrors As Boolean = False
Dim ErrorMessage As String = ""
Dim FileName As String = System.IO.Path.Combine
(AppDomain.CurrentDomain.BaseDirectory, "Customers.pdf")
Dim Document As Document = New
Document '(iTextSharp.text.PageSize.LETTER, 50, 10, 10, 10)
Try
PdfWriter.GetInstance(Document, New System.IO.FileStream(FileName,
System.IO.FileMode.Create))
Document.Open()
Document.NewPage()
Document.Add(New Paragraph("Hello World", FontFactory.GetFont
("Arial", 20, BaseColor.BLACK)))
Dim ch As New Chunk("Your Policy Presentation ", FontFactory.GetFont
("Arial", 15, BaseColor.BLACK))
Document.Add(ch)
Dim aTable As PdfPTable
aTable = New PdfPTable(dt.Columns.Count)
For Each col As DataColumn In dt.Columns
aTable.AddCell(col.ColumnName)
Next
Document.Add(aTable)
For Each row As DataRow In dt.Rows
aTable = New PdfPTable(dt.Columns.Count)
aTable.AddCell(row.Field(Of String)("Age"))
aTable.AddCell(row.Field(Of String)("Natural Cover"))
aTable.AddCell(row.Field(Of String)("Accidental Cover"))
aTable.AddCell(row.Field(Of String)("Premium"))
aTable.AddCell(row.Field(Of String)("Tax Rebate"))
aTable.AddCell(row.Field(Of String)("Net Outgo"))
aTable.AddCell(row.Field(Of String)("Return From LIC"))
'aTable.AddCell(row.Field(Of String)("Identifier"))
Document.Add(aTable)
Next
Catch de As DocumentException
bHasErrors = True
ErrorMessage = de.Message
Catch ioe As System.IO.IOException
bHasErrors = True
ErrorMessage = ioe.Message
End Try
Document.Close()
If bHasErrors Then
MessageBox.Show("Failed to create document" & Environment.NewLine &
ErrorMessage)
Else
MessageBox.Show("The document" & Environment.NewLine & FileName &
Environment.NewLine & "has been created")
End If
Process.Start(System.IO.Path.Combine
(AppDomain.CurrentDomain.BaseDirectory, "Customers.pdf"))
End Sub
End Class

Related

How to compare two rows from different excel's into one new excel in vb.net?

I am starting to program in vb.net and I am making a program that takes values ​​from excel columns of 2 files and shows results in a generated excel.
so the first excel has this columns: delivery number, contentID, packages, volume. the second excel has this columns:SPS Number, folder number, contentID, packages, volume.
the excel that i have to generato has this columns:SPS number,folder number, delivery number, contentID,packages, volume. The excel that i have to generate with the program uses contentID as the main identificator, and it has ti compare the packages and volume if the ContentID is the same.
so far i have this in a funtions file:
Module Funciones
'VARIABLES REMATE'
Public ENTREGA As New List(Of String)
Public PAQUETE As New List(Of String)
Public CONTENEDOR As New List(Of String)
Public VOLUMEN As New List(Of String)
'VARIABLES PLANILLA'
Public NSPS As New List(Of String)
Public NPLANILLA As New List(Of String)
Public PAQUETE2 As New List(Of String)
Public IDCONTENEDOR As New List(Of String)
Public VOLUMEN2 As New List(Of String)
Public Sub INICIALIZAR_PLANILLA(ByRef HOJAUSUARIOS As OfficeOpenXml.ExcelWorksheet)
Try
HOJAUSUARIOS.Cells("A1").Value = "N° SPS"
HOJAUSUARIOS.Cells("B1").Value = "N° PLANILLA"
HOJAUSUARIOS.Cells("C1").Value = "ENTREGA"
HOJAUSUARIOS.Cells("D1").Value = "CONTENEDOR"
HOJAUSUARIOS.Cells("E1").Value = "PAQUETES"
HOJAUSUARIOS.Cells("F1").Value = "VOLUMEN"
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Function seleccionardirectorio(ByVal filtro As String) As String
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = filtro
saveFileDialog1.Title = "Seleccione Directorio"
saveFileDialog1.ShowDialog()
Return saveFileDialog1.FileName
End Function
Function extraer_valores_remate(ByRef ruta As String) As Boolean
ExcelPackage.LicenseContext = LicenseContext.NonCommercial
Try
Dim stream = System.IO.File.OpenRead(ruta)
Dim package = New OfficeOpenXml.ExcelPackage(stream)
'// Libro
Dim Workbook = package.Workbook
'// Hojas
Dim hojas = Workbook.Worksheets
' Dim aux As Integer = 1
'While (Workbook.Worksheets.Count >= aux)
Dim hojaUsuarios = Workbook.Worksheets(Workbook.Worksheets.Item(0).ToString)
Dim indice As Integer = 2
While (indice < 2000)
'Numero entrega'
If (IsNothing(hojaUsuarios.Cells("A" & indice).Value) = False) Then
ENTREGA.Add(hojaUsuarios.Cells("A" & indice).Value)
End If
'Numero Contenedor'
If (IsNothing(hojaUsuarios.Cells("B" & indice).Value) = False) Then
CONTENEDOR.Add(hojaUsuarios.Cells("B" & indice).Value)
End If
'Paquete'
If (IsNothing(hojaUsuarios.Cells("C" & indice).Value) = False) Then
PAQUETE.Add(hojaUsuarios.Cells("C" & indice).Value)
End If
'Volumen'
If (IsNothing(hojaUsuarios.Cells("D" & indice).Value) = False) Then
VOLUMEN.Add(hojaUsuarios.Cells("D" & indice).Value)
End If
indice += 1
End While
indice += 1
Catch EX As Exception
MsgBox(EX.ToString)
Return False
End Try
Return True
End Function
Function extraer_valores_planilla(ByRef ruta As String) As Boolean
ExcelPackage.LicenseContext = LicenseContext.NonCommercial
Try
Dim stream = System.IO.File.OpenRead(ruta)
Dim package = New OfficeOpenXml.ExcelPackage(stream)
'// Libro
Dim Workbook = package.Workbook
'// Hojas
Dim hojas = Workbook.Worksheets
' While (Workbook.Worksheets.Count >= aux)
Dim hojaUsuarios = Workbook.Worksheets(Workbook.Worksheets.Item(0).ToString)
Dim indice As Integer = 2
While (indice < 5000)
'Numero entrega'
If (IsNothing(hojaUsuarios.Cells("A" & indice).Value) = False) Then
NSPS.Add(hojaUsuarios.Cells("A" & indice).Value)
End If
'Numero Contenedor'
If (IsNothing(hojaUsuarios.Cells("B" & indice).Value) = False) Then
NPLANILLA.Add(hojaUsuarios.Cells("B" & indice).Value)
End If
'Paquete'
If (IsNothing(hojaUsuarios.Cells("C" & indice).Value) = False) Then
IDCONTENEDOR.Add(hojaUsuarios.Cells("C" & indice).Value)
End If
'Volumen'
If (IsNothing(hojaUsuarios.Cells("D" & indice).Value) = False) Then
PAQUETE2.Add(hojaUsuarios.Cells("D" & indice).Value)
End If
If (IsNothing(hojaUsuarios.Cells("E" & indice).Value) = False) Then
VOLUMEN2.Add(hojaUsuarios.Cells("E" & indice).Value)
End If
indice += 1
End While
indice += 1
Catch EX As Exception
MsgBox(EX.ToString)
Return False
End Try
Return True
End Function
Public Sub LIMPIAR_VARIABLES_REMATE()
ENTREGA.Clear()
CONTENEDOR.Clear()
PAQUETE.Clear()
VOLUMEN.Clear()
End Sub
Public Sub LIMPIAR_VARIABLES_PLANILLA()
ENTREGA.Clear()
CONTENEDOR.Clear()
PAQUETE.Clear()
VOLUMEN.Clear()
End Sub
and on the main file i have this
Imports System.IO
Imports System.Text.RegularExpressions Imports OfficeOpenXml Imports OfficeOpenXml.Style
Public Class Form1 Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
OFD.Title = "Selecciona un archivo"
OFD.Filter = "XLSX|*.xlsx"
If OFD.ShowDialog() = DialogResult.OK Then
Dim extension As String = System.IO.Path.GetExtension(OFD.FileName)
Dim nombreOriginal As String = System.IO.Path.GetFullPath(OFD.FileName)
TextBox1.Text = nombreOriginal
extraer_valores_remate(nombreOriginal)
Button4.Enabled = True
Button3.Enabled = True
Else
MsgBox("Campo Requerido", MsgBoxStyle.Exclamation, Title:="Faltan Datos")
TextBox1.Focus()
End If
End Sub
Public nombre_archivo As String = ""
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim OFD As New OpenFileDialog
OFD.Title = "Selecciona un archivo"
OFD.Filter = "XLSX|*.xlsx"
If OFD.ShowDialog() = DialogResult.OK Then
Dim extension As String = System.IO.Path.GetExtension(OFD.FileName)
nombre_archivo2 = System.IO.Path.GetFileName(OFD.FileName)
Dim nombreOriginal As String = System.IO.Path.GetFullPath(OFD.FileName)
TextBox2.Text = nombreOriginal
extraer_valores_planilla(nombreOriginal)
Else
MsgBox("Campo Requerido", MsgBoxStyle.Exclamation, Title:="Faltan Datos")
TextBox2.Focus()
End If
End Sub
Public nombre_archivo2 As String = ""
'********VARIABLES EXCEL DE CARGA**********'
'Public ENTREGA As New List(Of String)
'Public IDCONTENEDOR As New List(Of String)
''Public PAQUETES As New List(Of String)
'Public VOLUMEN As New List(Of String)
'Public NSPS As New List(Of String)
'Public NPLANILLA As New List(Of String)
'Public IDCONTENERDOR2 As New List(Of String)
'' Public PAQUETES2 As New List(Of String)
'Public VOLUMEN2 As New List(Of String)
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
LIMPIAR_VARIABLES_REMATE()
TextBox1.Text = ""
MsgBox("Las variables del remate se han limpiado correctamente", MsgBoxStyle.Information, Title:="LIMPIAR")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
ExcelPackage.LicenseContext = LicenseContext.NonCommercial
Dim path As String = seleccionardirectorio("Excel|.xlsx")
If (String.IsNullOrWhiteSpace(path) = False) Then
Dim excel = New ExcelPackage(New FileInfo(path))
excel.Workbook.Worksheets.Add("Hoja1")
Dim aux As Integer = 1
Dim Workbook = excel.Workbook
Dim hojas = Workbook.Worksheets
Dim hoja1 = Workbook.Worksheets("Hoja1")
'DAMOS NOMBRE A LAS COLUMNAS
INICIALIZAR_PLANILLA(hoja1)
While (aux <= CONTENEDOR.Count)
hoja1.Cells("C" & aux + 1).Value = ENTREGA.Item(aux - 1)
aux += 1
End While
aux = 1
While (aux <= IDCONTENEDOR.Count)
hoja1.Cells("A" & aux + 1).Value = NSPS.Item(aux - 1)
aux += 1
End While
aux = 1
While (aux <= IDCONTENEDOR.Count)
hoja1.Cells("B" & aux + 1).Value = NPLANILLA.Item(aux - 1)
aux += 1
End While
aux = 1
While (aux <= IDCONTENEDOR.Count)
hoja1.Cells("D" & aux + 1).Value = IDCONTENEDOR.Item(aux - 1)
aux += 1
End While
aux = 1
While (aux <= IDCONTENEDOR.Count)
hoja1.Cells("E" & aux + 1).Value = PAQUETE2.Item(aux - 1)
aux += 1
End While
aux = 1
While (aux <= IDCONTENEDOR.Count)
hoja1.Cells("F" & aux + 1).Value = VOLUMEN2.Item(aux - 1)
'Cambiar color de la celda ocupar este codigo'
'hoja1.Cells("A" & aux + 1).Style.Fill.PatternType = ExcelFillStyle.Solid
'hoja1.Cells("A" & aux + 1).Style.Fill.BackgroundColor.SetColor(Color.Red)
aux += 1
End While
aux = 1
excel.Save()
MsgBox("Documento Creado Correctamente", MsgBoxStyle.Information, Title:="Operacion Correcta")
Process.Start(path)
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
LIMPIAR_VARIABLES_PLANILLA()
TextBox2.Text = ""
MsgBox("Las variables de la planilla se han limpiado correctamente", MsgBoxStyle.Information, Title:="LIMPIAR")
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
End Class
so as you can this does not compare the two excel and it just shows me information
Any ideas on how to do this?
Thanks in advance
you need to match the rows by combining 2 loops.
For each itemfromfile1 in file1
for each itemfromfile2 in file2
' Match 2 rows with each other
if itemfromfile1.SomeField = itemfromfile2.SomeField then
' These are the linked rows between the 2 documents
end if
next
next
Simple fill in the pseudo variables with the code that your office implementation uses.

Find And Format String in RichTextBox Vb.NET

I am working with Vb.NET and the requirement was to find the string n RTF control and make it , bold,italic or whatever color,I have done my efforts to find the string but yesterday I found it not working as per my requirement.
At bottom I will write the full code with test data.
I have a simple form and two control on it, One is CombBox to Select Different Test Cases and One RichTextBox for Displaying the Text.
'Class Declarations
Private FormattingApplied As Boolean = False
Private SelectedBold As Boolean = False
Private SearchText As String = String.Empty
Private SelectedItalic As Boolean = False
Private SelectedUnderLine As Boolean = False
' On Form Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbList.Items.Add("Apple Pina Apple")
cmbList.Items.Add("Apple Delicious Pineapple")
cmbList.Items.Add("Apple Milk Shake Apa")
cmbList.Items.Add("Apple Strawberry ")
FormattingApplied = True
SelectedBold = True
cmbList.SelectedIndex = 0
End Sub
Function UppercaseFirstLetter(ByVal val As String) As String
' Test for nothing or empty.
If String.IsNullOrEmpty(val) Then
Return val
End If
Dim array() As Char = val.ToCharArray
array(0) = Char.ToUpper(array(0))
Return New String(array)
End Function
Private Sub ApplyFormatting(ByVal SearchText As String)
Dim TrimmedString As String = String.Empty
Dim ISTrimmed As Boolean = False
If FormattingApplied Then
Dim count As New List(Of Integer)()
If rtfText.Text.Length >= 53 Then
rtfText.Text = rtfText.Text.Substring(0, 50) + "..."
End If
For i As Integer = 0 To rtfText.Text.Length - 1
If rtfText.Text.IndexOf(SearchText, i) <> -1 Then
count.Add(rtfText.Text.IndexOf(SearchText, i))
ElseIf rtfText.Text.IndexOf(UppercaseFirstLetter(SearchText), i) <> -1 Then
count.Add(rtfText.Text.IndexOf(UppercaseFirstLetter(SearchText), i))
End If
Next
Try
For i As Integer = 0 To count.Count - 1
rtfText.[Select](count(i), SearchText.Length)
If SelectedBold Then
rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Bold)
ElseIf SelectedItalic Then
rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Italic)
ElseIf SelectedUnderLine Then
rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Underline)
'ElseIf SelectedBold AndAlso SelectedItalic AndAlso SelectedUnderLine Then
' rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Underline)
End If
count.RemoveAt(i)
Next
'For i As Integer = 0 To count.Count - 1
' rtfText.[Select](count(i), SearchText.Length)
' If SelectedBold Then
' rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Bold)
' ElseIf SelectedItalic Then
' rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Italic)
' ElseIf SelectedUnderLine Then
' rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Underline)
' End If
' count.RemoveAt(i)
'Next
Catch
count.Reverse()
End Try
rtfText.[Select](rtfText.Text.Length, 0)
rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Regular)
End If
End Sub
Private Sub cmbList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbList.SelectedIndexChanged
rtfText.Text = cmbList.SelectedItem.ToString()
rtfText.[Select](0, rtfText.Text.Length)
rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Regular)
If cmbList.SelectedIndex > -1 Then
ApplyFormatting("apple")
End If
End Sub
Here is the output:
Use this method
Private Sub formatString(ByVal SearchText As String)
Dim position As Integer = 0
Dim rtfString As String = LCase(rtfText.Text)
Dim cnt As Integer = 0
Dim isStop As Boolean = False
While Not isStop
Dim i As Integer = rtfString.IndexOf(SearchText, cnt)
If i < 0 Then
isStop = True
Else
rtfText.Select(i, SearchText.Length)
rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Bold)
cnt = i + 1
End If
End While
rtfText.Select(position, 0)
End Sub
in the cmbList_SelectedIndexChanged
If cmbList.SelectedIndex > -1 Then
'ApplyFormatting("apple")
formatString("apple")
End If

How to show DateTime.Now.ToString(Format("yyyy")) incrimental in for loop

![enter image description here][1]In windows Application I want to Show a datagridview in which year column I want to show year in incremental by 1 i.e. 2014, 2015 etc by using for Loop
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Init.
' Set up the Header Color and Font.
With DataGridView1.ColumnHeadersDefaultCellStyle
.Alignment = DataGridViewContentAlignment.MiddleCenter
.BackColor = Color.DarkRed
.ForeColor = Color.Gold
.Font = New Font(.Font.FontFamily, .Font.Size, _
.Font.Style Or FontStyle.Bold, GraphicsUnit.Point)
End With
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
' Fill in some Text.
'
' Code for First Row
'
Dim Bonus = TextBox2.Text * 0.001 * ComboBox2.SelectedItem
Dim SA = TextBox2.Text
Dim NormalCover = TextBox2.Text + Bonus
Dim Dab = 2 * SA
Dim AccBenefit = Dab + Bonus
Dim Premium = TextBox4.Text
Dim TaxRebate = Premium * ComboBox1.SelectedItem * 0.01
Dim NetOutgo = Premium - TaxRebate
Dim YlyReturn = 0
Dim arrStrings As String()
arrStrings = New String() _
{ _
DateTime.Now.ToString(Format("yyyy")), TextBox1.Text.ToString, NormalCover.ToString, _
AccBenefit.ToString, Premium.ToString, CStr(Math.Round(TaxRebate)), CStr(Math.Round(NetOutgo)), YlyReturn.ToString _
}
DataGridView1.Rows.Add(arrStrings)
arrStrings = Nothing
'
' Code for second to second last Row
'
For i As Integer = 0 To TextBox3.Text - 2
TextBox1.Text = TextBox1.Text + 1
NormalCover = NormalCover + Bonus
AccBenefit = AccBenefit + Bonus
arrStrings = New String() _
{ _
DateTime.Now.ToString(Format("yyyy")).ToString, TextBox1.Text.ToString, NormalCover.ToString, _
AccBenefit.ToString, Premium.ToString, CStr(Math.Round(TaxRebate)), CStr(Math.Round(NetOutgo)), YlyReturn.ToString _
}
DataGridView1.Rows.Add(arrStrings)
arrStrings = Nothing
Next i
'
' Code for Last Row
'
TextBox1.Text = TextBox1.Text + 1
NormalCover = NormalCover + Bonus
AccBenefit = AccBenefit + Bonus
YlyReturn = TextBox2.Text + Bonus * TextBox3.Text
arrStrings = New String() _
{ _
DateTime.Now.ToString(Format("yyyy")), TextBox1.Text.ToString, NormalCover.ToString, _
AccBenefit.ToString, Premium.ToString, CStr(Math.Round(TaxRebate)), CStr(Math.Round(NetOutgo)), YlyReturn.ToString _
}
DataGridView1.Rows.Add(arrStrings)
arrStrings = Nothing
'
' Code for Summary Row
'
Premium = Premium * TextBox3.Text
TaxRebate = TaxRebate * TextBox3.Text
NetOutgo = Premium - TaxRebate
arrStrings = New String() _
{ _
"Total".ToString, "-".ToString, "-".ToString, _
"-".ToString, Premium.ToString, CStr(Math.Round(TaxRebate)), CStr(Math.Round(NetOutgo)), "-".ToString _
}
DataGridView1.Rows.Add(arrStrings)
arrStrings = Nothing
Catch ex As Exception
End Try
End Sub
Private Sub TextBox1_Leave(sender As Object, e As System.EventArgs) Handles TextBox1.Leave
If Not IsNumeric(TextBox1.Text) Then TextBox1.Focus()
If Val(TextBox1.Text) < 0 Then
TextBox1.BackColor = Color.Red
Else
TextBox1.BackColor = Color.White
End If
End Sub
End Class
how to show that pl any can explain.
If you want a loop to generate a series of years starting at the current year then you should do something like this:
For i = 0 To 10
Dim year = Date.Now.Year + i
'...
Next
Running that code now would give you a series of Integer values from 2014 to 2024 inclusive, to do with whatever you like.

Listview - select mosti similar item to value

How would one select item in listview (first column) that is most similar to string value from e.g. label or textbox.
Listview is populated with this code :
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListView1.Items.Clear()
ListView1.View = System.Windows.Forms.View.Details
ListView1.Columns.Add("COL1", 100, HorizontalAlignment.Left) 'KONTO
ListView1.Columns.Add("COL2", 140, HorizontalAlignment.Left) 'NAZIV
Dim FilePath As String = "W:\GLAVNI\KOR14\"
Dim DBF_File As String = "MATIKGL"
Dim ColName As String = "KONTO"
'Dim naz As String
Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
" ;Extended Properties=dBASE IV")
con.Open()
Using cmd As New OleDbCommand("SELECT * FROM MATIKGL ORDER BY KONTO, NAZIV", con)
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While (reader.Read())
Me.ListView1.Items.Add(reader("KONTO"))
'ListView1.Items(i).SubItems.Add(rdr.Item("YourColumnName").ToString)
'BELOW SELECTS ALL ITEMS THAT STARTS WITH 2020-
For i = 0 To ListView1.Items.Count - 1
If ListView1.Items(i).ToString.Contains("2020-") Then
Else
ListView1.Items.Remove(ListView1.Items(i))
End If
Next
End While
Else
End If
End Using
End Using
con.Close()
End Using
End Sub
I have one textbox and a button.
Textual input from textbox should be compared with all items in listview and closest should be selected. One more thing : All items are sorted alphabetically
Button code is :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox1.Text)'this one is changed since all items starts with "2020-"& UCASE TEXT
If checkInt <> -1 Then
ListView1.Items(checkInt).Selected = True
ListView1.Focus()
Else
Label1.Text = "Search string not found"
End If
End Sub
UPDATED CODE
Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox3.Text)
If checkInt <> -1 Then
TextBox4.Focus()
Else
Label14.Text = "NEMA"
On Error GoTo ext
Dim li As ListViewItem
ListView1.SelectedItems.Clear()
ListView1.HideSelection = False
li = ListView1.FindItemWithText("2020-" & UCase(TextBox3.Text))
If Not (li Is Nothing) Then
Me.ListView1.Focus()
li.Selected = True
li.EnsureVisible()
ElseIf li Is Nothing Then
li = ListView1.FindItemWithText("2020-" & Strings.Left(TextBox3.Text, 1))
Me.ListView1.Focus()
li.Selected = True
li.EnsureVisible()
Else
End If
Exit Sub
ext:
TextBox3.Text = ""
TextBox3.Focus()
Label14.Text = "String not found"
End If
This one works.
I know it's not the best solution but it's working.
Could fixed this without your help, thank you Phillip Trelford
Define a function to score two strings for closeness then use LINQ to find the lowest score, i.,e.
' Example score function
Function Score(a As String, b As String) As Integer
Dim index = 0
While index < a.Length And index < b.Length
Dim diff = Math.Abs(AscW(a(index)) - AscW(b(index)))
If diff <> 0 Then Return diff
index += 1
End While
Return Math.Abs(a.Length - b.Length)
End Function
Function Closest(searchWord As String, words As String()) As String
Dim ordered =
From w In words
Select Word = w, Score = Score(w, searchWord)
Order By Score
Return ordered.First().Word
End Function
Sub Main()
Dim words = {"Alpha", "Apple", "Ask"}
Dim searchWord = "Ann"
Dim word = Closest(searchWord, words)
Console.WriteLine(word)
End Sub
Update
To select the value in a WinForms ListView, you need to do roughly this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
Dim prefix = "2020-"
' Extract items from Listview
Dim items = New List(Of String)()
For Each item In ListView1.Items
items.Add(item)
Next
Dim words = items.ToArray()
Dim searchWord = TextBox1.Text
Dim resultWord = Closest(searchWord, words)
'this one is changed since all items starts with "2020-"& UCASE TEXT
Dim checkInt As Integer = FindItem(ListView1, prefix & resultWord)
If checkInt <> -1 Then
ListView1.Items(checkInt).Selected = True
ListView1.Focus()
Else
Label1.Text = "Search string not found"
End If
End Sub

dynamically created dropdownlist

I have a problem with DropDownList's SelectedIndexChanged event.
when the value of district DropDownList is changed, it fires its SelectedIndexChanged event but it does not fill the licencee DropDownList.
Also, when the value of dynamically created brandDropDownList is changed, it first fires the SelectedIndexChanged event of district DropDownList and then fills the licencee DropDownList.
I am giving some code here so you can understand what I am saying.
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic.CompilerServices
Partial Class Transaction_Sales_Demand
Inherits System.Web.UI.Page
Dim objClsDemand As New ClsDemand
Dim objClsPurchase As New ClsPurchaseOrderGen
Shared ds As DataSet
Shared dt As DataTable
Public WithEvents Drpdynamic As DropDownList
Public WithEvents Txtdynamic As TextBox
Public unitname() As String
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'If Not IsPostBack Then
'ds = objClsDemand.FillUnitInHeader()
'generateDynamicColumns(ds)
'GrdDemand.DataBind()
'End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
''Dim newddlBrand As DropDownList
'Dim strddlbrandID As String
'Dim ddlNewBrand As DropDownList = form1.FindControl("ddlBrand")
'strddlbrandID = ddlNewBrand.ID
ds = objClsDemand.FillUnitInHeader()
generateDynamicColumns(ds)
If Not (IsPostBack) Then
'objClsDemand.FillBrand(ddlNewBrand)
objClsDemand.FillDistrict(ddlDistrict)
End If
End Sub
Private Sub generateDynamicColumns(ByVal Ds As DataSet)
Dim i As Integer = 0
Dim h As Integer = 0
Dim pnl As Panel = New Panel()
Dim intcount As Integer = 0
' Dim dr As DataRow
'objClsDemand.FillLicencee(ddllicencee)
'objClsDemand.FillBrand(ddlBrand)
'objClsDemand.FillLabel(ddlLabel)
pnl.Controls.Clear()
dt = New DataTable()
If dt.Columns.Count = 0 Then
dt.Columns.Add("LabelCode")
dt.Columns.Add("BrandName")
dt.Columns.Add("LabelName")
dt.Columns.Add("ProofLiter")
End If
For i = 0 To Ds.Tables(0).Rows.Count - 1
dt.Columns.Add(New DataColumn(Ds.Tables(0).Rows(i)(0)))
dt.Columns.Add(New DataColumn(Ds.Tables(0).Rows(i)(1)))
Next
Dim Lbldynamic As Label
Response.Write("</table>")
Response.Write("<tr>")
For intcount = 0 To dt.Columns.Count - 1
Response.Write("<th>")
Response.Write("<b>")
Lbldynamic = New Label()
Lbldynamic.Height = "23"
Lbldynamic.Font.Bold = True
Lbldynamic.Style("Position") = "Absolute"
' txtunit(i).Style("Top") = "210px"
Lbldynamic.Style("Top") = 130
If dt.Columns(intcount).ColumnName = "BrandName" Then
Lbldynamic.Width = "140"
Lbldynamic.Style("Left") = h
h = h + 143
ElseIf dt.Columns(intcount).ColumnName = "LabelName" Then
Lbldynamic.Width = "210"
Lbldynamic.Style("Left") = h
h = h + 213
Else
Lbldynamic.Width = "110"
Lbldynamic.Style("Left") = h
h = h + 113
End If
Lbldynamic.Text = dt.Columns(intcount).ColumnName
'unitname(intcount) = Lbldynamic.Text
' Response.Write(un.GetValue(i))
Response.Write("</b>")
Response.Write("</th>")
pnl.Controls.Add(Lbldynamic)
'form1.Controls.Add(Lbldynamic)
Response.Write("<th>")
Next
Response.Write("</tr>")
h = 0
Response.Write("<tr>")
For intcount = 0 To dt.Columns.Count - 1
If dt.Columns(intcount).ColumnName = "BrandName" Or dt.Columns(intcount).ColumnName = "LabelName" Then
Response.Write("<th>")
Response.Write("<b>")
Drpdynamic = New DropDownList
If dt.Columns(intcount).ColumnName = "BrandName" Then
Drpdynamic.ID = "ddlBrand"
Drpdynamic.AutoPostBack = True
objClsDemand.FillBrand(Drpdynamic)
AddHandler Drpdynamic.SelectedIndexChanged, AddressOf Me.Drpdynamic_SelectedIndexChanged
Drpdynamic.Width = "140"
Drpdynamic.Style("Left") = h
h = h + 143
ElseIf dt.Columns(intcount).ColumnName = "LabelName" Then
Drpdynamic.ID = "ddlLabel"
Drpdynamic.AutoPostBack = True
Drpdynamic.Width = "210"
Drpdynamic.Style("Left") = h
h = h + 213
End If
Drpdynamic.Height = "23"
'Drpdynamic.Width = "110"
Drpdynamic.Font.Bold = True
Drpdynamic.Style("Position") = "Absolute"
' txtunit(i).Style("Top") = "210px"
Drpdynamic.Style("Top") = 150
'Drpdynamic.Style("Left") = h
' Response.Write(un.GetValue(i))
Response.Write("</b>")
Response.Write("</th>")
pnl.Controls.Add(Drpdynamic)
'h = h + 113
Response.Write("<th>")
Else
Response.Write("<th>")
Response.Write("<b>")
Txtdynamic = New TextBox()
If dt.Columns(intcount).ColumnName = "LabelCode" Then
Txtdynamic.ID = "txtLabelCode"
Else
Txtdynamic.ID = "txtdynamic" + intcount.ToString()
End If
AddHandler Txtdynamic.TextChanged, AddressOf Me.TextBox_TextChanged
Txtdynamic.AutoPostBack = True
Txtdynamic.Height = "23"
Txtdynamic.Width = "110"
Txtdynamic.Font.Bold = True
Txtdynamic.Style("Position") = "Absolute"
' txtunit(i).Style("Top") = "210px"
Txtdynamic.Style("Top") = 150
Txtdynamic.Style("Left") = h
' Response.Write(un.GetValue(i))
Response.Write("</b>")
Response.Write("</th>")
pnl.Controls.Add(Txtdynamic)
h = h + 113
Response.Write("<th>")
End If
Next
Response.Write("</tr>")
Response.Write("</table>")
form1.Controls.Add(pnl)
End Sub
Private Sub Drpdynamic_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drpdynamic.SelectedIndexChanged
Try
Dim ddlNewBrand As DropDownList = New DropDownList()
ddlNewBrand = DirectCast(sender, DropDownList)
Dim ddlNewLabel As DropDownList = New DropDownList()
ddlNewLabel = form1.FindControl("ddlLabel")
If Not ddlNewBrand.SelectedValue = "" Then
objClsDemand.BrandId = ddlNewBrand.SelectedValue
End If
objClsDemand.FillLabel(ddlNewLabel)
'ddlLabel.Enabled = True
Catch ex As Exception
ProjectData.SetProjectError(ex)
clsFunctions.ErrorLog(DateAndTime.Today, "Transaction", "Demand Generation", "ddlBrand_SelectedIndexChanged", ex.Message)
ProjectData.ClearProjectError()
End Try
End Sub
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtdynamic.TextChanged
Dim txtNewText As TextBox = New TextBox()
txtNewText = DirectCast(sender, TextBox)
Dim i As Integer = 3
Dim ddlNewBrand As DropDownList = DirectCast(sender, TextBox).FindControl("ddlBrand")
Dim ddlNewLabel As DropDownList = New DropDownList()
ddlNewLabel = form1.FindControl("ddlLabel")
If txtNewText.ID = "txtLabelCode" Then
Try
Dim dr As DataRow
objClsPurchase.LabelCode = txtNewText.Text
If Not txtNewText.Text.Equals("") Then
dr = objClsPurchase.fillDetails()
If Not IsDBNull(dr("Brandname")) Then
ddlNewBrand.SelectedItem.Text = dr("BrandName")
ddlNewBrand.Enabled = False
End If
If Not IsDBNull(dr("LabelName")) Then
If ddlNewLabel.SelectedValue = "" Then
objClsDemand.FillLabel(ddlNewLabel)
ddlNewLabel.SelectedItem.Text = dr("LabelName")
ddlNewLabel.Enabled = False
Else
ddlNewLabel.SelectedItem.Text = dr("LabelName")
ddlNewLabel.Enabled = False
End If
End If
Else
objClsPurchase.FillBrand(ddlNewBrand)
End If
Catch ex As Exception
ProjectData.SetProjectError(ex)
clsFunctions.ErrorLog(DateAndTime.Today, "Transaction", "Purchase Order Generation", "txtLabelcode_TextChanged", ex.Message)
ProjectData.ClearProjectError()
End Try
'Else
' Dim dr1 As DataRow
' For i = 4 To dt.Columns.Count - 1
' If txtNewText.ID = "txtdynamic" + i.ToString() Then
' Dim txtnew As TextBox = New TextBox()
' txtnew = form1.FindControl(dt.Columns(i + 1).ColumnName())
' txtNewText.ID = "txtdynamic" + i.ToString()
' objClsDemand.UnitId = objClsDemand.UnitId_byName(dt.Columns(i).ColumnName)
' objClsDemand.LabelId = Val(ddlNewLabel.SelectedValue)
' dr1 = objClsDemand.FillRate(txtNewText)
' txtnew.Text = dr1("LendingPrice")
' End If
' Next
End If
End Sub
Protected Sub ImageButton3_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton3.Click
'Dim txtNew As TextBox = New TextBox()
'Dim ddlnew As DropDownList = New DropDownList()
'Dim str As String
'For intcount = 0 To dt.Columns.Count - 1
' If dt.Columns(intcount).ColumnName = "BrandName" Then
' 'ddlnew = DirectCast(sender, DropDownList)
' ddlnew = form1.FindControl("ddlBrand")
' MsgBox("this is Brand ddl" + ddlnew.SelectedItem.Text)
' ElseIf dt.Columns(intcount).ColumnName = "LabelName" Then
' 'ddlnew = DirectCast(sender, DropDownList)
' ddlnew = form1.FindControl("ddlLabel")
' MsgBox("this is label ddl" + ddlnew.SelectedItem.Text)
' ElseIf dt.Columns(intcount).ColumnName = "LabelCode" Then
' 'txtNew = DirectCast(sender, TextBox)
' txtNew = form1.FindControl("txtLabelCode")
' MsgBox("this is label code txt")
' Else
' str = "txtdynamic" + intcount.ToString()
' MsgBox("this is" + str + " label code txt" + txtNew.Text)
' End If
'Next
End Sub
Protected Sub ddlDistrict_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDistrict.SelectedIndexChanged
If Not ddlDistrict.SelectedValue = "" Then
objClsDemand.DistrictId = ddlDistrict.SelectedValue
End If
objClsDemand.FillLicencee(ddllicencee)
End Sub
End Class
Have you checked that after you set a new datasource for each dropdown you are making a call to DataBind() on that contol?