Adding a header row to a datagrid breaks paging - vb.net

I wonder if anyone ever incurred into this.
I adding a extra row to a datagrid header, in order to provide a common description to a group of columns.
I use the following code, but it creates problems with the pager, I.E. the bottom pager is not in synch with the top one.
Sub dgResults_Item_Created(sender As Object, e As DataGridItemEventArgs)
'http://www.codeproject.com/Articles/16049/Merge-Header-GridView-DataGrid
If (e.Item.ItemType = ListItemType.Header) Then
Dim mTable = DirectCast(dgResults.DataSource, DataView)
If (Not IsNothing(mTable)) Then
Dim colSpan = 4 ' colspan of new header
Dim colCount = mTable.Table.Columns.Count ' add cell at colCount - colSpan
Dim dgitem = New DataGridItem(0, 0, ListItemType.Header)
Dim dgcell1 = New TableCell()
Dim dgcell2 = New TableCell()
dgcell1.ColumnSpan = colCount - colSpan
dgcell2.ColumnSpan = colSpan
dgitem.Cells.Add(dgcell1)
dgitem.Cells.Add(dgcell2)
dgcell2.Text = "Client information"
dgcell2.HorizontalAlign = HorizontalAlign.Center
dgResults.Controls(0).Controls.Add(dgitem)
End If
End If
End Sub 'Item_Created

Probably the best way to go would be using a GridView
http://msdn.microsoft.com/en-us/library/05yye6k9.aspx

Related

autoresizing column header in visual basic datagridview

I've been trying to resize the header column in my program as it cuts off an important row label. AutoSizeColumnsMode and AutoResizeColumns() doesn't work. All my attempts so far have lead to the other columns been resized, however not the header column.Image of form
Dim dgv_flightTemplate As New DataGridView
Dim c As Integer = txb_columns.Text
Dim r As Integer = txb_rows.Text
For colcount As Integer = 0 To c - 1
Dim nc As New DataGridViewTextBoxColumn
nc.Name = "Seating Column"
dgv_flightTemplate.Columns.Add(nc)
Next
dgv_flightTemplate.Rows.Add(r)
dgv_flightTemplate.Rows(0).HeaderCell.Value = "A -"
Me.Controls.Add(dgv_flightTemplate)
dgv_flightTemplate.Location = New Point(400, 400)
dgv_flightTemplate.AllowUserToAddRows = False
dgv_flightTemplate.AllowUserToDeleteRows = False
dgv_flightTemplate.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
dgv_flightTemplate.AutoResizeColumns()
also note this has to be done programmatically because i am creating the datagridview from the size the user gives
Thanks for any replies,
Taine

Checking existing database record items against listbox items

I have a datatable that I am adding records to in my windows forms application. this datatable only has 2 columns and the first column is the primary key and is an integer. The second column contains names and I need to automatically add a list of names from a listbox to the table. Most of these names will already have their own record in the table but there will be different names in the listbox each time. I need to check the existing record items against the listbox items to make sure no duplicates get added, but if there is a name in the listbox that does not exist in the datatable, add a new record for that name.
What I have tried so far is this:
Private m_cn As New SqlConnection()
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_IntRowPosition As Integer = 0
Private Sub btnInsertIntoDatabase_Click(sender As Object, e As EventArgs) Handles btnInsertIntoDatabase.Click
Dim drReadRow As DataRow = m_DataTable.NewRow()
Dim dcReadCol As DataColumn = m_DataTable.Columns.Item(1)
Dim intLoopCounter As Integer = 0
Dim unique As Boolean = True
If m_DataTable.Rows.Count = 0 Then
For m_IntRowPosition = 0 To (lstScannedNames.Items.Count() - 1)
Dim drNewRow As DataRow = m_DataTable.NewRow()
drNewRow("emp_id") = m_IntRowPosition
drNewRow("emp_name") = RTrim(lstScannedNames.Items.Item(RTrim(m_IntRowPosition)))
m_DataTable.Rows.Add(drNewRow)
Next m_IntRowPosition
GoTo SomeWhereElse
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ElseIf m_DataTable.Rows.Count > 0 Then
For m_IntRowPosition = 0 To m_DataTable.Rows.Count
For intLoopCounter = 0 To lstScannedNames.Items.Count - 1
If RTrim(m_DataTable.Rows(m_IntRowPosition).Item(1)) = lstScannedNames.Items.Item(intLoopCounter) Then
unique = False
ElseIf RTrim(m_DataTable.Rows(m_IntRowPosition).Item(1)) <> lstScannedNames.Items.Item(intLoopCounter) Then
unique = True
lstScannedNames.SelectedIndex = intLoopCounter
intLoopCounter = lstScannedNames.Items.Count - 1
End If
Next intLoopCounter
If (unique) = True Then
Dim drNewRow As DataRow = m_DataTable.NewRow()
drNewRow("emp_id") = m_DataTable.Rows.Count()
drNewRow("emp_name") = lstScannedNames.Items.Item(lstScannedNames.SelectedIndex)
m_DataTable.Rows.Add(drNewRow)
Else
unique = True
End If
Next m_IntRowPosition
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End If
SomeWhereElse:
m_DA.Update(m_DataTable)
MessageBox.Show("Operation Completed")
End Sub
The area of focus is the ElseIf statement. When I run this code and there are already records in the database, it adds the first name that is in the list and not in the datatable, to the datatable. It then adds the first name from the list to a new record, for each item in the list, which is about 20 times but it depends on the list length. I've been trying different things and I know I'm close but I've been stuck for a while.
Reverse your logic. Loop over the items in the listbox and check if they are present in the datatable
....
For intLoopCounter = 0 To lstScannedNames.Items.Count - 1
Dim name = lstScannedNames.Items.Item(intLoopCounter)
Dim rowExist = m_DataTable.Select("emp_name = '" & name & "'")
if rowExist Is Nothing OrElse rowExist.Length = 0 Then
Dim drNewRow As DataRow = m_DataTable.NewRow()
drNewRow("emp_id") = m_DataTable.Rows.Count()
drNewRow("emp_name") = name
m_DataTable.Rows.Add(drNewRow)
End If
Next m_IntRowPosition
...
Using the DataTable.Select method avoids to write an explicit loop to find the duplicate. And you can directly add the row when you are certain that there is no duplicate

VB.net update list items from file

I click a button (with this code) to load lines from file "testexam" into a a listbox "lstHere". Testexam will be updated by another program and I want a code to copy new lines of "testexam" to the bottom of "lstHere". Secondly the selected index should come to the first item of the new list. Any help will be greatly appreciated.
Private Sub
Dim MReader As New StreamReader("C:\Users\Sparrow\testexam.txt")
Dim this1 As String = ""
Dim thisline(6000) As String
Dim i As Integer = 0
Do Until MReader.Peek = -1
this1= MReader.ReadLine
thisline(i)= this1
lstHere.Items.Add(thisline(i))
'go to the next line.
i = i + 1
Loop
End Sub
I don't know how you are trying to implement this, but it works with different buttons.
Button1:
'Add the selected item to the top
ListBox1.Items.Insert(0, ListBox1.SelectedIndex)
Button2:
'Delete duplicates
Dim items(ListBox1.Items.Count - 1) As Object
ListBox1.Items.CopyTo(items, 0)
ListBox1.Items.Clear()
ListBox1.Items.AddRange(items.AsEnumerable().Distinct().ToArray())

How to get child datagrid values

How can I save values in child popup DataGridView?
I am having parent datagridview and child DataGridView in vb.net
Consider I have 2 columns and 2 rows.
When I click 1st column, I will get child DataGridView just near to this cell. Child DataGridView also has 2 rows and 2 columns where I can enter values.
When I click 2nd column, I'll get another new child DataGridView near this cell.
Now if I move back to first column, values I entered are lost. How can I save entered values in popup child window?
Here is my code:
sub cell_click Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location Dim _pointGrid As Point = DgV.Location Dim _pointLocation As Point _pointLocation.X = _pointCell.X 'width _pointLocation.Y = _pointCell.Y 'height SelectionInGrid() mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y))) end sub
Public SelectionInGrid() Dim t1,t2 As New DataGridViewTextBoxColumn() Dim gv As New DataGridView
gv.Columns.Add(t1)
gv.Columns.Add(t2)
gv.Columns(0).HeaderText = "Employee"
gv.Columns(1).HeaderText = "Currency"
gv.Width = t1.Width + t2.Width
Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
mControlHost.Padding = Padding.Empty
mControlHost.AutoSize = False
mPopup = New ToolStripDropDown()
mPopup.Padding = Padding.Empty
mPopup.Items.Add(mControlHost)
End
sub cell_click
Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
Dim _pointGrid As Point = DgV.Location
Dim _pointLocation As Point
_pointLocation.X = _pointCell.X 'width
_pointLocation.Y = _pointCell.Y 'height
SelectionInGrid()
mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y)))
end sub
Public SelectionInGrid()
Dim t1,t2 As New DataGridViewTextBoxColumn()
Dim gv As New DataGridView
gv.Columns.Add(t1)
gv.Columns.Add(t2)
gv.Columns(0).HeaderText = "Employee"
gv.Columns(1).HeaderText = "Currency"
gv.Width = t1.Width + t2.Width
Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
mControlHost.Padding = Padding.Empty
mControlHost.AutoSize = False
mPopup = New ToolStripDropDown()
mPopup.Padding = Padding.Empty
mPopup.Items.Add(mControlHost)
End
The problem lies in the SelectionInGrid Sub which is called every single time you click a cell. In this function you have these lines:
Dim gv As New DataGridView
Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
mPopup = New ToolStripDropDown()
This means that everytime cell_click runs a new DataGridView, a new ToolStripControlHost and a new ToolStripDropDown are created. To solve this you need to keep track of the different ToolStripDropDowns. For example using a dictionary:
Private PopUps As New Dictionary(Of String, ToolStripDropDown)
sub cell_click
Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
Dim _pointGrid As Point = DgV.Location
Dim _pointLocation As Point
_pointLocation.X = _pointCell.X 'width
_pointLocation.Y = _pointCell.Y 'height
If PopUps.ContainsKey(<Parent Selected cell identifyer>) Then
mPopup = PopUps(<Parent Selected cell identifyer>)
Else
SelectionInGrid()
PopUps.Add(<Parent Selected cell identifyer>,mPopup)
End If
mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y)))
end sub
I do believe this should work. "Parent Selected cell identifyer" must be something unique from the Parent DGV row/cell that was clicked.

Exporting displayed columns in dataGridView to Excel

I may be blind, but this is a little different than the normal export-to-excel. I've created a solution and would like to know if this is the best way. Or even if there is another way to do it.
Background: WinForms, VisualBasic, VS2012, N-Tier (backend is DB2). My DTOs are set up in order of the DB2 tables. My users want to see the fields in a certain order within the DGV AND be able to export the fields in the same order. Users can also rearrange and hide columns. Hidden columns should not be exported.
My solution does do the normal copy from dgv field by field to excel. The difference is that I had to use DataGridViewColumnCollection so that I could utilize DataGridViewElementStates.Visible in order to make sure I'm only exporting visible columns.
Here is the code.
Private Sub ExportToExcelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportToExcelToolStripMenuItem.Click
If((dgv.Columns.Count = 0) Or (dgv.Rows.Count = 0)) Then Exit Sub
Dim XlApp = New Excel.Application With {.Visible = True}
xlApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet)
Dim xlWS = xlApp.ActiveSheet
xlWS.Name = "Exported Data"
'Copy visible data from DGV to Excel
Dim columnCollection As DataGridViewColumnCollection = dgv.Columns
Dim currentVisibleColumn AS DataGridViewColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
Dim lastColumnExported As DataGridViewColumn = currentVisibleColumn
Dim visibleColumntCount As Integer = columnCollection.GetColumnCount(DataGridViewElementStates.Visible)
'Finally export the data
For c = 1 to VisibleColumnCount
xlWS.Cells(1,c) = currentVisibleColumn.HeaderText
currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
lastColumnExported = currentVisibleColumn
Next
'Only export visible cells
For r = 0 To dgv.Rows.Count - 1
'Reset values
currentVisibleColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
lastColumnExported = currentVisibleColumn
For c = 1 to visibleColumnCount
Dim value = dgv.Rows(r).Cells(currentVisibleColumn.Index).Value
If value <> vbNullString Then
xlWS.Cells(r + 2, c) = value.ToString()
End If
currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
lastColumnExported = currentVisibleColumn
Next
Next
'Autosize columns in excel
Dim columns = xlWS.UsedRange.Columns
columns.AutoFit()
End Sub
Thank you for your feedback.
Brian.
Theres is more simple way to do that and it is to perform the if statmente at each iteration of the cells copy process. You ask if that column at that index is visible, if it is visible you copy else you skeep that column and take next.