Steveinbeloit

Hi,
I am working with a DataGridView that I would like to display two rows of data for each record in the DataTable.

I have two uses for this, one, the user wants the information in the first row, and the description in the second. Description is just another column on the same record, and some type of space divider between those two rows, and the next two which would correspond to another row in the database.

The other use needs to look like a two line check book register. Two rows of data for each row returned from the database.

All my searches have been returning how to wrap data in a particular cell, but not quite what I am after.

Anyone done anything like this
Thanks


Re: Windows Forms Designer DataGridView multiple display rows per one record

Gavin Jin - MSFT

Hi,here is a sample code use datareader

Imports System.Data

Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim conn As New SqlConnection("Server=(Local);" & _

"DataBase=Northwind; Integrated Security=SSPI")

Dim sqlstr As String = "SELECT FirstName,LastName,BirthDate,HireDate FROM Employees"

Dim command As New SqlCommand(sqlstr, conn)

Dim dt As New DataTable

dt.Columns.Add("c1", GetType(System.String))

dt.Columns.Add("c2", GetType(System.String))

conn.Open()

Dim reader As SqlDataReader = command.ExecuteReader()

While reader.Read()

Dim line, line1 As String

line = reader.GetString(0).ToString()

line1 = reader.GetString(1).ToString()

dt.Rows.Add(line, line1)

End While

conn.Close()

Me.DataGridView1.DataSource = dt

End Sub

End Class

Hope it helps






Re: Windows Forms Designer DataGridView multiple display rows per one record

Steveinbeloit

Gavin,
Thank you for the response.

Is the idea here to load the DataTable with more than one row per each record read from the Database, and then bind the DataGridView normally It will still represent one row per each row in the DataTable, but we have loaded multiple row per record

Thanks





Re: Windows Forms Designer DataGridView multiple display rows per one record

Steveinbeloit

I think this will still only show one row on the datagridview per row in the table won't it

I need something like Access's continuous form.




Re: Windows Forms Designer DataGridView multiple display rows per one record

Gavin Jin - MSFT

Hi,please check this code

Imports System.Data

Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim conn As New SqlConnection("Server=(Local);" & _

"DataBase=Northwind; Integrated Security=SSPI")

Dim sqlstr As String = "SELECT FirstName,LastName,BirthDate,HireDate FROM Employees"

Dim command As New SqlCommand(sqlstr, conn)

Dim dt As New DataTable

dt.Columns.Add("c1", GetType(System.String))

dt.Columns.Add("c2", GetType(System.String))

conn.Open()

Dim reader As SqlDataReader = command.ExecuteReader()

While reader.Read()

Dim line, line1 As String

line = reader.GetString(0).ToString()

line1 = reader.GetString(1).ToString()

dt.Rows.Add(line, line1)

Dim line2, line3 As String

line = reader.Getdate(0).ToString()

line1 = reader.Getdate(1).ToString()

dt.Rows.Add(line2, line3)

End While

conn.Close()

Me.DataGridView1.DataSource = dt

End Sub

End Class

My computer doesnot have VS,so I cannot debug it,maybe the "Gerdate" is not correct,you can change it when you debug.But this method is right.

Hope it helps.






Re: Windows Forms Designer DataGridView multiple display rows per one record

Steveinbeloit

I see. Your point is that I can load multiple lines into the DGV from one line of the database table. That is what I was asking, thanks.

Looks like I would have the same number and names of the columns, but they would contain different data. Maybe some conditional formatting so they don't always show.

Thanks.