How to Add Combobox in datagridview only for header? - vb.net

This is my code - I want to add combobox for my second column. So that whatever value user enter in that column I can use for further operation. I have changed Items in combo intentionally.
Dim cmbHeaderCell As New ComboBox
cmbHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList
cmbHeaderCell1.Visible = True
cmbHeaderCell1.Items.Add("India")
cmbHeaderCell1.Items.Add("China")
DGrdVLoadStb.Columns(1).Width = cmbHeaderCell1.Width
cmbHeaderCell1.Location = DGrdVLoadStb.GetCellDisplayRectangle(1, -1,True).Location
cmbHeaderCell1.Size = DGrdVLoadStb.Columns(1).HeaderCell.Size
cmbHeaderCell1.SelectedIndex = 0
Here I am getting the location (0, 0) from GetCellDisplayRectangle(1, -1, True) method. I have checked many related questions on stackOverflow but I didn't get perfect answer please help me. Thank you in advance.

As I wrote in comment, I managed to do this by using,
1. width of first column of datagridview adding 2 to it and
2. making it as X of my combobox loacation and Y as 2.
here is my code,
Dim cmbHeaderCell1 As New ComboBox
cmbHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList
cmbHeaderCell1.Visible = True
cmbHeaderCell1.Items.Clear()
cmbHeaderCell1.Items.Add("India")
cmbHeaderCell1.Items.Add("China")
DGrdVLoadStb.Columns(1).Width = cmbHeaderCell1.Width
'Dim X As Integer = DGrdVLoadStb.GetCellDisplayRectangle(1, -1, False).Location.X
'Dim Y As Integer = DGrdVLoadStb.GetCellDisplayRectangle(1, -1, True).Location.Y
cmbHeaderCell1.Location = New Point(datagridview1.Columns(0).Width + 2, 2)
cmbHeaderCell1.Size = DGrdVLoadStb.Columns(1).HeaderCell.Size
cmbHeaderCellStressRate.DropDownStyle = ComboBoxStyle.DropDownList
DGrdVLoadStb.Controls.Add(cmbHeaderCell1)
cmbHeaderCell1.SelectedIndex = 0
If any one have better solution please post it I will accept it.

Related

DevExpress GridControl : Checkedit on Column Trouble

I try to make grid columns during runtime, and one of column using repositorycheckedit, I tried several methods but none worked
here is my code
Dim dtSet As New DataSet()
Dim dtTable As New DataTable("menu")
Dim grCol As New DataColumn
Dim rpChk As New RepositoryItemCheckEdit
grCol.ColumnName = "smenu"
grCol.Caption = "Menu"
dtTable.Columns.Add("Menu")
dtTable.Columns(0).ColumnName = "smenu"
dtTable.Columns.Add("New")
dtTable.Columns(1).ColumnName = "snew"
dtTable.Rows.Add("Nama", 1)
dtSet.Tables.Add(dtTable)
gr.DataSource = dtSet
gr.DataMember = "menu"
rpChk.ValueChecked = 1
rpChk.ValueUnchecked = 0
grV.Columns("snew").ColumnEdit = rpChk
somehow, the column supposed to show checkbox always grayed out, if i try to mark the checkbox, it will become null after lost focus/change cell
pplease anyone.? thanks
Source #2 (After revised) and still no result as expected
Dim dtTable As New DataTable("menu")
Dim rpChk As New RepositoryItemCheckEdit
dtTable.Columns.Add("Menu")
dtTable.Columns(0).ColumnName = "smenu"
dtTable.Columns.Add("New")
dtTable.Columns(1).ColumnName = "snew"
dtTable.Rows.Add("Nama", CSByte(1))
dtTable.Rows.Add("Nama1", CSByte(0))
dtTable.Rows.Add("Nama2", CSByte(1))
dtTable.Rows.Add("Nama3", CSByte(0))
gr.DataSource = dtTable
rpChk.ValueChecked = 1
rpChk.ValueUnchecked = 0
grV.Columns("snew").ColumnEdit = rpChk
After tried several times, maybe someone needs answer
So finally I solved it. When create columns we need to define what type for the columns
dtTable.Columns.Add("Menu",GetType(String))
dtTable.Columns(0).ColumnName = "smenu"
dtTable.Columns.Add("New",GetType(Boolean))
dtTable.Columns(1).ColumnName = "snew"
And things finally worked.

Libreoffice Unable to fill ListBox because I could not declare the listbox righteously

I have a Listbox in my Calc Worksheet and I am trying to address it for filling it with data
DialogLibraries.LoadLibrary("Standard")
oDialog = CreateUnoDialog(DialogLibraries.Standard)
listBoxGruppe = oEvent.Source.Context.getControl("Drop_Down_5") 'oDialog.getControl("Drop_Down_5")
aItems = sheetb.getCellRangeByName(subStrRan).dataarray
Dim sItems(ubound(aItems))
For i = 0 To ubound(aItems)
sItems(i) = aItems(i)(0)
Next i
listBoxGruppe.addItems(sItems, 0)
oDialog.Execute()
However, it seems u need to call a certain Dialog, but idk what is the name of my Dialog for this scenario - this is how my code behind looks like
I think you don't need dialogs at this situation
dim RangeAddr as new com.sun.star.table.CellRangeAddress
RangeAddr.EndColumn = 1
RangeAddr.EndRow = Cell.CellAddress.Row
RangeAddr.Sheet = 2
RangeAddr.StartColumn = 1
RangeAddr.StartRow = 0
dim initParam(0) as new com.sun.star.beans.NamedValue
initParam(0).Name="CellRange"
initParam(0).Value = RangeAddr
CellRangeListSource = doc.createInstanceWithArguments("com.sun.star.table.CellRangeListSource", initParam )
listBoxGruppe.setListEntrySource CellRangeListSource

How to add a new row into DataGridView and fill it with data

This is my code
Public Sub InvoicItmCall()
'If IteDataTb.Rows.Count = 0 Then Exit Sub
SellingDGView.Rows.Add()
SellingDGView.Rows.Item(0).Cells(0).Value = SellingDGView.Rows.Item(0).Cells(0).Value + 1
SellingDGView.Rows.Item(0).Cells(1).Value = IteDataTb.Rows(0).Item("IteFName")
SellingDGView.Rows.Item(0).Cells(2).Value = IteDataTb.Rows(0).Item("IteFSizeUnit")
SellingDGView.Rows.Item(0).Cells(4).Value = IteDataTb.Rows(0).Item("IteFSellpris")
SellingDGView.Rows.Item(0).Cells(6).Value = IteDataTb.Rows(0).Item("IteVat")
'Dim unused = SellingDGView.Rows.Add(rows)
End Sub
Right now, the code works fine and fills the first row on the grid, this is for a barcode scanner. When I scan one barcode, it fills the row with the appropriate data. However, when I scan another code, it overwrites the older row.
I want it to add a new row and add the other data to it.
So when I scan a different item after the first one, it should fill up a new row.
How do I do that?
Thanks in advance :)
EDIT:
I have found a workaround
If IteDataTb.Rows.Count > 0 Then
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(row.Item("IteFName"), row.Item("IteFSizeUnit"), row.Item("IteFSellpris"), row.Item("IteVat"))
Next row
End If
This creates a new row every time and fills it. But now I want to fill specific cells.
How do I do that?
Right now it fills everything inappropriately.
If you wanted to do it the way you originally were, I'd do it like this:
Dim index = SellingDGView.Rows.Add()
Dim newRow = SellingDGView.Rows(index)
'Dim newRow = SellingDGView.Rows(SellingDGView.Rows.Add())
Dim dataRow = IteDataTb.Rows(0)
newRow.Cells(0).Value = SellingDGView.Rows(0).Cells(0).Value + 1
newRow.Cells(1).Value = dataRow("IteFName")
newRow.Cells(2).Value = dataRow("IteFSizeUnit")
newRow.Cells(4).Value = dataRow("IteFSellpris")
newRow.Cells(6).Value = dataRow("IteVat")
If you wanted to use the more appropriate overload of Add that I mentioned and you used in your second example:
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(SellingDGView.Rows(0).Cells(0).Value + 1,
row("IteFName"),
row("IteFSizeUnit"),
Nothing,
row("IteFSellpris"),
Nothing,
row("IteVat"))
Next
The If statement is pointless.
Also, I doubt that you're getting the next ID in the best way with that expression that adds 1 but that's a different question.
Every time that you need a new row, you can do as follows:
Dim i As Integer = SellingDGView.Rows.Add()
SellingDGView.Rows.Item(i).Cells(0).Value = i
SellingDGView.Rows.Item(i).Cells(1).Value = IteDataTb.Rows(j).Item("IteFName")
SellingDGView.Rows.Item(i).Cells(2).Value = IteDataTb.Rows(j).Item("IteFSizeUnit")
SellingDGView.Rows.Item(i).Cells(4).Value = IteDataTb.Rows(j).Item("IteFSellpris")
SellingDGView.Rows.Item(i).Cells(6).Value = IteDataTb.Rows(j).Item("IteVat")
Herein, I've assumed that the variable "j" indicates to your desired row in IteDataTb datagridview. I mean that you should control the "j" variable yourself. I can help you if know more about the IteDataTb.

How do I collapse white space in a list of string that was randomized

I have a list of strings that hold 10 answers.
Each question has a different amount of answers 2-10.
After randomizing the list, I end up with white space or empty spaces in my list depending on the number of answers.
After randomizing the list of let's say 2 answers, i would like to shift them back to position 0 and 1 in my list keeping the size of the list at 10 and of course keeping the order randomized.
I'm not sure how to programmatically solve this problem...
I've tried to sort/reverse the list after randomize, but of course, this removes the randomization.
I've tried to remove the white space with something like
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
but I then get an out of bounds when trying to write them back to my radiobuttons.text as there are 10 of them.
This is where writing my list of...
RadioAnswer1.Text = answerlist(0)
RadioAnswer2.Text = answerlist(1)
RadioAnswer3.Text = answerlist(2)
RadioAnswer4.Text = answerlist(3)
RadioAnswer5.Text = answerlist(4)
RadioAnswer6.Text = answerlist(5)
RadioAnswer7.Text = answerlist(6)
RadioAnswer8.Text = answerlist(7)
RadioAnswer9.Text = answerlist(8)
RadioAnswer10.Text = answerlist(9)
Ideally, I want the list randomized then however many answers there are written back into the list starting at 0 going down to 10.
I hope my question is clear.
Additional info edit
So here is how I'm loading the answers into the List Of..
Dim answerlist As New List(Of String)
WEFESQLConn.ConnectionString = connectstring
WEFESQLConn.Open()
WERESQLStatment.CommandText = "SELECT * FROM [WEFE Questions] WHERE QuestionID = " & SQLQuestionNum.ToString
WERESQLStatment.Connection = WEFESQLConn
WEFESQLRead = WERESQLStatment.ExecuteReader
If WEFESQLRead.HasRows Then
WEFESQLRead.Read()
lblQuestion.Text = WEFESQLRead.Item("Question").ToString
answerlist.Add(WEFESQLRead.Item("CorrectAnswer").ToString)
answerlist.Add(WEFESQLRead.Item("Answer2").ToString)
answerlist.Add(WEFESQLRead.Item("Answer3").ToString)
answerlist.Add(WEFESQLRead.Item("Answer4").ToString)
answerlist.Add(WEFESQLRead.Item("Answer5").ToString)
answerlist.Add(WEFESQLRead.Item("Answer6").ToString)
answerlist.Add(WEFESQLRead.Item("Answer7").ToString)
answerlist.Add(WEFESQLRead.Item("Answer8").ToString)
answerlist.Add(WEFESQLRead.Item("Answer9").ToString)
answerlist.Add(WEFESQLRead.Item("Answer10").ToString)
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
WEFESQLRead.Close()
WEFESQLConn.Close()
RadioAnswer1.Text = answerlist(0)
RadioAnswer2.Text = answerlist(1)
RadioAnswer3.Text = answerlist(2)
RadioAnswer4.Text = answerlist(3)
RadioAnswer5.Text = answerlist(4)
RadioAnswer6.Text = answerlist(5)
RadioAnswer7.Text = answerlist(6)
RadioAnswer8.Text = answerlist(7)
RadioAnswer9.Text = answerlist(8)
RadioAnswer10.Text = answerlist(9)
With this code I get the out of bounds as there are not enough answers to populate the answerlist.
Without
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
I get the spaces in my pre-drawn radio buttons.
I am all ready hiding the unused buttons - the issue there are 10 positions for the buttons and with the randomization of the list.
4 spots of 10 used image
Why don't you toggle the visibility of the controls dynamically after removing the blank entries from the list? Take a look at this example:
'Store all controls in a collection
Dim answers(9) As RadioButton = {RadioAnswer1, RadioAnswer2, RadioAnswer3, RadioAnswer4, RadioAnswer5, RadioAnswer6, RadioAnswer7, RadioAnswer8, RadioAnswer9, RadioAnswer10}
'Iterate through all answers
For index As Integer = 0 To answerlist.Count - 1
'Show the control and set the text
With answers(index)
.Text = answerlist.Item(index)
.Visible = True
End With
Next
'Loop through the rest of the answer controls
For index As Integer = answerlist.Count To answers.Length - 1
'Hide the control
answers(index).Visible = False
Next
Initial setup...
Dim radioButtons As New List(Of RadioButton)
radioButtons.Add(RadioAnswer1)
radioButtons.Add(RadioAnswer2)
...
radioButtons.Add(RadioAnswer10)
After removing blank answers from the randomized list...
For i = 0 To answerList.Count - 1
radioButtons(i).Text = answerList(i)
radioButtons(i).Visible = True
Next
For i = answerList.Count to radioButtons.Count - 1
radioButtons(i).Visible = False
Next
To close this out I ended up going with removing all the spaces from my ListOF then using a DO WHILE for each radiobutton.text entries.
example portion
answerlist = RandomizeListOrder(answerlist)
answerlist.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
Dim ALCount As Integer = answerlist.Count
Dim ALCounter = 0
Do
If ALCounter < ALCount Then
ALCounter += 1
RadioAnswer1.Text = answerlist(0)
ElseIf ALCounter = ALCount Then
Exit Do
End If
Maybe not very clean as there are 10 of these entries, but I'll work on that later. Just trying to get the idea out.
Thanks everyone for the suggestions they got me on the right path.

Capturing an event such as mouse click in Datagridview in VB

Update 5/21/17. Thank you for the suggestion of using a Table. That was helpful. I actually figured it out. I made myinputable a global variable by declaring the Dim statement at the top and making it a Datagridview type. Now I can turn it off in the other event that I needed to do it.
I am a novice. I have created a Datagridview in VB 2015 to capture a bunch of data from the use. When the user is finished with the data entry, I want to store the cell values in my variables. I do not know how to capture any event from my dynamically created datagridview "myinputable." My code is below. Please help.
Private Sub inputmodel()
Dim prompt As String
Dim k As Integer
'
' first get the problem title and the number of objectives and alternatives
'
prompt = "Enter problem title: "
title = InputBox(prompt)
prompt = "Enter number of criteria: "
nobj = InputBox(prompt)
prompt = "Enter number of alternatives: "
nalt = InputBox(prompt)
'
' now create the table
'
Dim Myinputable As New DataGridView
Dim combocol As New DataGridViewComboBoxColumn
combocol.Items.AddRange("Increasing", "Decreaing", "Threashold")
For k = 1 To 6
If k <> 2 Then
Dim nc As New DataGridViewTextBoxColumn
nc.Name = ""
Myinputable.Columns.Add(nc)
Else
Myinputable.Columns.AddRange(combocol)
End If
Next k
' now add the rows and place the spreadsheet on the form
Myinputable.Rows.Add(nobj - 1)
Myinputable.Location = New Point(25, 50)
Myinputable.AutoSize = True
Me.Controls.Add(Myinputable)
FlowLayoutPanel1.Visible = True
Myinputable.Columns(0).Name = "Name"
Myinputable.Columns(0).HeaderText = "Name"
Myinputable.Columns(1).Name = "Type"
Myinputable.Columns(1).HeaderText = "Type"
Myinputable.Columns(2).Name = "LThresh"
Myinputable.Columns(2).HeaderText = "Lower Threshold"
'Myinputable.Columns(2).ValueType = co
Myinputable.Columns(3).Name = "UThresh"
Myinputable.Columns(3).HeaderText = "Upper Threshold"
Myinputable.Columns(4).Name = "ABMin"
Myinputable.Columns(4).HeaderText = "Abs. Minimum"
Myinputable.Columns(5).Name = "ABMax"
Myinputable.Columns(5).HeaderText = "Abs. Maximum "
Myinputable.Rows(0).Cells(0).Value = "Help"
If Myinputable.Capture = True Then
MsgBox(" damn ")
End If
End Sub
As #Plutonix suggests, you should start by creating a DataTable. Add columns of the appropriate types to the DataTable and then bind it to the grid, i.e. assign it to the DataSource of your grid, e.g.
Dim table As New DataTable
With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
DataGridView1.DataSource = table
That will automatically add the appropriate columns to the grid if it doesn't already have any, or you can add the columns in the designer and set their DataPropertyName to tell them which DataColumn to bind to. As the user makes changes in the grid, the data will be automatically pushed to the underlying DataTable.
When you're done, you can access the data via the DataTable and even save the lot to a database with a single call to the Update method of a data adapter if you wish.