Miten saa kopioitua DataGridView-ohjausobjektin taulukosta (Visual Basic 2010 Express) tiedot 2-ulotteiseen taulukkoon esim. taulukko (rivi, sarake) ja miten uudet lasketut tiedot saadaan takaisin DataGridView-ohjausobjektiin. Anttij
Moi anttij!
saat kopioitua datan taulukkoon ja palautettua taulukosta seuraavasti.
Private Structure colStruct Dim colname As String Dim coltext As String End Structure Private colData() As colStruct Private cellData(0, 0) As Object Private rows As Integer Private cols As Integer Private Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click If DataGridView1.Rows.Count = 0 Then Exit Sub End If cols = DataGridView1.Columns.Count rows = DataGridView1.Rows.Count ReDim cellData(rows, cols) Redim colData(cols) For col As Integer = 0 To cols colData(col).coltext = _ DataGridView1.Columns().HeaderText colData(col).colname = _ DataGridView1.Columns().Name For row As Integer = 0 To Rows cellData(rows, cols) = _ DataGridView1.Item(row, col).Value Next row Next col End Sub Private Sub Button2_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click If cellData.GetUpperbound(0) = 0 Then Exit Sub End If dataGridView1.DataSource = Nothing dataGridView1.Columns.Clear dataGridView1.Rows.Clear For col As Integer = 0 To cols DataGridView1.Columns.Add( _ colData(col).colname, _ colData(col).coltext) Next cols For row As Integer = 0 To Rows DataGridView1.Rows.Add() Next row For col As Integer = 0 To cols For row As Integer = 0 To Rows DataGridView1.Item(row, col).Value _ = cellData(rows, cols) Next row Next col Erase colData ReDim cellData(0, 0) End Sub
EDIT:
'... colData(col).coltext = _ DataGridView1.Columns(col).HeaderText 'tuli pari colData(col).colname = _ DataGridView1.Columns(col).Name 'pikku mokaa '...
Suuret Kiitokset! Kokeillaan saanko toimimaan!
Moi taas anttij!
meni hätäpäissä(ni) ****ksi toi edellinen...
Imports System.Data Imports System.Data.SqlClient Public Partial Class MainForm Private Structure colStruct Dim colname As String Dim coltext As String Dim coltype As String End Structure Private colData() As colStruct Private cellData(,) As Object Private rows As Integer Private cols As Integer Private ds As New DataSet Public Sub New() Me.InitializeComponent() End Sub Sub MainForm_Load(sender As Object, e As EventArgs) Dim connstr As String = _ "Data Source=.\SQLEXPRESS;Initial Catalog=taulu" _ & ";User ID=tunnuksesi;Password=salasanasi;" Dim conn As New SqlConnection conn.ConnectionString = connstr conn.Open Dim query As String = "Select * From [taulu]" Dim da As New SqlDataAdapter(query, conn) da.Fill(ds,"product") dataGridView1.DataSource = ds.Tables("taulu") conn.Close: da = Nothing: conn = Nothing End Sub Sub Button1_Click(sender As Object, e As EventArgs) If DataGridView1.Rows.Count = 0 Then Exit Sub End If cols = DataGridView1.Columns.Count - 1 rows = DataGridView1.Rows.Count - 1 ReDim cellData(cols, rows) Redim colData(cols) For col As Integer = 0 To cols colData(col).coltext = _ DataGridView1.Columns(col).HeaderText colData(col).colname = _ DataGridView1.Columns(col).Name For row As Integer = 0 To rows If row = 0 Then colData(col).coltype = _ dataGridView1.Item(col, _ row).Value.GetType.ToString End If cellData(col, row) = _ dataGridView1.Item(col, row).Value Next row Next col End Sub Sub Button2_Click(sender As Object, e As EventArgs) If cellData.GetUpperbound(0) = 0 Then Exit Sub End If dataGridView1.DataSource = Nothing dataGridView1.Columns.Clear dataGridView1.Rows.Clear For col As Integer = 0 To cols DataGridView1.Columns.Add( _ colData(col).colname, _ colData(col).coltext) Next col For row As Integer = 0 To rows DataGridView1.Rows.Add() Next row For col As Integer = 0 To cols For row As Integer = 0 To rows If row = 0 Then dataGridView1.Item(col, row).ValueType = _ Type.GetType(colData(col).coltype) End If DataGridView1.Item(col, row).Value _ = cellData(col, row) Next row Next col Erase colData ReDim cellData(0, 0) End Sub End Class
Kiitos! Nea. Edellisestä sain jo niin paljon vinkkejä että nyt kaikki toimii. Oli hyvä ettei se ollut aivan OK, että itse pääsin juonesta kiinni.
Tv. Anttij
Aihe on jo aika vanha, joten et voi enää vastata siihen.