Autosize Rows for Datagridview in VB.net not working - vb.net

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:

Related

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.

Control name from Variable or Dataset. (Combobox)(.items.add)(.datasource)

I've checked for hours but I can't seem to find anything to help.
I want to loop through tables and columns from a dataset and use the column name in a combobox.items.add() line, however the eventual goal is to fill the combobox from the dataset itself possibly in a combobox.datasource line.
The first problem is that I can't get the code correct to setup the combobox control where it allows me to use .items.add("") and in extension .datasource
Error Message = "Object reference not set to an instance of an object"
dstcopt is the dataset from a oledbDataAdapter .fill(dstcopt,"table") line (which returns correct values)
tc_opt is a tab name on a tab control where the comboboxes are
For Each dstable In dstcopt.Tables
For Each dscolumn In dstable.Columns
Dim colName As String = dscolumn.ToString
MsgBox(colName) 'This retuns "aantigen"
Dim cb As ComboBox = Me.tc_opt.Controls("cb_" & colName)
cb.Items.Add(colName)
'cb_aantigen.DataSource = dstcopt.Tables(dstable.ToString)
'cb_aantigen.DisplayMember = "aantigen"
'cb_atarget.DataSource = dstcopt.Tables(dstable.ToString)
'cb_atarget.DisplayMember = "atarget"
Next
Next
The second problem comes when I do it manually (which works) using the exact combobox names cb_aantigen and cb_atarget as seen in the comments.
The problem is that once the form is loaded and the cb's are filled with the correct values, I can't change the value in any single cb individually, when I change one value it changes them all (there is 15 comboboxes in total) I know this is down to using a dataset, but I don't know away to 'unlink them from each other or the dataset'
Not sure if I need to split this into 2 questions, but help on either problem would be appreciated.
EDIT:
After looking at only this section of code for a day. This is what I have come up with to tackle both the problems at once.
The combobox control not working was down to using a tab tc_opt instead of a groupbox gp_anti
The issue with splitting the dataset up into individual comboboxes, I've worked around by taking the value of each cell in the database and adding it separately, probably a better way to do it though
For Each dstable As DataTable In dstcopt.Tables
For Each dscolumn As DataColumn In dstable.Columns
Dim colName As String = dscolumn.ToString
Dim cb(2) As ComboBox
cb(0) = CType(Me.gp_anti.Controls("cb_" & colName), ComboBox)
cb(1) = CType(Me.gp_rec.Controls("cb_" & colName), ComboBox)
cb(2) = CType(Me.gp_nat.Controls("cb_" & colName), ComboBox)
For icb = 0 To cb.Count - 1
If Not (IsNothing(cb(icb))) Then
For irow = 0 To dstable.Rows.Count - 1
If dstable.Rows(irow)(colName).ToString <> Nothing Then
Dim icbitemdupe As Boolean = False
If cb(icb).Items.Contains(dstable.Rows(irow)(colName).ToString) Then
icbitemdupe = True
End If
If icbitemdupe = False Then
cb(icb).Items.Add(dstable.Rows(irow)(colName).ToString)
End If
End If
Next
End If
Next
Next
Next

how to add items in combox dynamically in tablelayoutpanel in vb.net

i have comboboxes dynamically created in a tablelayoutpanel and i want to add items to that combobox dynamically( i dont want to use data sources).
here is my code
Dim cmb(maxx)
For a = 0 To maxx - 1
TableLayoutPanel1.Height += 31
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, 30))
TableLayoutPanel1.RowCount += 1
TableLayoutPanel1.Controls.Add(New CheckBox With {.Text = CopyaillongoDataSet.inventory.Rows(a).Item(1).ToString, .Font = New System.Drawing.Font("Microsoft Sans Serif", 10)}, 0, TableLayoutPanel1.RowCount - 1)
TableLayoutPanel1.Controls.Add(New ComboBox With {.Name = cmb(a), .MaxDropDownItems = 10, .DropDownStyle = ComboBoxStyle.DropDownList}, 1, TableLayoutPanel1.RowCount - 1)
cmb(a).items.add(a)
Next
i assigned a name to a combobox and tried to use that name to add items but were unsucessful. is it possible to create items dynamically with a combobox dynamically created? if so, can you help me? i am working on my thesis and i need to get rid of this problem. thank you for those who can help.
You can't reference a control by just a string name the way you are doing it in your code (you didn't post what you are doing between Dim cmd(maxx) and the loop, but I'm guessing you want to do something like this, where you create a new ComboBox control and add it to the Panel:
Dim newCombo As New ComboBox With {.MaxDropDownItems = 10,
.DropDownStyle = ComboBoxStyle.DropDownList}
newCombo.Items.Add(a)
TableLayoutPanel1.Controls.Add(newCombo, 1, TableLayoutPanel1.RowCount - 1)
You can reference the control later with TableLayoutPanel1.GetControlFromPosition(column, row) function.

Listview help needed

I am confused being a relatively new user to vb.net. Why my listview is not amending the value in the list? If I may, so I have a correct working of how listview displays its data from database. I have a general question in addition to my code problem.
I have five columns in my listview (0-4). Am I correct in saying that if my access database contained say 10 fields but I only needed to display five of them but one of them was field (9), then would code the list like my code below, which is not changing the value and will only display the list if I remove the 'else' statement.
What is the error? Many thanks
UPDATED CODE:
oledbCnn.ConnectionString = My.Settings.storageConnectionString
oledbCnn.Open()
'drcount = Convert.ToInt32(dr("RowCount"))
sql = "Select TOP 100 * from Requests ORDER BY [Date-time received] DESC"
Debug.Print(sql)
Dim oledbCmd As OleDbCommand = New OleDbCommand(sql, oledbCnn)
Using dr = oledbCmd.ExecuteReader()
'clear items in the list before populating with new values
'lvRequests.Items.Clear()
While dr.Read()
If dr.HasRows Then
Dim LVI As New ListViewItem
With LVI
.Text = dr(0).ToString()
.UseItemStyleForSubItems = False
.SubItems.Add(CDate(dr(5)).ToShortDateString())
.SubItems.Add(dr(1).ToString())
.SubItems.Add(dr(3).ToString())
If dr(3).ToString = "D" Then
.SubItems(3).Text = "Destroyed"
ElseIf dr(3).ToString = "O" Then
.SubItems(3).Text = "Out"
ElseIf dr(3).ToString = "I" Then
.SubItems(3).Text = "Intake"
End If
.SubItems.Add(dr(9).ToString())
If dr(9).ToString = "DEMO" Then
.SubItems(9).Text = "Done"
End If
End With
lvRequests.Items.Add(LVI)
lvcount += 1
End If
End While
End Using
There is not enough info to be really sure what is going on. It appears that you pull 10 columns from the DB, but if you only post 5 of them to the LV, then there should only be 5 subitems. So this is wrong:
If dr(9).ToString() = "DEMO" Then
lvRequests.Items(lvRequests.Items.Count - 1).SubItems(9).Text = "Done"
' should probably be:
If dr(9).ToString() = "DEMO" Then
lvRequests.Items(lvRequests.Items.Count - 1).SubItems(4).Text = "Done"
What might make it clearer at least in code would be to use Names for the subitems. If you instance a SubItem object rather than use a default constructor, you can assign a name and reference them that way instead:
Dim si As New ListViewItem.ListViewSubItem
With si
.Text = dr(X).ToString ' dunno whats in there
' this is probably not exactly right, but give the SubItem
' the same name as the db column
.Name = dr.Table.Columns(X).ColumnName
End With
thisItem.SubItems.Add(si) ' add sub to Item collection
Now, your code can use the name rather than index:
If dr(9).ToString() = "DEMO" Then
lvReq.Items(lvReq.Items.Count - 1).SubItems("TheColumnName").Text = "Done"
it no longer really matters how many columns or subitems there are. The ListViewItem.SubItems collection does not require sub item names to be unique, so it is possible to end up with 2 with the same name, but if you map from the DR/DT/DB correctly, it should take care of itself.
If the LV is bound to the datasource (you did not say), then there could be as many SubItems as db/datasource columns (never actually used an LV that way).

Changing DataGridView Column Properties in VB Code

So I'm adding columns in the code, rather than design view...
frmMain.dgv_test.Columns.Add("col1", "1")
frmMain.dgv_test.Columns.Add("col2", "2")
'etc
How do I edit properties such as Column Width, Frozen, and all the other properties that can be seen in the design view if I were to "design" a column?
Thank you.
Create a new Temp DataGridColumn then set all the properties you want for that column and then add it to the grid.
Dim tempC as new DataGridColumn()
tempC.HeaderText ="col1"
tempC.HeaderStyle.whatever
etc....
...then
frmMain.dgv_test.Collumns.Add(tempC)
http://msdn.microsoft.com/en-us/library/2wfbzezz%28v=VS.100%29.aspx
The DataGridViewColumnCollection.Add method actually returns the index of the added DataGridViewColumn, so you can also do this:
Dim colIndex As Integer = frmMain.dgv_test.Columns.Add("col1", "1")
Dim col As DataGridViewColumn = frmMain.dgv_test.Columns(colIndex)
col.Width = 100
col.Frozen = True
Or here's another, less verbose way:
With frmMain.dgv_test.Columns
Dim col As DataGridViewColumn = .Item(.Add("col1", "1"))
col.Width = 100
col.Frozen = True
End With
And so on.