Click here to Skip to main content
15,890,512 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
RaltonLewis2-Oct-08 3:55
RaltonLewis2-Oct-08 3:55 
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
richardw482-Oct-08 4:12
richardw482-Oct-08 4:12 
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
RaltonLewis2-Oct-08 4:26
RaltonLewis2-Oct-08 4:26 
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
richardw482-Oct-08 4:44
richardw482-Oct-08 4:44 
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
richardw482-Oct-08 4:59
richardw482-Oct-08 4:59 
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
RaltonLewis2-Oct-08 7:11
RaltonLewis2-Oct-08 7:11 
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
richardw482-Oct-08 9:02
richardw482-Oct-08 9:02 
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
RaltonLewis2-Oct-08 10:19
RaltonLewis2-Oct-08 10:19 
I looked at my database again. Corrected some errors. The sub tables are one-to-many relationships with the main table. It does not look as if what i want to achieve will work. I do not want the datagridview to display more than one record per item.

What i am also doing is exporting my datagrid to excel. Here is the code (It exports the main table perfectly). If you could perhaps help me modify my code to ADD the additional columns from the sub table records to the excel spread sheet, that will also solve my problem because it will be used mainly for pivots and printing.

The vb is merely a front-end for capturing and displaying the data and i thought it would be easy enough to just add the extra columns to the datagridview, because my export to excel already works very well.

Thanks very much for your help so far.

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click

'Verfying the datagridview has data or not
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
Exit Sub
End If

'Creating dataset to export
ds = New System.Data.DataSet

'Add table to dataset
ds.Tables.Add()

'Add column to the table
For i As Integer = 0 To DataGridView1.ColumnCount - 1
ds.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
Next
'Add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
dr1 = ds.Tables(0).NewRow
For j As Integer = 0 To DataGridView1.Columns.Count - 1
dr1(j) = DataGridView1.Rows(i).Cells(j).Value
Next
ds.Tables(0).Rows.Add(dr1)
Next

Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()

Dim dt As System.Data.DataTable = ds.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0

For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next

For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next

wSheet.Columns.AutoFit()
Dim strFileName As String = "C:\AAA\MemberList.xls" 'This will become a textbox later
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try

If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If

wBook.SaveAs(strFileName)
excel.Workbooks.Open(strFileName)
excel.Visible = True

End Sub
GeneralRe: How Do I Add A New Column For Each Record From Table To Existing DatagridView Pin
richardw482-Oct-08 22:06
richardw482-Oct-08 22:06 
Questionopen window using vb Pin
jhyn2-Oct-08 2:07
jhyn2-Oct-08 2:07 
AnswerCheck this CodeProject article Pin
David Mujica2-Oct-08 3:04
David Mujica2-Oct-08 3:04 
QuestionRe: open window using vb Pin
richardw482-Oct-08 3:45
richardw482-Oct-08 3:45 
AnswerRe: open window using vb Pin
jhyn2-Oct-08 15:50
jhyn2-Oct-08 15:50 
QuestionRe: open window using vb Pin
richardw482-Oct-08 22:11
richardw482-Oct-08 22:11 
QuestionVS 2008 Express - need to create a setup package Pin
Anoop Brijmohun2-Oct-08 0:22
Anoop Brijmohun2-Oct-08 0:22 
AnswerRe: VS 2008 Express - need to create a setup package Pin
Johan Hakkesteegt2-Oct-08 1:47
Johan Hakkesteegt2-Oct-08 1:47 
AnswerRe: VS 2008 Express - need to create a setup package Pin
jzonthemtn2-Oct-08 6:01
jzonthemtn2-Oct-08 6:01 
GeneralRe: VS 2008 Express - need to create a setup package Pin
Paul Conrad2-Oct-08 9:35
professionalPaul Conrad2-Oct-08 9:35 
GeneralRe: VS 2008 Express - need to create a setup package Pin
Anoop Brijmohun2-Oct-08 20:27
Anoop Brijmohun2-Oct-08 20:27 
QuestionWinForms DatagridView Field Pin
MatthysDT1-Oct-08 23:12
MatthysDT1-Oct-08 23:12 
AnswerRe: WinForms DatagridView Field Pin
George B Gilbert2-Oct-08 6:40
George B Gilbert2-Oct-08 6:40 
GeneralRe: WinForms DatagridView Field Pin
MatthysDT2-Oct-08 20:07
MatthysDT2-Oct-08 20:07 
GeneralRe: WinForms DatagridView Field Pin
George B Gilbert3-Oct-08 9:48
George B Gilbert3-Oct-08 9:48 
GeneralRe: WinForms DatagridView Field Pin
MatthysDT6-Oct-08 2:13
MatthysDT6-Oct-08 2:13 
GeneralRe: WinForms DatagridView Field Pin
George B Gilbert6-Oct-08 5:42
George B Gilbert6-Oct-08 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.