На форме расположен элемент управления DataGridView
Dim request As Net.FtpWebRequest = Net.WebRequest.Create("ftp://server.ru")
request.Credentials = New Net.NetworkCredential("login", "password")
request.Method = Net.WebRequestMethods.Ftp.ListDirectoryDetails
Dim response As Net.FtpWebResponse = request.GetResponse()
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(responseStream)
Dim Line As String
Do
Line = reader.ReadLine
If Len(Line) < 3 Then Exit Do
Dim Params() As String = Line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
DataGridView1.Rows.Add(Params(4) & " b.", Params(6) & " " & Params(5) & " " & Params(7), Params(8))
Loop
Контекстное меню для DataGridView
Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
If ((e.RowIndex <> -1) AndAlso (e.ColumnIndex <> -1)) Then
If (e.Button = MouseButtons.Right) Then
Dim clickedCell As DataGridViewCell = CType(sender, DataGridView).Rows(e.RowIndex).Cells(e.ColumnIndex)
Me.DataGridView1.CurrentCell = clickedCell
DataGridView1.CurrentRow.Selected = True
Dim relativeMousePosition = DataGridView1.PointToClient(Cursor.Position)
Me.ContextMenuStrip1.Show(DataGridView1, relativeMousePosition)
End If
End If
End Sub
Добавить ComboBox-столбец в DataGridView
Dim combCol As New DataGridViewComboBoxColumn
With combCol
.HeaderText = "Combo Column"
.Items.AddRange(New String() {"Дерево", "Дорога", "Травмпункт"})
End With
DataGridView1.Columns.Add(combCol)
Пример с чтением и сохранением данных из DataGridView в XML
Dim DT As New DataTable
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DT.TableName = "Table1"
DT.Columns.Add("Col1")
DT.Rows.Add()
DataGridView1.DataSource = DT
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DT.WriteXml("1.xml")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
DT.TableName = "Table1"
DT.Columns.Add("Col1")
DT.Columns.Add("Col2")
DT.ReadXml("1.xml")
DataGridView1.DataSource = DT
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
DT.Columns.Add("Col2")
End Sub
Автоподбор высоты строки в DataGridView
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
DataGridView1.Rows(0).Cells(0).Style.WrapMode = DataGridViewTriState.True
Обновление таблиц в БД (Сохранить изменения из DataGridView)
Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\KBK.mdb")
Dim tbl As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim cb As OleDb.OleDbCommandBuilder
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
da.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM КЦСР", cn)
tbl.Clear()
da.Fill(tbl)
DataGridView1.DataSource = tbl
cb = New OleDb.OleDbCommandBuilder(da)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
da.UpdateCommand = cb.GetUpdateCommand()
da.Update(tbl)
End Sub
Сохранение и чтение всей DataGridView, используя XML-файлы через DataTable и DataSet
Dim dt As New DataTable ' - создание объекта таблица данных
Dim ds As New DataSet ' - создание объекта набор данных
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyBase.Text = "Почти табличный редактор"
Button1.Text = "Запись"
If IO.File.Exists("C:\tabl.xml") = False Then
' Если XML-файла НЕТ:
DataGridView1.DataSource = dt
' Заполнение шапки таблицы:
dt.Columns.Add("Имена")
dt.Columns.Add("Номера телефонов")
' Добавить объект dt в DataSet:
ds.Tables.Add(dt)
Else ' Если XML-файл ЕCТЬ:
ds.ReadXml("C:\tabl.xml")
DataGridView1.DataMember = "Название таблицы"
DataGridView1.DataSource = ds
End If
End Sub
Private Sub ЗАПИСЬ(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dt.TableName = "Название таблицы"
ds.WriteXml("C:\tabl.xml")
End Sub