iterating through a dataset and apply values from 5 of 7 columns to textboxs in an active report - vb.net

I want to iterate through a dataSet and apply each value to a textbox in an active report. i dont know if these text boxes need to be a the Group/Header area or what. i know that my code below is only retrieving the first row. how can I iterate through all rows and apply the data to text boxes that active reports manages to get multiple rows in the group section
Private Sub rptUserCellPhoneSwap_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
Me.PageSettings.Orientation = GrapeCity.ActiveReports.Document.Section.PageOrientation.Landscape
DateTxt.Text = Now.ToShortDateString & " " & Now.ToShortTimeString
Dim DataSet = GrabInformation(FirstName, LastName)
UserTxt.Text = LastName + ", " + FirstName
'For Each dr As DataRow In DataSet.Tables(0).Rows
' OldIMEITxt.Text = DataSet.Tables(0).Rows(dr("OldIMEI")).ToString
' NewIMEITxt.Text = DataSet.Tables(0).Rows(dr("NewIMEI")).ToString
' ReasonTxt.Text = DataSet.Tables(0).Rows(dr("SwapReason")).ToString
' DateRepTxt.Text = DataSet.Tables(0).Rows(dr("DateSwapped")).ToString
' ValueTxt.Text = DataSet.Tables(0).Rows(dr("EstimatedAccumulatedValue")).ToString
'Next
If Not IsNothing(DataSet) Then
If DataSet.Tables(0).Rows.Count > 0 Then
OldIMEITxt.Text = DataSet.Tables(0).Rows(0)("OldIMEI")
NewIMEITxt.Text = DataSet.Tables(0).Rows(0)("NewIMEI")
ReasonTxt.Text = DataSet.Tables(0).Rows(0)("SwapReason")
DateRepTxt.Text = DataSet.Tables(0).Rows(0)("DateSwapped")
ValueTxt.Text = DataSet.Tables(0).Rows(0)("EstimateAccumulatedValue")
End If
End If
End Sub

ActiveReports can read data from your DataSet without additional loops in code.
if you return the data table to DataSource property of report object, then it is enough to set DataField property of TextBox controls and GroupHeader section correctly to show data in report. the rendering engine will go through all data rows automatically:
Private Sub SectionReport1_ReportStart(sender As Object, e As EventArgs) Handles MyBase.ReportStart
' bind TextBox controls to fields in table
Me.txtF1.DataField = "F1"
Me.txtF2.DataField = "F2"
' set the grouping field
Me.GroupHeader1.DataField = "F2"
' set the report data source
Me.DataSource = GetSampleData().Tables(0)
End Sub
Private Function GetSampleData() As DataSet
Dim ds = New DataSet()
Dim dt = ds.Tables.Add("TestData")
dt.Columns.Add("F1")
dt.Columns.Add("F2")
dt.Rows.Add("1", "0")
dt.Rows.Add("2", "0")
dt.Rows.Add("1", "1")
dt.Rows.Add("2", "1")
Return ds
End Function
if you prefer to read data row by row in "semi-automatic mode", then FetchData event handler can help here:
Dim i As Integer
Dim dt As DataTable = Nothing
Private Sub SectionReport2_ReportStart(sender As Object, e As EventArgs) Handles MyBase.ReportStart
i = 0
' bind TextBox controls to fields in table
Me.txtF1.DataField = "F1"
Me.txtF2.DataField = "F2"
' set the grouping field
Me.GroupHeader1.DataField = "F2"
dt = GetSampleData().Tables(0)
End Sub
Private Sub SectionReport2_DataInitialize(sender As Object, e As EventArgs) Handles MyBase.DataInitialize
Me.Fields.Add("F1")
Me.Fields.Add("F2")
End Sub
Private Sub SectionReport2_FetchData(sender As Object, eArgs As FetchEventArgs) Handles MyBase.FetchData
If dt.Rows.Count > i Then
Me.Fields("F1").Value = dt.Rows(i)(0)
Me.Fields("F2").Value = dt.Rows(i)(1)
eArgs.EOF = False
Else
eArgs.EOF = True
End If
i = i + 1
End Sub
Private Function GetSampleData() As DataSet
Dim ds = New DataSet()
Dim _dt = ds.Tables.Add("TestData")
_dt.Columns.Add("F1")
_dt.Columns.Add("F2")
_dt.Rows.Add("1", "0")
_dt.Rows.Add("2", "0")
_dt.Rows.Add("3", "1")
_dt.Rows.Add("4", "1")
Return ds
End Function
also, i would recommend to look at the sample with run time data binding in the ActiveReports installation package.
here is a link to the sample description on the official site:
Unbound Data

This is how you would loop through to get all rows, but in this example the only data that will be left in the textboxes will be the last row.
if you want to concatenate each rows information in the specified text box then you should have the information like this.
OldIMEITxt.Text = OldIMEITxt.Text & dr("OldIMEI")
Loop Code
For each dr as Datarow in DataSet.Tables(0).Rows
OldIMEITxt.Text = dr("OldIMEI")
NewIMEITxt.Text = dr("NewIMEI")
ReasonTxt.Text = dr("SwapReason")
DateRepTxt.Text = dr("DateSwapped")
ValueTxt.Text = dr("EstimateAccumulatedValue")
Next

Related

How to freeze merged columns in data grid view when scrolling vertically?

I have a data grid view where I need the columns to be frozen or fixed when scrolling vertically.
I have a data grid view control in vb.net windows application which displays the data in a parent-child hierarchy(as shown below). The first column displays the parent data and the second column displays all its child data. The child data in the second column can be as much as 100 rows or even more. So when scrolling down through the grid, the value in the first column does not remain there as it is while the values in the second column(i.e. the child data) scrolls down. So if the user wants to check to which parent, the current child info belongs to, then again he will have to scroll up to the starting of the column to find the name of the parent. I want the values in the first column to be displayed or frozen till it reaches the end of the list of its child values in the grid or at least till the next row where the next parent data starts. I have suggested the client to go with a tree view but they are not agreeing and need it in a data grid view itself. Is there anyway to achieve this in a data grid view?
Thanks in advance.
You can't freeze a row (in runtime, on dgv scrolling) with index greater than zero because all those before are frozen and at that point you can't scroll your datagridview.
If I understood correctly what you want I wrote this class quickly (probably should be optimized). Usage is simple.
1 - First create your own datagridview.
2 - then add your columns and rows (IMPORTANT: Put a “X” in the Tag in each row is a Parent or is considered as title for other rows as you seen in TestPopulate method) .
3 - Call the class I made by passing the datagridview (you created first) as a parameter. At this point this control takes its size, placement and REPLACE YOUR DATAGRIDVIEW .
Private Class CustomDgv
Inherits Panel
Dim WithEvents TopDgv As DataGridView = New DataGridView
Dim WithEvents DownDgv As DataGridView = New DataGridView
Dim Cols As Integer
' This variable is in case you have more rows as "headrow"
' In TestPopulate you can see how to get those
Dim listOfOwnerRows As List(Of Integer) = New List(Of Integer)
Dim currentTopRow As Integer = -1
Protected Overloads Property Height As Integer
Get
Return MyBase.Height
End Get
Set(value As Integer)
MyBase.Height = value
TopDgv.Height = TopDgv.RowTemplate.Height - 1
DownDgv.Height = value - TopDgv.Height - 1
End Set
End Property
Protected Overloads Property Width As Integer
Get
Return MyBase.Width
End Get
Set(value As Integer)
MyBase.Width = value
TopDgv.Width = value - 1
DownDgv.Width = value - 1
End Set
End Property
Sub New(dgvOriginal As DataGridView)
DownDgv = dgvOriginal
Dim parentCtrl As Control = dgvOriginal.Parent
parentCtrl.Controls.Remove(dgvOriginal)
parentCtrl.Controls.Add(Me)
Me.Location = DownDgv.Location
Me.Size = DownDgv.Size
Me.BorderStyle = DownDgv.BorderStyle
TopDgv.Width = Width - 2 - SystemInformation.VerticalScrollBarWidth
TopDgv.Height = TopDgv.RowTemplate.Height
TopDgv.ScrollBars = ScrollBars.None
TopDgv.ColumnHeadersVisible = False
TopDgv.BorderStyle = BorderStyle.None
DownDgv.ColumnHeadersVisible = False
DownDgv.BorderStyle = BorderStyle.None
TopDgv.Left = 0
DownDgv.Left = 0
DownDgv.Width = Width - 2
DownDgv.Height = Height - 2
For Each Col As DataGridViewColumn In DownDgv.Columns
Dim cIndex As Integer = TopDgv.Columns.Add(Col.Clone)
If Col.Frozen Then
TopDgv.Columns(cIndex).Frozen = True
End If
Cols += 1
Next
DownDgv.Top = 0
Me.Controls.Add(TopDgv)
Me.Controls.Add(DownDgv)
If DownDgv.Rows.Count > 0 Then
listOfOwnerRows = (From R As DataGridViewRow In DownDgv.Rows
Where R.Tag = "X"
Select R.Index).ToList
If listOfOwnerRows.Count > 0 Then
SetFrosenRow(listOfOwnerRows(0))
End If
End If
End Sub
Protected Sub SetFrosenRow(index As Integer)
If DownDgv.Rows.Count > index Then
TopDgv.Rows.Clear()
TopDgv.Rows.Add()
Dim currentRIndex As Integer = DownDgv.FirstDisplayedScrollingRowIndex
'If you want onlly the base row
For i As Integer = 0 To Cols - 1
TopDgv.Rows(0).Cells(i).Value = DownDgv.Rows(index).Cells(i).Value
Next
'Or else get the diplayed on top row
TopDgv.Rows(0).DefaultCellStyle = New DataGridViewCellStyle With {
.BackColor = Color.Bisque
}
currentTopRow = index
End If
End Sub
Protected Sub SetChildValuesInTopRow(index As Integer)
For i As Integer = 1 To Cols - 1
TopDgv.Rows(0).Cells(i).Value = DownDgv.Rows(index).Cells(i).Value
Next
End Sub
Private Sub DownDgv_Scroll(sender As Object, e As ScrollEventArgs) Handles DownDgv.Scroll
Try
If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
Dim topR As Integer = DownDgv.FirstDisplayedScrollingRowIndex
'If you want in top row the current value that is in the top uncomment this
SetChildValuesInTopRow(topR)
If listOfOwnerRows.Count > 0 Then
Dim rToSetAsOwner As Integer = listOfOwnerRows(listOfOwnerRows.Count - 1)
For i As Integer = listOfOwnerRows.Count - 1 To 0 Step -1
If listOfOwnerRows(i) <= topR Then
rToSetAsOwner = listOfOwnerRows(i)
Exit For
End If
Next
If rToSetAsOwner <> currentTopRow Then
SetFrosenRow(rToSetAsOwner)
End If
Console.WriteLine("rToSetAsOwner: " & rToSetAsOwner)
End If
Else
TopDgv.HorizontalScrollingOffset = DownDgv.HorizontalScrollingOffset
End If
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
End Class
Usage:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
' first populate you grid putting a tag in each row which is a header/parent/title for other rows
TestPopulate()
Dim customControl As Control = New CustomDgv(DataGridView1)
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
Sub TestPopulate()
For i As Integer = 0 To 100
DataGridView1.Rows.Add()
If i = 0 Then
DataGridView1.Rows.Item(0).Cells(0).Value = "Owner 0"
DataGridView1.Rows(0).Tag = "X"
End If
If i = 50 Then
DataGridView1.Rows.Item(50).Cells(0).Value = "Owner 50"
DataGridView1.Rows(50).Tag = "X"
End If
If i = 70 Then
DataGridView1.Rows.Item(70).Cells(0).Value = "Owner 70"
DataGridView1.Rows(70).Tag = "X"
End If
DataGridView1.Rows.Item(i).Cells(1).Value = "child_" & i.ToString & "_1"
DataGridView1.Rows.Item(i).Cells(2).Value = "child_" & i.ToString & "_2"
Next
End Sub
I hope I have been helpful

Read data from dynamically created text-box

I'm trying to collect data after creating dynamic text-box with vb.net
Private Sub btn_OK_lines_number_Click(sender As Object, e As EventArgs)
Handles btn_OK_lines_number.Click
Dim i As Integer
Dim x As Integer
Dim Z As Integer
Z = 150
If IsNumeric(txt_lines_number.Text) Then
Int32.TryParse(txt_lines_number.Text, x)
For i = 1 To x
Dim newTB As New TextBox
Dim newLB As New Label
newLB.Name = "lbl_workstation_number_line" & i
newLB.Text = "Nbr Work Station in Line" & i
newLB.Size = New Size(190, 20)
newLB.ForeColor = Color.White
newLB.Font = New Font("consolas", 12, FontStyle.Regular, GraphicsUnit.Pixel)
newLB.Location = New Point(20, Z + i * 30)
newTB.Name = "Textbox" & i
newTB.Size = New Size(170, 20)
newTB.Location = New Point(200, Z + i * 30)
Me.Controls.Add(newTB)
Me.Controls.Add(newLB)
Next
i = i + 1
Else
MessageBox.Show("please enter a number")
txt_lines_number.Text = ""
End If
End Sub
Let's say you just have one row, and only create one TextBox. You set the name here:
newTB.Name = "Textbox" & i
where the resulting TextBox is named Textbox1. The problem is you can't just reference the identifier Textbox1 directly in your code, as you do with txt_lines_number. You can't even reference it as a member of the class (Me.Textbox1). This name didn't exist at compile time, and so it's not an identifier you can use, and it's not a member of the class at all. There was never a matching Dim statement for that name.
What you can do, though, is look again in the Controls collection where you added the TextBox to the form:
Me.Controls("Textbox1")
or
Me.Controls("Textbox1").Text
You may also need to cast the value to a TextBox:
Dim box As TextBox = DirectCast(Me.Controls("Textbox1"), TextBox)
MessageBox.Show(box.Text)
Remember that case matters here.
Further saving this in a DB is out of scope for one question. There are as many ways to do that as there are programmers in the world. You should make your own attempt first, and come back here with a new question when you run into specific problems.
Thank you,
this is my attempt and it is done !
Dim userInput As TextBox = Form1.Controls.Item("TextBox" & i.ToString)
mycommand.Parameters.AddWithValue("#workstation", userInput.Text)
:D
Because you creating dynamic amount of input controls, right tool for the job will be DataGridView control.
Create a class to represent your data
Public Class LineInfo
Public Property Number As Integer
Public Property WorkStationNumber As Integer
End Class
Create `DataGridView in the form designer.
Private Sub btn_OK_lines_number_Click(sender As Object, e As EventArgs) Handles btn_OK_lines_number.Click
Dim linesAmount As Integer
If Integer.TryParse(txt_lines_number.Text, linesAmount) = False Then
MessageBox.Show("please enter a number")
txt_lines_number.Text = ""
Exit Sub
End If
' Create class instance for every line
Dim lines =
Enumerable.Range(1, linesAmount)
.Select(Function(i) New LineInfo With { .Number = i })
.ToList()
'Set lines as DataSource to the DataGridView
Me.DataGridView1.DataSource = lines
End Sub
DataGridView will display all lines and provide input fields to update work station numbers.
You can access updated lines later by casting DataSource back to the List
Dim lines = DirectCast(Me.DataGridView1.DataSource, List(Of LineInfo))
' Now you can access all data and save it to the database
Dim parameters =
lines.Select(Function(line)
Return new SqlParameter With
{
.ParameterName = $"#workstation{line.Number}",
.SqlDbType = SqlDbType.Int,
.Value = line.WorkStationNumber
}
End Function)
.ToList()
myCommand.Parameters.AddRange(parameters)
You can freely change style, font colors of different columns in the datagridview.

How To Refresh GridlookupEdit datasource with another datasource

here I will explain what I want to ask.
before I explain what is my problem.
I created a program and I add 1 forms, in my form I add combobox and components of deexpress is GridLookupEdit.
the function of the combobox itself which displays options category.
while Gridlookup will display data from the database based on the category selected from Combobox.
At the time I chose the first category, namely 'Study Program' from the combobox, then the data is successfully performing in GridLookup.
But When I choose a category of the Other, which is the data based Category I choose the existing data in the database, but it can not show all GridLookup.
And Gridlookup only list items that previously emptied and menyisahkan Name column of the previous data.
for code that I use and I Attach screenshot on the following page:
Private Sub CBCariOpt_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBCariOpt.SelectedIndexChanged
With CBCariOpt
If .SelectedIndex = -1 Or .Text = "" Then Exit Sub
If .SelectedIndex = 0 Or .SelectedIndex = 1 Or .SelectedIndex = 4 Or .SelectedIndex = 5 Then
GLOptCari.Properties.DataSource = DBNull.Value
FilDataBantuGridLookup(.SelectedIndex, GLOptCari, DKritria, General.Constant.ConfigPath)
Else
GLOptCari.SendToBack()
With DKritria
.BringToFront()
.Text = ""
.Focus()
.Select()
End With
End If
End With
End Sub
Code For FillData into Data source Gridlookup
Private Sub FilDataBantuGridLookup(SelIndex As Integer, GL As GridLookUpEdit, dkritria As MetroTextBox, path As String)
With QD
If SelIndex = 0 Then .FillToGridLook(QProdi, GL, "PRODI", "KODE", path)
If SelIndex = 1 Then .FillToGridLook(QJursan, GL, "JURUSAN", "KODE", path)
If SelIndex = 4 Then .FillToGridLook(QThAnktan, GL, "THN_ANGKATAN", "THN_ANGKATAN", path)
If SelIndex = 5 Then .FillToGridLook(QThWisda, GL, "THN_WISUDA", "THN_WISUDA", path)
.Dispose()
GL.Properties.PopupFormSize = New Size With {.Width = GL.Width} 'GridLookUpEdit
GL.BringToFront()
dkritria.SendToBack()
End With
End Sub
Query To DataSource In GridLookupEdit :
Public Sub FillToGridLook(ByVal query As String, FilCtl As DevExpress.XtraEditors.GridLookUpEdit, SDis As String, SVal As String, ByVal path As String)
Using KN As New Koneksi.Koneksi
If KN.OpenFromFile(path) Then
Using da As New MySqlDataAdapter(query, KN.OKoneksi)
Dim dt As New DataTable
dt = New DataTable
dt.Rows.Clear()
dt.Columns.Clear()
da.Fill(dt)
If dt.Rows.Count > 0 Then
FilCtl.Refresh()
FilCtl.RefreshEditValue()
With FilCtl.Properties
.DataSource = Nothing
.DataSource = dt
.DisplayMember = SDis
.ValueMember = SVal
.ImmediatePopup = True
.View.OptionsView.ShowColumnHeaders = False
End With
FilCtl.Refresh()
End If
da.Dispose()
dt.Dispose()
End Using
End If
KN.Dispose()
End Using
End Sub
For Result :
Screen Shoot
therefore, I would like to ask,:
How to refresh the DataSource in gridlookup who initially had to accommodate the initial data prior to the new data?

Pass values from Numeric control into corresponding cells in DataGridView control with a button

I want to move the values from each of four Numeric Up/Down controls into the DataGridView columns/rows with a button. For instance, the operator sets the values in the up/down numeric controls and then clicks the button. The program should then add a new row to the DataGridView and pass whatever values that are in the Numeric Up/Down controls into the new cells of the new row. As of now, I have the adding new rows part of it working as well as the delete button working (delete last row of the DataGridView). Now, how to pass the Numeric control values into the cell of the new row of the DataGridView with the button? Each new row created of the DatGridView has four cells to correspond to the four Numeric up/down controls. Thank you.
Private Sub addStep_btn_Click(sender As Object, e As EventArgs) Handles addStep_btn.Click
LftMtr_Data_Grid.ColumnCount = 4
LftMtr_Data_Grid.RowCount = LftMtr_Data_Grid.RowCount + 1
LftMtr_Data_Grid.Columns(0).HeaderText = " Spindle Speed (rpm)"
LftMtr_Data_Grid.Columns(1).HeaderText = " Accel Rate (rpm/S)"
LftMtr_Data_Grid.Columns(2).HeaderText = " Decel Rate (rpm/S)"
LftMtr_Data_Grid.Columns(3).HeaderText = " Time (S)"
For Each c As DataGridViewColumn In LftMtr_Data_Grid.Columns
c.Width = 120
Next
Dim rowNumber As Integer = 1
For Each row As DataGridViewRow In LftMtr_Data_Grid.Rows
If row.IsNewRow Then Continue For
row.HeaderCell.Value = "Step " & rowNumber
rowNumber = rowNumber + 1
Next
LftMtr_Data_Grid.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
rowCount1 = LftMtr_Data_Grid.RowCount
txtBox1.Text = rowCount1
End Sub
Try something like below. Note that you don't need to setup the column headers everytime, just once in the Load() event would do!
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LftMtr_Data_Grid.ColumnCount = 4
LftMtr_Data_Grid.Columns(0).HeaderText = " Spindle Speed (rpm)"
LftMtr_Data_Grid.Columns(1).HeaderText = " Accel Rate (rpm/S)"
LftMtr_Data_Grid.Columns(2).HeaderText = " Decel Rate (rpm/S)"
LftMtr_Data_Grid.Columns(3).HeaderText = " Time (S)"
For Each c As DataGridViewColumn In LftMtr_Data_Grid.Columns
c.Width = 120
Next
LftMtr_Data_Grid.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub
Private Sub addStep_btn_Click(sender As Object, e As EventArgs) Handles addStep_btn.Click
Dim values() As Object = {NumericUpDown1.Value, NumericUpDown2.Value, NumericUpDown3.Value, NumericUpDown4.Value}
Dim index As Integer = LftMtr_Data_Grid.Rows.Add(values)
LftMtr_Data_Grid.Rows(index).HeaderCell.Value = "Step " & (index + 1)
LftMtr_Data_Grid.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub
Private Sub delStep_btn_Click(sender As Object, e As EventArgs) Handles delStep_btn.Click
If LftMtr_Data_Grid.Rows.Count > 0 Then
LftMtr_Data_Grid.Rows.RemoveAt(LftMtr_Data_Grid.Rows.Count - 1)
End If
End Sub
End Class
I figured it out on my own. Here is what I came up with:
Dim rowNumber As Integer = 1
For Each row As DataGridViewRow In LftMtr_Data_Grid.Rows
If row.IsNewRow Then Continue For
row.HeaderCell.Value = "Step " & rowNumber
LftMtr_Data_Grid.CurrentCell = LftMtr_Data_Grid.Rows(LftMtr_Data_Grid.RowCount - 1).Cells(0)
LftMtr_Data_Grid.CurrentRow.Cells(0).Value = LftMtr_Speed_Incr.Value
LftMtr_Data_Grid.CurrentRow.Cells(1).Value = LftMtr_Accel_Incr.Value
LftMtr_Data_Grid.CurrentRow.Cells(2).Value = LftMtr_Decel_Incr.Value
LftMtr_Data_Grid.CurrentRow.Cells(3).Value = test_Time_Incr1.Value
rowNumber = rowNumber + 1
Next
This application works with an empty or partially filled dataGridView. When the user clicks the button, the code creates a new row in the dataGridView, makes the new row the selected row, and then finally populates each of the cells in the new row with the values that are in the four Numeric Up/down controls (could be a text box too depending on your application). This is code copied directly from my specific application. Yours may differ slightly with variable names, label text, etc.

Dual check box selection vb / winform

I am after a little bit of help:
I have a datagridview control where I add another couple of columns (as check boxes) in order to select multiple rows. When I select the first checkbox column I want the second to be selected automatically, but can then be de-selected if required. And if the first checkbox is deselected, the 2nd is auto deselected too.
I have this working with the following code:
Private Sub dgvBikeAvailability_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvBikeAvailability.CellContentClick
Debug.Print("Row index = " + e.RowIndex.ToString + ". Column index = " + e.ColumnIndex.ToString + ". Column Name = ")
'Debug.Print()
'when a bike is selected, a helmet is automatically selected, but can be deselected if the customer requires
If e.ColumnIndex = 0 Then
dgvBikeAvailability.Rows(e.RowIndex).Cells(0).Value = Not dgvBikeAvailability.Rows(e.RowIndex).Cells(0).Value
dgvBikeAvailability.Rows(e.RowIndex).Cells(1).Value = dgvBikeAvailability.Rows(e.RowIndex).Cells(0).Value
End If
End Sub
Unfortunately, wen the datagridview is refreshed, the column indexes are incremented, and the column indexes for the 2 check box columns are now 2 and 3.
I would like to be able to refer to them by name. They are declared in the sub that refreshes the datagridview:
colBikeSelectionCheckBox.HeaderText = "Select Bike"
colBikeSelectionCheckBox.Name = "colSelectBike")
colHelmetCheckBox.Name = "colSelectHelmet"
dgvBikeAvailability.Columns.Add(colHelmetCheckBox)
but the System.Windows.Forms.DataGridViewCellEventArgs class does not allow me to select column name.
Any ideas and suggestions will be greatly appreciated!
Edit: More in depth code segment:
Private Sub frmBikeHire_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
refreshGrid()
getStaffMember()
End Sub
'loads and refreshes the dgv
Private Sub refreshGrid()
Dim i As Integer
'initially fill in the rental dates for current day
txtDateFrom.Text = CStr(MonthCalendar1.SelectionRange.Start)
txtDateTo.Text = CStr(MonthCalendar1.SelectionRange.End)
'grab data from DB, set as dgv datasource
startDate = CStr(MonthCalendar1.SelectionRange.Start)
endDate = CStr(MonthCalendar1.SelectionRange.End)
dtBikeAvailability = sqlFuncDB_getDataTable("SELECT * FROM tb_bikeDetail WHERE bikeid NOT IN (SELECT DISTINCT bikeid FROM tb_bikemovements WHERE bikeMovementDate BETWEEN '" + func_convertDateSQLSERVER(startDate) + "' AND '" + func_convertDateSQLSERVER(endDate) + "') AND bikeid NOT IN (SELECT tb_bikemovements.bikeid FROM tb_bikemovements JOIN (SELECT bikeid, max(bikemovementdate) bmd FROM tb_bikemovements WHERE bikemovementdate < '" + func_convertDateSQLSERVER(startDate) + "' group by bikeid) lastmove ON lastmove.bikeid=tb_bikemovements.bikeid AND lastmove.bmd=tb_bikemovements.bikemovementdate WHERE bikeMovementType = '0')")
dvBikeAvailability = New DataView(dtBikeAvailability)
dgvBikeAvailability.DataSource = dvBikeAvailability
'switch off all columns
For i = 0 To dgvBikeAvailability.Columns.Count - 1
dgvBikeAvailability.Columns(i).Visible = False
Next
'displays only relevant column(s)
dgvBikeAvailability.Columns("bikeName").Visible = True
dgvBikeAvailability.Columns("bikeName").HeaderText = "Bike Name"
dgvBikeAvailability.Columns("bikeStyle").Visible = True
dgvBikeAvailability.Columns("bikeStyle").HeaderText = "Bike Style"
dgvBikeAvailability.Columns("bikeColour").Visible = True
dgvBikeAvailability.Columns("bikeColour").HeaderText = "Bike Colour"
'remove this line for program deployment
dgvBikeAvailability.Columns("bikeID").Visible = True
dgvBikeAvailability.Columns("bikeID").HeaderText = "Bike Number"
'add new check box column for selecting the bike
Dim colBikeSelectionCheckBox As New DataGridViewCheckBoxColumn
colBikeSelectionCheckBox.DataPropertyName = "PropertyName"
colBikeSelectionCheckBox.HeaderText = "Select Bike"
colBikeSelectionCheckBox.Name = "colSelectBike"
dgvBikeAvailability.Columns.Add(colBikeSelectionCheckBox)
'add new column for selecting helmet - consider adding as default setting
Dim colHelmetCheckBox As New DataGridViewCheckBoxColumn
colHelmetCheckBox.DataPropertyName = "PropertyName"
colHelmetCheckBox.HeaderText = "Helmet?"
colHelmetCheckBox.Name = "colSelectHelmet"
dgvBikeAvailability.Columns.Add(colHelmetCheckBox)
dgvBikeAvailability.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End Sub
Private Sub MonthCalendar1_DateChanged(sender As System.Object, e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
refreshGrid()
End Sub
Private Sub dgvBikeAvailability_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvBikeAvailability.CellClick
Debug.Print("Row index = " + e.RowIndex.ToString + ". Column index = " + e.ColumnIndex.ToString + ". Column Name = " + dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectBike").OwningColumn.ToString)
If dgvBikeAvailability.Columns(e.ColumnIndex).Name = "colSelectBike" Then
dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectBike").Value = Not dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectBike").Value
dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectHelmet").Value = dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectBike").Value
End If
End Sub
I am unsure why that doesn't work. Each time the calendar control is changed it refreshes the datagridview, which is increasing the column index.
You can use the column index provided by the DataGridViewCellEventArgs class to retrieve the column and from that get the name to compare.
So something like:
If dgvBikeAvailability.Columns(e.ColumnIndex).Name == "colSelectBike" Then
' Your logic here
End If
For referencing the columns within your code to toggle the CheckBoxes, you can very happily use the names, and in fact do not need the index provided by the event args. The event args index appears to only be used to check that one of your desired columns was selected.
dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectBike").Value = Not dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectBike").Value
dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectHelmet").Value = dgvBikeAvailability.Rows(e.RowIndex).Cells("colSelectBike").Value
Although the code above should work it appears that something odd is happening with your grid. A quick solution is to instead of using the .Add() to use the .Insert() method of the DataGridView, which provided an index parameter, allowing you to directly control where your column goes.