Hej Hej Torbjörn, se detta inlägg http://www.pellesoft.se/thread/214245/fyll-en-datagrid-i-vbnet Gjorde så här alltid någon som kan ha till hjälp ?VB.NET
Sitter och leker lite med vb 2010 express och försöker få en sökning från en access databas som ska visas i en DataGridView1 eller liknade MSFlexGrid i vb6.
Någon som har ett enkelt exempel på hur man ska göra, får inte till det har letat på nätet men hittar inget som jag kan förstå!!!!
Vill enbart se hur man ska göra för att söka efter ett värde i en tabell som ska visar i en DataGridView.
Någon här som har ett litet exempel som man kan börja lära sig skiljer sig mycket från VB6 som jag är van vid eller inte ?
Hälsningar
Tobbe Sv: VB.NET
Sv:VB.NET
Public Sub Flexgrid()
Dim conn As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim x As Integer
Dim i As Integer
Dim sDate As String
Try
Me.DataGridView1.Rows.Clear()
Me.DataGridView1.RowCount = 30
Me.DataGridView1.ColumnCount = 15
With DataGridView1.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(DataGridView1.Font, FontStyle.Bold)
End With
' Create an unbound DataGridView by declaring a column count.
' DataGridView1.ColumnCount = 13
DataGridView1.ColumnHeadersVisible = True
' Set the column header style.
Dim columnHeaderStyle As New DataGridViewCellStyle()
' columnHeaderStyle.BackColor = Color.Beige
columnHeaderStyle.Font = New Font("Verdana", 8, FontStyle.Bold)
DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
For Each row In DataGridView1.Rows
If Not row.index / 2 = Int(row.index / 2) Then
row.defaultcellstyle.backcolor = Color.Azure
End If
Next
DataGridView1.SelectionMode = _
DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = False
' Set the column header names.
' Me.DataGridView1.Columns(0).HeaderText = "ID"
Me.DataGridView1.Columns(1).HeaderText = "Regnr"
Me.DataGridView1.Columns(2).HeaderText = "Datum"
Me.DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(3).HeaderText = "Tid"
Me.DataGridView1.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(4).HeaderText = "Tekniker"
Me.DataGridView1.Columns(5).HeaderText = "System"
Me.DataGridView1.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(6).HeaderText = "Delar"
Me.DataGridView1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(7).HeaderText = "Mottagen"
Me.DataGridView1.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(8).HeaderText = "Påbörjad"
Me.DataGridView1.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(9).HeaderText = "Påbörjad tid"
Me.DataGridView1.Columns(10).HeaderText = "Avbryten"
Me.DataGridView1.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(11).HeaderText = "Klar"
Me.DataGridView1.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Me.DataGridView1.Columns(12).HeaderText = "Utlämnad"
Me.DataGridView1.Columns(13).HeaderText = "Avdelning"
Me.DataGridView1.Columns(0).Visible = False
' Me.DataGridView1.Columns(0).Width = 40
Me.DataGridView1.Columns(1).Width = 60
Me.DataGridView1.Columns(2).Width = 80
Me.DataGridView1.Columns(3).Width = 40
Me.DataGridView1.Columns(4).Width = 170
Me.DataGridView1.Columns(5).Width = 70
Me.DataGridView1.Columns(6).Width = 80
Me.DataGridView1.Columns(7).Width = 80
Me.DataGridView1.Columns(8).Width = 80
Me.DataGridView1.Columns(9).Width = 120
Me.DataGridView1.Columns(10).Width = 80
Me.DataGridView1.Columns(11).Width = 40
Me.DataGridView1.Columns(12).Width = 130
Me.DataGridView1.Columns(13).Width = 146
Me.DataGridView1.Columns(14).Visible = False
sDate = MonthCalendar1.SelectionStart.ToShortDateString
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & Application.StartupPath & "\Db\Bokning.mdb"
Dim cmd As New OleDbCommand
cmd.CommandText = "SELECT [ID], [Regnr], [Datum], [BokTime], [Mek], [System], [Fplock], [Start], [Status], [Mott], [Avb], [Klar], [Utl], [Avd] FROM [Tider] where [Datum] = '" & MonthCalendar1.SelectionStart.ToShortDateString & "' and Avd = '" & ToolStripStatusLabel2.Text & "'"
cmd.Connection = conn
conn.Open()
Dim rdr As OleDbDataReader = cmd.ExecuteReader
x = 30
For i = 0 To x
With (rdr.Read)
Me.DataGridView1.Rows(i).Cells(0).Value = rdr("ID").ToString
Me.DataGridView1.Rows(i).Cells(1).Value = rdr("Regnr").ToString
Me.DataGridView1.Rows(i).Cells(2).Value = rdr("Datum").ToString
Me.DataGridView1.Rows(i).Cells(3).Value = rdr("BokTime").ToString
Me.DataGridView1.Rows(i).Cells(4).Value = rdr("Mek").ToString
Me.DataGridView1.Rows(i).Cells(5).Value = rdr("System").ToString
Me.DataGridView1.Rows(i).Cells(6).Value = rdr("Fplock").ToString
If Me.DataGridView1.Rows(i).Cells(6).Value = "X" Then
Me.DataGridView1.Rows(i).Cells(6).Value = "LDC"
With Me.DataGridView1.Rows(i).Cells(6).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
End If
If Me.DataGridView1.Rows(i).Cells(6).Value = "-" Then
Me.DataGridView1.Rows(i).Cells(6).Value = "Inga delar"
With Me.DataGridView1.Rows(i).Cells(6).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
End If
If Me.DataGridView1.Rows(i).Cells(6).Value = "V" Then
Me.DataGridView1.Rows(i).Cells(6).Value = "Ok utfört"
With Me.DataGridView1.Rows(i).Cells(6).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
End If
If Me.DataGridView1.Rows(i).Cells(6).Value = "0" Then
Me.DataGridView1.Rows(i).Cells(6).Value = "Ej utfört"
With Me.DataGridView1.Rows(i).Cells(6).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.LightCoral
End With
End If
If Me.DataGridView1.Rows(i).Cells(6).Value = "Plockat" Then
Me.DataGridView1.Rows(i).Cells(6).Value = "Förplockat"
With Me.DataGridView1.Rows(i).Cells(6).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
End If
If Me.DataGridView1.Rows(i).Cells(6).Value = "E" Then
Me.DataGridView1.Rows(i).Cells(6).Value = "Beställ delar"
End If
If Me.DataGridView1.Rows(i).Cells(6).Value = "B" Then
Me.DataGridView1.Rows(i).Cells(6).Value = "Beställt"
End If
Me.DataGridView1.Rows(i).Cells(8).Value = rdr("Start").ToString
If Me.DataGridView1.Rows(i).Cells(8).Value = "True" Then
Me.DataGridView1.Rows(i).Cells(8).Value = "Ja"
Else
Me.DataGridView1.Rows(i).Cells(8).Value = "Nej"
End If
Me.DataGridView1.Rows(i).Cells(7).Value = rdr("Mott").ToString
If Me.DataGridView1.Rows(i).Cells(7).Value = "True" Then
Me.DataGridView1.Rows(i).Cells(7).Value = "Ja"
With Me.DataGridView1.Rows(i).Cells(7).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
Else
Me.DataGridView1.Rows(i).Cells(7).Value = ""
End If
Me.DataGridView1.Rows(i).Cells(8).Value = rdr("Start").ToString
If Me.DataGridView1.Rows(i).Cells(8).Value = "True" Then
Me.DataGridView1.Rows(i).Cells(8).Value = "Ja"
With Me.DataGridView1.Rows(i).Cells(8).Style
.Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
Me.DataGridView1.Rows(i).Cells(8).ToolTipText = "Arbete är påbörjat"
Else
Me.DataGridView1.Rows(i).Cells(8).Value = ""
End If
Me.DataGridView1.Rows(i).Cells(9).Value = rdr("Status").ToString
If Len(Me.DataGridView1.Rows(i).Cells(9).Value) > 1 Then
With Me.DataGridView1.Rows(i).Cells(9).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
End If
Me.DataGridView1.Rows(i).Cells(10).Value = rdr("Avb").ToString
If Me.DataGridView1.Rows(i).Cells(10).Value = "True" Then
Me.DataGridView1.Rows(i).Cells(10).Value = "Ja"
With Me.DataGridView1.Rows(i).Cells(10).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.Yellow
End With
Else
Me.DataGridView1.Rows(i).Cells(10).Value = ""
End If
Me.DataGridView1.Rows(i).Cells(11).Value = rdr("Klar").ToString
If Me.DataGridView1.Rows(i).Cells(11).Value = "False" Then
Me.DataGridView1.Rows(i).Cells(11).Value = ""
' With Me.DataGridView1.Rows(i).Cells(11).Style
'.Font = New Font("Verdana", 10)
'.'ForeColor = Color.Black
'.BackColor = Color.Moccasin
'End With
Me.DataGridView1.Rows(i).Cells(11).ToolTipText = "Verkstad arbete är inte klar"
Else
Me.DataGridView1.Rows(i).Cells(11).Value = "Ja"
With Me.DataGridView1.Rows(i).Cells(11).Style
.Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
Me.DataGridView1.Rows(i).Cells(11).ToolTipText = "Verkstad arbete är klar"
End If
Me.DataGridView1.Rows(i).Cells(12).Value = rdr("Utl").ToString
If Len(Me.DataGridView1.Rows(i).Cells(12).Value) > 1 Then
With Me.DataGridView1.Rows(i).Cells(12).Style
' .Font = New Font("Verdana", 10)
.ForeColor = Color.Black
.BackColor = Color.MediumSpringGreen
End With
End If
Me.DataGridView1.Rows(i).Cells(13).Value = rdr("Avd").ToString
End With
Next i
rdr.Close()
conn.Close()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
Exit Sub
End Try
End Sub