Datatable calendar on VisualBasic - asp.net

I have a request to show a grid similar to a calendar showing on columns the day and on row months.
Something like this
Month |Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sun|Mon|Tue|Wed|...|Thu|
January | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11|...................| 31|
Febrary | | | | 1| 2| 3| 4| 5| 6| 7|...........................| 29|
...
December| 30| 31| | | | | 1| 2| 3| 4|............................
Is there a way to do this, considering that The Start day of the year must be the real day name, not a constant and this must be generated dynamically
I try this but my idea goes off!
Private Sub GenerateCalendar(ByVal year As Integer)
Dim colsCount As Integer = 3
If Bisiesto(año) Then
colsCount = colsCount + 366
Else
colsCount = colsCount + 365
End If
Dim dtCalendar As New DataTable
dtCalendar.Columns.Add("Mes")
Dim dayMonthYear As Date = New Date(year , 1, 1)
'Showing the distribution of the first month
While dayMonthYear.Year = año And dayMonthYear.Month = 1
Dim dtColumn As DataColumn = New DataColumn()
dtColumn.ColumnName = WeekdayName(Weekday(dayMonthYear)) & dayMonthYear.Day & dayMonthYear.Month
dtColumn.Caption = WeekdayName(Weekday(dayMonthYear))
dtCalendar.Columns.Add(dtColumn)
dayMonthYear = diaMesAño.AddDays(1)
End While
Dim row As DataRow = dtCalendario.NewRow()
'Here I need to distribute the days on its column day
dtCalendario.Rows.Add(row)
wdgCalendario.DataSource = dtCalendar
wdgCalendario.DataBind()
End Sub
Public Function LeapYear(ByVal year As Integer)
Dim isLeapYear As Boolean = False
If year Mod 4 = 0 Then
If (year Mod 100 = 0) And Not (year Mod 400 = 0) Then
isLeapYear = False
Else
isLeapYear = True
End If
Else
isLeapYear = False
End If
Return isLeapYear
End Function

Note: You cannot have duplicate column names in the DataTable, so i have appended the weekday with the number of week in month. But see it for yourself:
Private Function GetCalendarTable(year As Int32) As DataTable
Dim curCulture = System.Globalization.CultureInfo.CurrentCulture
Dim firstYearDate = New Date(year, 1, 1)
Dim currentDate = firstYearDate
Dim tblCalendar = New DataTable
tblCalendar.Columns.Add(New DataColumn("MonthName"))
Dim maxDiff = 0
For m = 1 To 12
'find max difference between first year's weekday and month's first weekday
'if the latter is earlier in the week, it is considered to be in the next week
Dim monthFirstWeekDay = New Date(year, m, 1).DayOfWeek
Dim diff = (7 + (monthFirstWeekDay - firstYearDate.DayOfWeek)) Mod 7
If diff > maxDiff Then
maxDiff = diff
End If
Next
Dim weekDayNum = curCulture.Calendar.GetDaysInMonth(year, 1) + maxDiff
' Create DataColumns with weekday as ColumnsName
For wd = 1 To weekDayNum
Dim weekday = currentDate.ToString("ddd")
Dim weekInMonth = (From col In tblCalendar.Columns.Cast(Of DataColumn)()
Where col.ColumnName Like String.Format("{0} W#", weekday)).Count + 1
Dim columnName = String.Format("{0} W{1}", weekday, weekInMonth)
tblCalendar.Columns.Add(New DataColumn(columnName))
currentDate = currentDate.AddDays(1)
Next
' Create the DataRows(every month)
For m = 1 To 12
Dim daysInMonth = curCulture.Calendar.GetDaysInMonth(year, m)
Dim firstMonthDate = New Date(year, m, 1)
Dim daysBefore = (7 + (firstMonthDate.DayOfWeek - firstYearDate.DayOfWeek)) Mod 7
Dim daysBehind = tblCalendar.Columns.Count - (daysBefore + daysInMonth) - 1
Dim monthDays = From d In Enumerable.Range(1, daysInMonth)
Select New With {.Day = d.ToString}
Dim emptyDaysBefore = From d In Enumerable.Range(1, daysBefore)
Select New With {.Day = ""}
Dim emptyDaysAfter = From d In Enumerable.Range(1, daysBehind)
Select New With {.Day = ""}
Dim monthName = curCulture.DateTimeFormat.GetMonthName(m)
' piece together parts
Dim allFields = ({New With {.Day = monthName}}.
Union(emptyDaysBefore).
Union(monthDays).
Union(emptyDaysAfter).
Select(Function(d) d.Day)).ToArray
tblCalendar.Rows.Add(allFields)
Next
Return tblCalendar
End Function

Related

Find matching dates between GridViews A & B and auto-tick checkbox in GridView A if theres is match - VB.Net

I have a Gridview that generates the total dates of selected month and displays them in table format, along with a checkbox column.
Said checkbox, when ticked will upload the date, plus additional data from other fields into SQL database. Un-ticking it will delete the data row from the database.
As it is, the GridView cannot remember which row's checkbox was ticked in the last session if I decide to change month or start a new session.
I don't think I can use SQL query to help with that since the GridView content is generated via code and does not use the database. So I made another GridView, this one collects the relevant dates from SQL that have matching information from the additional fields when they were uploaded to database.
My idea was to have the 1st GridView check the data from 2nd GridView and automatically tick the correct checkboxes when it loads.
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowBtn.Click
Call load_Calander()
End Sub
Sub load_Calander()
'~ code to generate date column in GridView1 ~'
Dim year As Integer = Convert.ToInt32(CurrentYear.Text)
Dim month As Integer = Convert.ToInt32(Mth_Num.Text)
If month = 1 Then
Mth_Txt.Text = "Jan"
ElseIf month = 2 Then
Mth_Txt.Text = "Feb"
ElseIf month = 3 Then
Mth_Txt.Text = "Mar"
ElseIf month = 4 Then
Mth_Txt.Text = "Apr"
ElseIf month = 5 Then
Mth_Txt.Text = "May"
ElseIf month = 6 Then
Mth_Txt.Text = "Jun"
ElseIf month = 7 Then
Mth_Txt.Text = "Jul"
ElseIf month = 8 Then
Mth_Txt.Text = "Aug"
ElseIf month = 9 Then
Mth_Txt.Text = "Sep"
ElseIf month = 10 Then
Mth_Txt.Text = "Oct"
ElseIf month = 11 Then
Mth_Txt.Text = "Nov"
ElseIf month = 12 Then
Mth_Txt.Text = "Dec"
End If
Dim myList As List(Of [myClass]) = New List(Of [myClass])()
If month = 1 OrElse month = 3 OrElse month = 5 OrElse month = 7 OrElse month = 8 OrElse month = 10 OrElse month = 12 Then
For i As Integer = 1 To 31
Dim item As [myClass] = New [myClass]()
item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
item.[Day] = dateValue.ToString("ddddddddd")
myList.Add(item)
Next
ElseIf month = 4 OrElse month = 6 OrElse month = 9 OrElse month = 11 Then
For i As Integer = 1 To 30
Dim item As [myClass] = New [myClass]()
item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
item.[Day] = dateValue.ToString("ddddddddd")
myList.Add(item)
Next
ElseIf DateTime.IsLeapYear(year) Then
For i As Integer = 1 To 29
Dim item As [myClass] = New [myClass]()
item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
item.[Day] = dateValue.ToString("ddddddddd")
myList.Add(item)
Next
Else
For i As Integer = 1 To 28
Dim item As [myClass] = New [myClass]()
item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
item.[Day] = dateValue.ToString("ddddddddd")
myList.Add(item)
Next
End If
GridView1.DataSource = myList
GridView1.DataBind()
'code that generates GridView2 (SQL data)
Dim cmd As New SqlCommand
Dim DateTB As New DataSet
Dim DateCmd As String = "select ScheduleDate FROM [SQLIOT].[dbo].[ZEPB_PreventiveDateSchedule] " _
& " where line_desc = '" & LineList.SelectedItem.Text & "' and process = '" & ProcessList.SelectedItem.Text & "' " _
& " and machine = '" & MachineList.SelectedItem.Text & "' and Step = '" & StepList.SelectedItem.Text & "' " _
& " and inspection = '" & InspectionList.SelectedItem.Text & "' and specification = '" & SpecificationList.SelectedItem.Text & "' "
Dim da As New SqlDataAdapter(DateCmd, Conn)
cmd.Connection = Conn
Conn.Open()
da.Fill(DateTB, "TempStore")
Dim dvgroup As DataView = DateTB.Tables("TempStore").DefaultView
GridView2.DataSource = dvgroup
GridView2.DataBind()
Conn.Close()
'code to cross-reference and auto tick checkbox - Currently empty...
Currently I'm a little stuck on how to code the cross-reference part.
Also, a little explanation on how the program works.:
When 1st load there are a bunch of drop down lists where user picks their data, along with the month.
With a click of a button, the calendar GridView will load with full dates based on the month selector.
User will click on any of the GridView's row checkbox to upload the information from drop down list, along with the selected date from GridView to SQL database.
Hopefully this gives some more context to my question.

How to append a row from datatable1 to a datatable2 with registers ASP.NET?

I need to append a row from databla1 to datatable2 that has already rows, I tried to importrow(), clone(), but I cant' find the solution:
The datatable1 has this structure:
name | surname | iddepartment | address | phone1 | phone2 | country | cp | birthdate |
And the datatable2 is a public datatable created inside a class:
Public Shared datable2 As New DataTable
I have this code:
Dim datatable1 As DataTable = getdata("myQuery")
I tried to do this but I get an error:
Dim newRow= datatable1.Rows(0)
myClass.datatable2.rows.add(newRow)
How can I do this? thanks
As far as I understand.
VB.NET Sample :
'db query result'
table.Columns.Add("Name", GetType(String))
table.Columns.Add("surname", GetType(String))
table.Columns.Add("iddepartment", GetType(Integer))
table.Columns.Add("phone1", GetType(String))
table.Columns.Add("phone2", GetType(String))
table.Columns.Add("country", GetType(String))
table.Columns.Add("cp", GetType(String))
table.Columns.Add("birthdate", GetType(DateTime))
'column set'
Dim table2 As DataTable = table
Dim i As Integer = 0
'add dummy data'
For index = 1 To 10
'instance new dataRow '
Dim dr As DataRow = table2.NewRow
dr("Name") = "Jack"
dr("surname") = "Daniels"
dr("iddepartment") = i
dr("phone1") = "099999"
dr("phone2") = "09999"
dr("country") = "USA"
dr("cp") = "-"
dr("birthdate") = New DateTime
table2.Rows.Add(dr)
i = i + 1
Next
'each table2 rows'
For Each rowItem As DataRow In table2.Rows
Console.WriteLine(rowItem("Name"))
Console.WriteLine(rowItem("surname"))
Console.WriteLine(rowItem("iddepartment"))
Console.WriteLine(rowItem("phone1"))
Console.WriteLine(rowItem("phone2"))
Console.WriteLine(rowItem("country"))
Console.WriteLine(rowItem("cp"))
Console.WriteLine(rowItem("birthdate"))
Next
Console.Read()
Datatable Example Source Link : https://www.dotnetperls.com/datatable-vbnet

Generate TreeView Control Dynamically from Query (VB.NET)

I have never used TreeViews in the past and I want to display a hierarchical structure (recursive relationship of n levels). The data (available in a dataset - retrieved from database query) is in the following structure:
__ID__ | __NAME__ | __PARENT__
1 | Patrick |
2 | Mark |
3 | Scott | 2
4 | Jason |
5 | Julian |
6 | John | 6
7 | Steve |
8 | George | 1
9 | Robert | 1
10 | Rodney | 8
I'm trying to produce the following output
- Patrick [1]
- George [8]
- Rodney [10]
- Robert [9]
- Mark [2]
- Scott [3]
- Julian [5]
- John [6]
- Jason [4]
- Steve [7]
I'm trying to generate the Treeview control but have no experience with Treeviews. Any feedback or examples on how to achieve this would be really appreciated.
To fill the TreeView from a DataTable, try the following code
Dim DataTable1 As New DataTable
Private Sub FillTestTable()
DataTable1.Columns.Add("ID", GetType(Integer))
DataTable1.Columns.Add("NAME", GetType(String))
DataTable1.Columns.Add("PARENT", GetType(Integer))
DataTable1.Columns.Add("LEVEL", GetType(Integer))
DataTable1.Rows.Add(1, "Patrick")
DataTable1.Rows.Add(2, "Mark")
DataTable1.Rows.Add(3, "Scott", 2)
DataTable1.Rows.Add(4, "Jason")
DataTable1.Rows.Add(5, "Julian")
DataTable1.Rows.Add(6, "John", 5)
DataTable1.Rows.Add(7, "Steve")
DataTable1.Rows.Add(8, "George", 1)
DataTable1.Rows.Add(9, "Robert", 1)
DataTable1.Rows.Add(10, "Rodney", 8)
Dim i As Integer
For i = 0 To DataTable1.Rows.Count - 1
Dim ID1 As String = DataTable1.Rows(i).Item("ID").ToString
DataTable1.Rows(i).Item("LEVEL") = FindLevel(ID1, 0)
Next
End Sub
Private Function FindLevel(ByVal ID As String, ByRef Level As Integer) As Integer
Dim i As Integer
For i = 0 To DataTable1.Rows.Count - 1
Dim ID1 As String = DataTable1.Rows(i).Item("ID").ToString
Dim Parent1 As String = DataTable1.Rows(i).Item("PARENT").ToString
If ID = ID1 Then
If Parent1 = "" Then
Return Level
Else
Level += 1
FindLevel(Parent1, Level)
End If
End If
Next
Return Level
End Function
Code for VB.NET WindowsForms application
Private Sub CreateTree()
Dim MaxLevel1 As Integer = CInt(DataTable1.Compute("MAX(LEVEL)", ""))
Dim i, j As Integer
For i = 0 To MaxLevel1
Dim Rows1() As DataRow = DataTable1.Select("LEVEL = " & i)
For j = 0 To Rows1.Count - 1
Dim ID1 As String = Rows1(j).Item("ID").ToString
Dim Name1 As String = Rows1(j).Item("NAME").ToString
Dim Parent1 As String = Rows1(j).Item("PARENT").ToString
If Parent1 = "" Then
TreeView1.Nodes.Add(ID1, Name1)
Else
Dim TreeNodes1() As TreeNode = TreeView1.Nodes.Find(Parent1, True)
If TreeNodes1.Length > 0 Then
TreeNodes1(0).Nodes.Add(ID1, Name1)
End If
End If
Next
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
FillTestTable()
CreateTree()
TreeView1.ExpandAll()
End Sub
Code for ASP.NET application
Private Sub CreateTree()
Dim MaxLevel1 As Integer = CInt(DataTable1.Compute("MAX(LEVEL)", ""))
Dim i, j As Integer
For i = 0 To MaxLevel1
Dim Rows1() As DataRow = DataTable1.Select("LEVEL = " & i)
For j = 0 To Rows1.Count - 1
Dim ID1 As String = Rows1(j).Item("ID").ToString
Dim Name1 As String = Rows1(j).Item("NAME").ToString
Dim Parent1 As String = Rows1(j).Item("PARENT").ToString
If Parent1 = "" Then
TreeView1.Nodes.Add(New TreeNode(Name1, ID1))
Else
Dim Node1 As TreeNode = GetChildByValue(Parent1, TreeView1.Nodes)
If Not Node1 Is Nothing Then
Node1.ChildNodes.Add(New TreeNode(Name1, ID1))
End If
End If
Next
Next
End Sub
Private Function GetChildByValue(ByVal ID1 As String, ByVal NodeCollection1 As TreeNodeCollection) As TreeNode
For Each TreeNode1 As TreeNode In NodeCollection1
If TreeNode1.Value = ID1 Then
Return TreeNode1
Else
Dim TreeNode2 As TreeNode = GetChildByValue(ID1, TreeNode1.ChildNodes)
If Not TreeNode2 Is Nothing Then
Return TreeNode2
End If
End If
Next
Return Nothing
End Function
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FillTestTable()
CreateTree()
TreeView1.ExpandAll()
End Sub
The big deal here is coming with a generic and adaptable enough algorithm capable of performing the required sorting. Once this is in place, writing the values into the TreeView is straightforward, just adding Nodes and ChildNodes.
Dim NAME(10) As String
Dim PARENT(10) As Integer
Dim curID As Integer = 0
curID = 1
NAME(curID) = "Patrick [" & curID.ToString() & "]"
PARENT(curID) = 0
curID = curID + 1
NAME(curID) = "Mark [" & curID.ToString() & "]"
PARENT(curID) = 0
curID = curID + 1
NAME(curID) = "Scott [" & curID.ToString() & "]"
PARENT(curID) = 2
curID = curID + 1
NAME(curID) = "Jason [" & curID.ToString() & "]"
PARENT(curID) = 0
curID = curID + 1
NAME(curID) = "Julian [" & curID.ToString() & "]"
PARENT(curID) = 0
curID = curID + 1
NAME(curID) = "John [" & curID.ToString() & "]"
PARENT(curID) = 6
curID = curID + 1
NAME(curID) = "Steve [" & curID.ToString() & "]"
PARENT(curID) = 0
curID = curID + 1
NAME(curID) = "George [" & curID.ToString() & "]"
PARENT(curID) = 1
curID = curID + 1
NAME(curID) = "Robert [" & curID.ToString() & "]"
PARENT(curID) = 1
curID = curID + 1
NAME(curID) = "Rodney [" & curID.ToString() & "]"
PARENT(curID) = 8
Dim completed As Boolean = False
Dim firstIteration As Boolean = True
Dim totIDs As Integer = 10
Do
curID = 0
Do
curID = curID + 1
If (firstIteration) Then
If (PARENT(curID) = 0 And TreeView1.FindNode(NAME(curID)) Is Nothing) Then
TreeView1.Nodes.Add(New TreeNode(NAME(curID)))
End If
Else
If (PARENT(curID) > 0) Then
Dim targetNodes As TreeNodeCollection = TreeView1.Nodes
Dim count As Integer = 0
If (TreeView1.FindNode(NAME(curID)) Is Nothing) Then
For Each node As TreeNode In targetNodes
count = count + 1
If (node.Text.Contains("[" & PARENT(curID).ToString() & "]")) Then
node.ChildNodes.Add(New TreeNode(NAME(curID)))
Exit For
End If
Next
End If
End If
End If
Loop While (curID < totIDs)
If (firstIteration) Then
firstIteration = False
Else
Exit Do 'Just two iterations
End If
Loop While (Not completed)
This code relies on two arrays (NAME (strings), which also includes [original position] and PARENT (integers)) and performs the inclusions until the "second level", that is, main nodes and first child nodes.
I guess that you will have enough information to understand how to deal with TreeView and to build an algorithm capable of performing the sorting you want.

Specific Date and Time in ASP Classic

Hi i have the following Code :
Dim CurrentDate
CurrentDate = Date()
Dim intHour
Dim intMinute
Dim intSecond
intHour = 17
intMinute = 0
intSecond = 0
Dim NewDate
Dim NewDate1
Dim NewDate2
NewDate = DatePart("yyyy", CurrentDate)
NewDate1 = DatePart("m", CurrentDate)
NewDate2 = DatePart("d", CurrentDate)
Dim Dates
Dates = DateSerial(NewDate, NewDate1, NewDate2)
Dim Time
Time = TimeSerial(intHour, intMonth, intSecond)
I have done something equal in VB:
Dim value As Date = Date.Now
Dim intHour As Integer
Dim intMinute As Integer
Dim intSecond As Integer
intHour = 17
intMinute = 0
intSecond = 0
Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond)
In VB i can do
Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond).
In my ASP Code i have Dates = DateSerial(NewDate, NewDate1, NewDate2) and Time = TimeSerial(intHour, intMonth, intSecond). How can i put them together as DateTime like in VB?
Two ways:
dim h,n,s
h = 17
n = 1
s = 2
dim t
t = timeserial(h,n,s)
dim d
d = date()
dim ts1
ts1 = dateadd("h",h, _
dateadd("n",n, _
dateadd("s",s, d )))
dim ts2
ts2 = d + T
Both produce the same output. The one with the additions has some gotchas depending on how close to day 0 you play it. I think the first way is "saner". As far as I recall VBScript just stored the date part in the integer or a float and the time part in the fraction part of a float (as parts of a 24 hour day, so 12:00 is 0.5), hence you can just add them together with +

how to merge rows in datatable which had same value (VB)

I have one datatable tempDT with value :
Serial_No testong
---------------------------------------
DTSHCSN001205035919201 [ OUT ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201 [ IN ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201 [ OUT ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012
DTSHCSN001205035919201 [ IN ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012
The problem is I want merge duplicate serial_no into one row which the value of testong adding to new column.
I have tried many ways, but I can't find the solution.
Here is my code behind :
Dim tempDt = GetItemDataTable()
Dim dtData As New DataTable
dtData.Columns.Add("Serial_No")
Dim i As Integer = 0
Dim row As DataRow
For Each row In tempDt.Rows
i += 1
Dim dr As DataRow = dtData.NewRow
dr("Serial_No") = row(0)
If dr("Serial_No") = row(0) Then
Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
colBaru.Caption = i
colBaru.FieldName = i
colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.Width = 150
colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex = grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
colBaru.PropertiesTextEdit.EncodeHtml = True
grid.Columns.Add(colBaru)
dtData.Columns.Add(i)
dr(i) = row(3)
dtData.Rows.Add(dr)
Else
dr("Serial_No") = row(0)
dtData.Rows.Add(dr)
End If
When I debug the result is :
But I wanted the result is like this :
I have updated my code, so it would look at the columns rather using variable i
Dim tempDt = GetItemDataTable()
Dim dtData As New DataTable
'initial the first and second columns
dtData.Columns.Add("Serial_No")
dtData.Columns.Add("1")
Dim i As Integer = 0
Dim row As DataRow
For Each row In tempDt.Rows
Dim dr As DataRow
i += 1
'check if the serial no exists in the new Data Table
If dtData.Select("Serial_No='" & row(0) & "'").Length > 0 Then
'If found, then get the existing row
dr = dtData.Select("Serial_No='" & row(0) & "'")(0)
'Create new GridViewDataTextColumn
Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
colBaru.Caption = i
colBaru.FieldName = i
colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex = grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
colBaru.PropertiesTextEdit.EncodeHtml = False
grid.Columns.Add(colBaru)
'I assume you are adding the Number as the columns name
'Only need to create if the Column doesn't exist
If dtData.Columns.count - 1 < i Then
dtData.Columns.Add(i.ToString)
End If
'Use variable i here
dr(i) = row(3)
'Comment this out as you don't need to
'dtData.Rows.Add(dr)
Else
'reset value of i
i = 1
'If not found, then create a new row
dr = dtData.NewRow
' i put this to add the serial_no value to datatable
dr("Serial_No") = row(0)
'for adding first value i with same row as serial_no
Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
colBaru.Caption = i
colBaru.FieldName = i
colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex = grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
colBaru.PropertiesTextEdit.EncodeHtml = False
grid.Columns.Add(colBaru)
'dtData.Columns.Add("1")
'Would be better to use back variable i if you have reset it
dr(i) = row(3)
dtData.Rows.Add(dr)
End If
Next

Resources