Can u share to me the source code sir? I've been trying for a month, but failed.
@oakbank19532 жыл бұрын
Imports System.Data Imports System.Data.SqlClient Public Class frmUSAStates Private Sub frmUSAStates_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' www.sqlusa.com/bestpractices/dynamicsql/ ScreenWidth = Screen.PrimaryScreen.Bounds.Width ScreenHeight = Screen.PrimaryScreen.Bounds.Height Me.CenterToScreen() Dim adapter As New SqlDataAdapter Dim ds As New DataSet Dim CountOfRows As Integer = 0 sSQL = "select " sSQL = sSQL & "StateName, Abbreviation, Capital, LargestCity, Established, Population, " sSQL = sSQL & "Area_sqmiles, Area_sqkm, LandArea_sqmiles, LandArea_sqkm, WaterArea_sqmiles, WaterArea_sqkm, Representatives " sSQL = sSQL + "From tblUSAStates " sSQL = sSQL + "order by Abbreviation" OpenDB(sSQL) cmd.CommandText = sSQL cmd.ExecuteNonQuery() cmd = New SqlCommand(sSQL, cnn) adapter.SelectCommand = cmd adapter.Fill(ds) dgy = -1 dgx = -1 dgvTreeView.Rows.Clear() dgvTreeView.RowCount = 9999 For Each row As DataRow In ds.Tables(0).Rows If CountOfRows = 0 Then dgvTreeView.ColumnCount = row.Table.Columns.Count + 1 'dgvTreeView.Rows(dgx).Cells(0).Value = "+" For xx = 0 To row.Table.Columns.Count - 1 dgvTreeView.Columns(xx + 1).HeaderText = row.Table.Columns(xx).Caption Next If row.Table.Columns.Count * 100 + 30 > ScreenWidth Then dgvTreeView.Width = Me.Width - 30 Else dgvTreeView.Width = row.Table.Columns.Count * 100 + 30 ' 25 is for the column zero (+/-) End If dgvTreeView.Height = ScreenHeight - 100 'MsgBox(row.Table.Columns(0).Caption) ' statename 'MsgBox(row.ItemArray(0)) ' alaska - same as below... 'MsgBox(row.Field(Of String)(columnIndex:=0)) ' this works!!! End If dgx = dgx + 1 ' heading line - always have the element 0 as the first one. 'dgvTreeView.RowCount = dgvTreeView.RowCount + 1 'dgvTreeView.Columns(0).CellType.DataGridViewButtonColumn dgvTreeView.Rows(dgx).Cells(0).Value = "+" dgvTreeView.Columns(0).Width = 20 dgvTreeView.Rows(dgx).Cells(1).Value = row.ItemArray(0) ' row.Field(Of String)("StateName") ' detail line... dgx = dgx + 1 dgvTreeView.Rows(dgx).Cells(0).Value = "-" For xx = 1 To row.Table.Columns.Count - 1 dgvTreeView.Rows(dgx).Cells(xx + 1).Value = row.ItemArray(xx).ToString Next dgvTreeView.Rows.Item(dgx).Visible = False CountOfRows = CountOfRows + 1 Next adapter.Dispose() cmd.Dispose() CloseDB() End Sub Private Sub dgvTreeView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvTreeView.CellClick iDGVRow = dgvTreeView.CurrentCell.RowIndex iDGVCol = dgvTreeView.CurrentCell.ColumnIndex ' this will only work if you have one parent row and one child row - if there are other combinations - ' perhaps 5 child rows? then you will need to change things... If iDGVCol = 0 Then If dgvTreeView.Rows(iDGVRow).Cells(0).Value = "-" Then dgvTreeView.Rows.Item(iDGVRow).Visible = False Else dgvTreeView.Rows.Item(iDGVRow + 1).Visible = True End If End If End Sub 'Private Sub SetupColumnHeadings() ' 'dgvTreeView.ColumnCount = Row.Table.Columns.Count + 1 ' 'dgvTreeView.Rows(dgx).Cells(0).Value = "+" ' 'For xx = 0 To Row.Table.Columns.Count - 1 ' ' dgvTreeView.Columns(xx + 1).HeaderText = Row.Table.Columns(xx).Caption ' 'Next ' 'If Row.Table.Columns.Count * 100 + 30 > ScreenWidth Then ' ' dgvTreeView.Width = Me.Width - 30 ' 'Else ' ' dgvTreeView.Width = Row.Table.Columns.Count * 100 + 30 ' 25 is for the column zero (+/-) ' 'End If ' 'dgvTreeView.Height = ScreenHeight - 100 ' 'MsgBox(row.Table.Columns(0).Caption) ' statename ' 'MsgBox(row.ItemArray(0)) ' alaska - same as below... ' 'MsgBox(row.Field(Of String)(columnIndex:=0)) ' this works!!! 'End Sub End Class
@jazzassin21502 жыл бұрын
Hey that‘s great. Can you send me the Source Code?
@oakbank19532 жыл бұрын
Imports System.Data Imports System.Data.SqlClient Public Class frmUSAStates Private Sub frmUSAStates_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' www.sqlusa.com/bestpractices/dynamicsql/ ScreenWidth = Screen.PrimaryScreen.Bounds.Width ScreenHeight = Screen.PrimaryScreen.Bounds.Height Me.CenterToScreen() Dim adapter As New SqlDataAdapter Dim ds As New DataSet Dim CountOfRows As Integer = 0 sSQL = "select " sSQL = sSQL & "StateName, Abbreviation, Capital, LargestCity, Established, Population, " sSQL = sSQL & "Area_sqmiles, Area_sqkm, LandArea_sqmiles, LandArea_sqkm, WaterArea_sqmiles, WaterArea_sqkm, Representatives " sSQL = sSQL + "From tblUSAStates " sSQL = sSQL + "order by Abbreviation" OpenDB(sSQL) cmd.CommandText = sSQL cmd.ExecuteNonQuery() cmd = New SqlCommand(sSQL, cnn) adapter.SelectCommand = cmd adapter.Fill(ds) dgy = -1 dgx = -1 dgvTreeView.Rows.Clear() dgvTreeView.RowCount = 9999 For Each row As DataRow In ds.Tables(0).Rows If CountOfRows = 0 Then dgvTreeView.ColumnCount = row.Table.Columns.Count + 1 'dgvTreeView.Rows(dgx).Cells(0).Value = "+" For xx = 0 To row.Table.Columns.Count - 1 dgvTreeView.Columns(xx + 1).HeaderText = row.Table.Columns(xx).Caption Next If row.Table.Columns.Count * 100 + 30 > ScreenWidth Then dgvTreeView.Width = Me.Width - 30 Else dgvTreeView.Width = row.Table.Columns.Count * 100 + 30 ' 25 is for the column zero (+/-) End If dgvTreeView.Height = ScreenHeight - 100 'MsgBox(row.Table.Columns(0).Caption) ' statename 'MsgBox(row.ItemArray(0)) ' alaska - same as below... 'MsgBox(row.Field(Of String)(columnIndex:=0)) ' this works!!! End If dgx = dgx + 1 ' heading line - always have the element 0 as the first one. 'dgvTreeView.RowCount = dgvTreeView.RowCount + 1 'dgvTreeView.Columns(0).CellType.DataGridViewButtonColumn dgvTreeView.Rows(dgx).Cells(0).Value = "+" dgvTreeView.Columns(0).Width = 20 dgvTreeView.Rows(dgx).Cells(1).Value = row.ItemArray(0) ' row.Field(Of String)("StateName") ' detail line... dgx = dgx + 1 dgvTreeView.Rows(dgx).Cells(0).Value = "-" For xx = 1 To row.Table.Columns.Count - 1 dgvTreeView.Rows(dgx).Cells(xx + 1).Value = row.ItemArray(xx).ToString Next dgvTreeView.Rows.Item(dgx).Visible = False CountOfRows = CountOfRows + 1 Next adapter.Dispose() cmd.Dispose() CloseDB() End Sub Private Sub dgvTreeView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvTreeView.CellClick iDGVRow = dgvTreeView.CurrentCell.RowIndex iDGVCol = dgvTreeView.CurrentCell.ColumnIndex ' this will only work if you have one parent row and one child row - if there are other combinations - ' perhaps 5 child rows? then you will need to change things... If iDGVCol = 0 Then If dgvTreeView.Rows(iDGVRow).Cells(0).Value = "-" Then dgvTreeView.Rows.Item(iDGVRow).Visible = False Else dgvTreeView.Rows.Item(iDGVRow + 1).Visible = True End If End If End Sub 'Private Sub SetupColumnHeadings() ' 'dgvTreeView.ColumnCount = Row.Table.Columns.Count + 1 ' 'dgvTreeView.Rows(dgx).Cells(0).Value = "+" ' 'For xx = 0 To Row.Table.Columns.Count - 1 ' ' dgvTreeView.Columns(xx + 1).HeaderText = Row.Table.Columns(xx).Caption ' 'Next ' 'If Row.Table.Columns.Count * 100 + 30 > ScreenWidth Then ' ' dgvTreeView.Width = Me.Width - 30 ' 'Else ' ' dgvTreeView.Width = Row.Table.Columns.Count * 100 + 30 ' 25 is for the column zero (+/-) ' 'End If ' 'dgvTreeView.Height = ScreenHeight - 100 ' 'MsgBox(row.Table.Columns(0).Caption) ' statename ' 'MsgBox(row.ItemArray(0)) ' alaska - same as below... ' 'MsgBox(row.Field(Of String)(columnIndex:=0)) ' this works!!! 'End Sub End Class