Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: VB.NET: [VB2008] Rivin lisäys - datagridview

teekoo [19.03.2009 18:01:47]

#

Elikkäs taulukossa on muutama sarake joista yksi combobox, jolla haetaan tietoa toisesta taulukosta. Kuinka saan lisättyä rivin taulukon keskelle tai alkuun, kun bindingsource.addnew() lisää sen aina loppuun.

neau33 [20.03.2009 19:54:43]

#

Heippa teekoo!

Kokeile vääntää oheisen esimerkin pohjalta...

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Partial Class MainForm

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

   Private employeeTable As New DataTable()
   Private bindingSource1 As New BindingSource()

   Sub MainFormLoad(sender As Object, e As EventArgs)

      Dim connectionString As String = "Integrated Security=SSPI;" & _
      "Persist Security Info = False;Initial Catalog=Northwnd;" _
      & "Data Source = localhost"
      Dim connection As New SqlConnection()
      connection.ConnectionString = connectionString

      Dim employeeAdapter As New SqlDataAdapter _
      (New SqlCommand("Select * from Employees", connection))

      connection.Open()

      employeeAdapter.Fill(employeeTable)

      Dim dcol As DataColumn = New DataColumn
      With dcol
         .ColumnName = "OrderCount"
         .DataType = Type.GetType("System.Double")
      End With
      employeeTable.Columns.Add(dcol)

      Dim i As Integer = -1

      For Each row As DataRow In employeeTable.Rows
         i += 1: row("OrderCount") = i
      Next

      bindingSource1.DataSource = employeeTable
      dataGridView1.DataSource =  bindingSource1
      dataGridView1.Columns("OrderCount").Visible = False
      dataGridView1.Sort(dataGridView1.Columns( _
      "OrderCount"), SqlClient.SortOrder.Descending)

   End Sub

   Sub Button1Click(sender As Object, e As EventArgs)

      Dim drow As DataRow = employeeTable.NewRow()
      Dim count As Integer = employeeTable.Rows.Count

      For i As Integer = 0 To employeeTable.Columns.Count -1
         Select Case i
            Case Is < employeeTable.Columns.Count -1
               Select Case i
                  Case 0: 'drow(i) = JokuTaulu.Rows(x)(y)
                  'jne.
               End Select
            Case Is = employeeTable.Columns.Count -1
               drow(i) = count
         End Select
      Next

      employeeTable.Rows.Add(drow)
      dataGridView1.Sort(dataGridView1.Columns( _
      "OrderCount"), SqlClient.SortOrder.Descending)
      dataGridView1.CurrentCell = dataGridView1(0, 0)

   End Sub

End Class

Vastaus

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

Tietoa sivustosta