Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Making a dataGridView with more "columns"
 

Making a dataGridView with more "columns"

I have done a program in C# with visual studio 2005.

I have this datagridview that gets some stuff from a table and shows it...
http://img296.imageshack.us/img296/9381/capture004ge9.jpg

But I want to have 2 columns that means that the data is like:

http://img169.imageshack.us/img169/9452/capture004copyna5.jpg

and that it should happen automatically so that more data can be shown on one screen.

And all the standard datagridview sorting stuff should be working too...

Razer1911  Thursday, June 28, 2007 9:24 AM

Hi

I have a solution, although there is some different with your requirement . Maybe you can try sqldatareader,reload the data to a new datatable as you like,some thing like this

System.Data

System.Data.SqlClient

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 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))

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

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

Dim bf As New Boolean

bf = True

Dim j As Integer = 0

conn.Open()

Dim reader As SqlDataReader = command.ExecuteReader()

While reader.Read()

If bf = False Then

Dim line, line1 As String

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

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

dt.Rows.Add()

dt.Rows(j).Item(0) = line

dt.Rows(j).Item(1) = line1

bf = True

j = j + 1

Else

Dim line, line1 As String

dt.Rows.Add()

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

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

dt.Rows(j).Item(2) = line

dt.Rows(j).Item(3) = line1

bf = False

End If

End While

conn.Close()

Me.DataGridView1.DataSource = dt

End Sub

Class

Hope it helps

Gavin Jin - MSFT  Monday, July 02, 2007 5:13 AM
HEEELP!
Razer1911  Friday, June 29, 2007 6:29 AM

Hi

I have a solution, although there is some different with your requirement . Maybe you can try sqldatareader,reload the data to a new datatable as you like,some thing like this

System.Data

System.Data.SqlClient

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 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))

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

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

Dim bf As New Boolean

bf = True

Dim j As Integer = 0

conn.Open()

Dim reader As SqlDataReader = command.ExecuteReader()

While reader.Read()

If bf = False Then

Dim line, line1 As String

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

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

dt.Rows.Add()

dt.Rows(j).Item(0) = line

dt.Rows(j).Item(1) = line1

bf = True

j = j + 1

Else

Dim line, line1 As String

dt.Rows.Add()

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

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

dt.Rows(j).Item(2) = line

dt.Rows(j).Item(3) = line1

bf = False

End If

End While

conn.Close()

Me.DataGridView1.DataSource = dt

End Sub

Class

Hope it helps

Gavin Jin - MSFT  Monday, July 02, 2007 5:13 AM

Hi

I have a solution, although there is some different with your requirement . Maybe you can try sqldatareader,reload the data to a new datatable as you like,some thing like this

System.Data

System.Data.SqlClient

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 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))

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

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

Dim bf As New Boolean

bf = True

Dim j As Integer = 0

conn.Open()

Dim reader As SqlDataReader = command.ExecuteReader()

While reader.Read()

If bf = False Then

Dim line, line1 As String

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

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

dt.Rows.Add()

dt.Rows(j).Item(0) = line

dt.Rows(j).Item(1) = line1

bf = True

j = j + 1

Else

Dim line, line1 As String

dt.Rows.Add()

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

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

dt.Rows(j).Item(2) = line

dt.Rows(j).Item(3) = line1

bf = False

End If

End While

conn.Close()

Me.DataGridView1.DataSource = dt

End Sub

Class

Hope it helps

Gavin Jin - MSFT  Monday, July 02, 2007 5:13 AM

You can use google to search for other answers

Custom Search

More Threads

• Resizing a control problem in design mode
• UserControl and properties problem
• Disappearing user controls in VS2003 forms designer
• License a control
• set client area of control that inherits from panel
• application doesnt recognise controls event handler
• ListBox - iterating through selected items
• Problem in MaskedtextBox..
• Image size in TreeNode
• Selecting custom control in VS2005 designer.