Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: VB.NET: vb2010 - DataGridView ongelma

jokke568 [19.01.2013 22:07:52]

#

Minulla on Formilla DataGridView johon ohjaan DataBindinNavigaattorilla.

DataGridVien alla on textboxeja, joihin olisi tarkoitus eritellä DataGridViewistä valitun rivin tiedot.

Miten pitäisi edetä? En keksi nyt miten saan ne linkitettyä.

Olisiko jollain ideoita?

Grez [19.01.2013 22:35:32]

#

Bindaat ne textboxit siihen bindingsourceen mihin se bindingnavigator on bindattu.

neau33 [20.01.2013 09:11:56]

#

Moi jokke568!

tässä eräs tapa...

 'väännetty SharpDevelop 4.2:lla
Imports System
Imports System.Data
Imports System.Data.OleDb

Public Partial Class MainForm


	Private da As OleDbDataAdapter = Nothing
	Private ds As DataSet = Nothing
	Private dt As DataTable = Nothing
	Private conn As OleDbConnection = Nothing
	Private connstr As String = String.Empty
	Private dgvFilled As Boolean

	Public Sub New()
		Me.InitializeComponent()
	End Sub

	Sub MainFormLoad(sender As Object, e As EventArgs)

		conn = New OleDbConnection

		Dim dbPath As String = Environment.GetFolderPath( _
		Environment.SpecialFolder.MyDocuments) 'esim.
		dbPath += "\Tietokanata1.accdb"
		connstr = "Provider=Microsoft.ACE.OLEDB.12.0;" + _
		"Data Source="+ dbPath + ";Persist Security Info=False;"

		Try
			conn.ConnectionString = connstr
			conn.Open
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try

		If conn.State = ConnectionState.Open Then

			Dim query As String = "SELECT * FROM Taulu1"
			da = New OleDbDataAdapter(query, connstr)
			ds = New DataSet
			da.Fill(ds, "Taulu1")
			dt = ds.Tables("Taulu1")
			da = Nothing : ds = Nothing

			If dt.Rows.Count > 0 Then

				dataGridView1.DataSource = dt

				For i As Integer = 1 To dt.Columns.Count

					Dim txtfield As TextBox = New TextBox

					With txtfield

						Me.Controls.Add(txtfield)
						.Name = "txtfield" + CStr(i)
						.Width = dataGridView1.Columns(i - 1).Width
						.Top = dataGridView1.Top + dataGridView1.Height + 10 'esim.
						.Font = dataGridView1.Font

						Select Case i
							Case 1
								.Left = dataGridView1.Left + dataGridView1.RowHeadersWidth
							Case Is > 1
								.Left = Me.Controls("txtfield" + CStr(i - 1)).Left _
								+ Me.Controls("txtfield" + CStr(i - 1)).Width - 1
						End Select

					End With

				Next

			End If

		End If


	End Sub

	Sub MainFormShown(sender As Object, e As EventArgs)
		dgvFilled = True
	End Sub

	Sub DataGridView1CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)


		If dgvFilled Then

			e.CellStyle.WrapMode = DataGridViewTriState.True '(*)

			For i As Integer = 0 To DataGridView1.Columns.Count - 1

				Dim txtbox As TextBox = CType(Me.Controls("txtfield" & CStr(i + 1)),TextBox)

				With txtbox

					.Text = DataGridView1.CurrentRow.Cells.Item(i).Value.ToString

					If CType(e.CellStyle.WrapMode, Boolean) Then
						.Multiline = True
						.Height = DataGridView1.CurrentRow.Height
					Else
						.Multiline = False
					End If

				End With

				txtbox = Nothing

			Next

		End If

	End Sub

	Sub DataGridView1ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs)

		Me.Controls("txtfield" + CStr(e.Column.Index + 1)).Width = e.Column.Width

		For i As Integer = 1 To DataGridView1.Columns.Count

			With Me.Controls("txtfield" + CStr(i))

				Select Case i
					Case 1
						.Left = dataGridView1.Left + dataGridView1.RowHeadersWidth
					Case Is > 1
						.Left = Me.Controls("txtfield" + CStr(i - 1)).Left _
						+ Me.Controls("txtfield" + CStr(i - 1)).Width - 1
				End Select

			End With

		Next

	End Sub

	Sub MainFormFormClosing(sender As Object, e As FormClosingEventArgs)

		If conn.State = ConnectionState.Open Then
			conn.Close
		End If

		dt = Nothing
		conn = Nothing

	End Sub

End Class

jokke568 [23.01.2013 22:55:13]

#

Kiitos

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta