Dropdown property setting for a Comboboxcol in datagridview in vb.net - vb.net

I am facing a problem in VB.Net Winforms Application. I am using VS 2019.
The problem is the combobox column in Datagridview is not dropping down to show the list.
mDT and mDT1 are DataTables
ShowData is the name of the Datagridview
The properties in the designer for Show Data is shown below ( partially )
I tried various suggestions given in MSDN as well as here but am not successful. Might be because those might be old or are implemented differently in VS 2019.
The properties in the designer for Show Data is shown below ( partially )
[DGV Properties](https://i.stack.imgur.com/h60oO.jpg)
The Code
Dim mRow As DataRow
mDT = gDT_Structures.Copy()
mDT1 = New DataTable
mDT1.Columns.Add("PFID")
mDT1.AcceptChanges()
mRow = mDT1.NewRow()
mRow("PFID") = "PFID01"
mDT1.Rows.Add(mRow)
mDT1.AcceptChanges()
mRow = mDT1.NewRow()
mRow("PFID") = "PFID02"
mDT1.Rows.Add(mRow)
mDT1.AcceptChanges()
ShowData.Visible = True
ShowData.DataSource = mDT
'//Combobox column.
Dim ComboBoxcol = New DataGridViewComboBoxColumn()
ComboBoxcol.DataSource = mDT1
ComboBoxcol.Name = "PFID"
ComboBoxcol.ValueMember = "PFID"
ComboBoxcol.DisplayMember = "PFID"
ShowData.Columns.Add(ComboBoxcol)
For i = 0 To ShowData.Columns.Count() - 1
ShowData.Columns(i).ReadOnly = False
Next
ShowData.Select()
ShowData.Show()
The results View
[DGV Result](https://i.stack.imgur.com/l1Wi1.jpg)
Request guidance.
Thanks in Advance.

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.

Autosize Rows for Datagridview in VB.net not working

Hi, i'm making a memo type of data grid with Dynamic data from Database
and somehow i cannot make the rows auto resize.
i tried pretty much everything, here's some snippet of my code which i put on Form Load sub:
DataGridView2.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
DataGridView2.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
DataGridView2.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView2.Columns(2).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
Which came out like this:
After That i tried using this on DataGridView2.CellPainting Event
If e.Value Is Nothing Then Return
Dim s = e.Graphics.MeasureString(e.Value.ToString(), DataGridView2.Font)
If s.Width > DataGridView2.Columns(e.ColumnIndex).Width Then
Using gridBrush As Brush = New SolidBrush(DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As Pen = New Pen(gridBrush)
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
e.Graphics.DrawString(e.Value.ToString(), DataGridView2.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault)
DataGridView2.Rows(e.RowIndex).Height = CInt((s.Height * Math.Ceiling(s.Width / DataGridView2.Columns(e.ColumnIndex).Width)))
e.Handled = True
End Using
End Using
End If
which came out like this:
Tried fiddling with all properties but i seems unable to figure it out, Thank you in advance
Here's the process I followed:
Dropped a new datagridview on a form (actually I dragged it out of the datasources window, because I already had a dataset in the project)
Chose edit columns, chose a column, clicked [...] next to DefaultCellStyle for one of the string columns, set WrapMode to True
Set AutoSizeRowsMode of the DGV to AllCells
Inserted a long string into the underlying datatable the dgv was bound to:
And the result wrapped:
You can consider centering the elements in the DataGridView:
DataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
DataGridView2.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Result looks like:

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.

How to Add Combobox in datagridview only for header?

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.

vb.net crystal report view issues

I have a vb.net application that has a list of reports that you can select from.
When you select a report it loads the data and displays the report in the Crystal Report Viewer. However, The report display is way to big and the only way to resize it is to restore down the screen and then restore it to full screen. The scroll bars are only visible after you restore the page down and up.
The generated code for the viewer is as follows.
Me.CrystalReportViewer1.ActiveViewIndex = -1
Me.CrystalReportViewer1.AutoValidate = System.Windows.Forms.AutoValidate.EnablePreventFocusChange
Me.CrystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.CrystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Arrow
Me.CrystalReportViewer1.DisplayBackgroundEdge = False
Me.CrystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill
Me.CrystalReportViewer1.EnableDrillDown = False
Me.CrystalReportViewer1.Location = New System.Drawing.Point(230, 0)
Me.CrystalReportViewer1.Name = "CrystalReportViewer1"
Me.CrystalReportViewer1.SelectionFormula = ""
Me.CrystalReportViewer1.ShowCloseButton = False
Me.CrystalReportViewer1.ShowGroupTreeButton = False
Me.CrystalReportViewer1.ShowRefreshButton = False
Me.CrystalReportViewer1.ShowTextSearchButton = False
Me.CrystalReportViewer1.Size = New System.Drawing.Size(517, 715)
Me.CrystalReportViewer1.TabIndex = 1
Me.CrystalReportViewer1.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
Me.CrystalReportViewer1.ViewTimeSelectionFormula = ""
I am calling it like this:
Dim rpt As String
rpt = ListBox1.Items(ListBox1.SelectedIndex)
If Not CrystalReportViewer1.ReportSource Is Nothing Then CrystalReportViewer1.ReportSource.dispose()
Select Case rpt
Case "Scoot"
myreport = New graduation
LoadDatabaseInfo(myreport)
myreport.SetParameterValue("doop", indrno.Text)
'myreport.SetParameterValue("dte", indate.Value)
myreport.SetParameterValue("name", txb.Text)
CrystalReportViewer1.ReportSource = myreport
CrystalReportViewer1.Refresh()
CrystalReportViewer1.Zoom(55)
How do I fix This.. Thank you so much
Rather than zooming to 55 percent, try using a value of 1 to fit to the width of the page or 2 to fit in the page (from MSDN).
I was able to accomplish my resizing needs by setting the height and width properties of the window to set pixel values.
i.e.
CrystalReportViewer.Width = 1100
CrystalReportViewer.Height = 1200
It takes some playing around to get your values correct but this worked and I didn't have any zooming issues.