Resetting Datagridview combo box data at runtime - vb.net

I am creating grid on form load event.
Initially I am setting some values in datagridview combobox as :
Dim dgvc As DataGridViewComboBoxCell
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn1.Text \\setting selected item
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn2.Text
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" ")) \\adding blank
dgvc.Items.Add(txtColumn1.Text) \\then required value
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" "))
dgvc.Items.Add(txtColumn2.Text)
Now when user clicks on particular combobox.I am setting new values in it as :
// Resetting old values
If IsDBNull(dgvc) = False Then
dgvc.DataSource = Nothing
dgvc.Items.Clear()
End If
If DtTable.Rows.Count > 0 Then
Dim k As Integer
Dim dgvc1 As DataGridViewComboBoxCell
dgvc1 = New DataGridViewComboBoxCell()
For k = 0 To DtTable.Rows.Count - 1
If DtItemCd.Rows(k)("ItemCd").ToString <> Current_Code Then
datagrigview1.Rows(e.RowIndex).Cells("Column1").Value = DtTable.Rows(k)("Column1").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column1")
dgvc1.Items.Add(DtTable.Rows(k)("Column1").ToString)
datagrigview1.Rows(e.RowIndex).Cells("Column2").Value = DtTable.Rows(k)("Column2").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column2")
dgvc1.Items.Add(DtTable.Rows(k)("Column2").ToString)
End If
Next
End If
This shows both old and new records.Please help.

Probably your code here
If IsDBNull(dgvc) = False Then
is testing if the DataGridViewComboBoxCell is Null not if it's DataSource is null.
Hence it never enters the condional code below.
Could you try to change in this way and see if now you enter in the if condition?
If IsDBNull(dgvc.DataSource) = False Then

I got below solution :
If IsDBNull(dgvc) = False Then
dgvc.Items.Clear()
dgvc.DataSource = Nothing
dgvc = dgvSO.Rows(e.RowIndex).Cells("Column1")
dgvc.Items.Remove(" ")
dgvc.Items.Remove(Current_Code)
End If

This works
cb.SelectedItem = Nothing

Related

VB.NET: DataGridView different button in same Column

I am tring to initialize a checker that checks the cells of a certain column in datagridview, if the cell is equal to StaffMixname then the button text should be VIEW if not then the button text is LOCKED
here is my code so far.
Dim dgButtonColumn As New DataGridViewButtonColumn
Dim i As Integer
MetroGrid7.Columns.Add(dgButtonColumn)
dgButtonColumn.HeaderText = "Security"
dgButtonColumn.UseColumnTextForButtonValue = True
For i = 0 To MetroGrid7.Rows.Count
If MetroGrid7.Rows(i).Cells.Item(4).Value.ToString() = StaffMixname.Text Then
dgButtonColumn.Text = "VIEW"
dgButtonColumn.Name = "viewBtn"
dgButtonColumn.ToolTipText = "View"
Else
dgButtonColumn.Text = "LOCKED"
dgButtonColumn.Name = "searchSecurityBtn"
dgButtonColumn.ToolTipText = "LOCKED"
End If
Next
My desired result is like this
It doesn't work like this. In DataGridViewButtonColumn each cell contains a button but you can not access it. You can get the DataGridViewButtonCell and change these two properties value and ToolTipText. There is no name property in DataGridViewButtonCell. So to change them:
Dim dgButtonColumn As New DataGridViewButtonColumn
Dim i As Integer
MetroGrid7.Columns.Add(dgButtonColumn)
dgButtonColumn.HeaderText = "Security"
'remove this line
'dgButtonColumn.UseColumnTextForButtonValue = True
For i = 0 To MetroGrid7.Rows.Count
If MetroGrid7.Rows(i).Cells.Item(4).Value.ToString() = StaffMixname.Text Then
MetroGrid7.Rows(i).Cells.Item(6).Value = "VIEW"
MetroGrid7.Rows(i).Cells.Item(6).ToolTipText = "View"
Else
MetroGrid7.Rows(i).Cells.Item(6).Value = "LOCKED"
MetroGrid7.Rows(i).Cells.Item(6).ToolTipText = "LOCKED"
End If
Next
Dim status As String
status = status & Me.dgTitleList.CurrentRow.Cells("BDO").Value
status = status & Chr(13)
Insert the Value of the Datagridview Column to String and do the condition.
If status = StaffMixname.Text Then
dgButtonColumn.Text = "VIEW"
dgButtonColumn.Name = "viewBtn"
dgButtonColumn.ToolTipText = "View"
Else
dgButtonColumn.Text = "LOCKED"
dgButtonColumn.Name = "searchSecurityBtn"
dgButtonColumn.ToolTipText = "LOCKED"
End If
And I suggest is to use BreakPoint and check if the StaffMixName.Text has a Value.

Datagridview fills on second attempt. Never first attempt in Tab Control

I have a tab list where each tab has its own "x" amount of datagridviews. For some reason the first active tab fills the datagridviews just fine. When I click on the second tab in the Tab Control, the datagridviews fill the data but the gridview shows empty [When I followed the code it showed the records being filled into the datagridview].
If I click to another tab and then back to the second tab the gridview refills again and then shows the data. Does anyone know what may cause this? It is always the first attempt that never initially fills the other tabs [aside the first active tab]
So from what I can tell... nested Datagridviews can't be dynamically filled at first load using myDataGrid.Rows.Add(.....). The only way I was able to do this on first load was to build a Datatable and fill that instead. Can't understand why but it works....
With myDataGrid
Dim f As Font = .Font
.Columns.Clear()
.DataSource = Nothing
.DataMember = Nothing
.ColumnCount = 3
.AutoGenerateColumns = False
'.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells
.Columns(0).Name = "MsgID"
.Columns(0).HeaderText = "MsgID"
.Columns(0).DataPropertyName = "MsgID"
.Columns(0).Visible = False
.Columns(1).Name = "Message"
.Columns(1).HeaderText = "Message"
.Columns(1).DataPropertyName = "Message"
.Columns(2).Name = "Sig"
.Columns(2).HeaderText = "Sig"
.Columns(2).DataPropertyName = "Sig"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.Font = New Font(f, FontStyle.Bold)
.DefaultCellStyle.Alignment = 1
.Refresh()
End With
Dim dt As New DataTable
dt.Columns.Add("MsgID")
dt.Columns.Add("Message")
dt.Columns.Add("Sig")
dt.Columns(0).AutoIncrement = True
With ArrMessages(x)
Dim R As DataRow = dt.NewRow
R(0) = .MsgID
R(1) = .Msgs
R(2) = .Sig
dt.Rows.Add(R)
End With
'assign sourcing
myDataGrid.DataSource = dt
myDataGrid.Refresh()
Me.myDataGrid.Parent.Refresh()
myDataGrid.Refresh()

vb.net create empty column datagridview on page load

hi i want to define a column on datagridview on page_load events
how do i achieve this?
i've tried the code below
dgv_beli.AutoGenerateColumns = False
dgv_beli.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
dgv_beli.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'DEFINE COLUMN MANUALLY
'kode barang
Dim colKode As New DataGridViewTextBoxColumn
colKode.HeaderText = "Kode"
colKode.Name = "Kode"
'nama barang
Dim colNama As New DataGridViewTextBoxColumn
colNama.HeaderText = "Nama"
colNama.Name = "Nama"
'jenis
Dim colJenis As New DataGridViewTextBoxColumn
colJenis.HeaderText = "Jenis"
colJenis.Name = "kd_jenis"
colJenis.Width = 120
dgv_beli.AllowUserToAddRows = True
dgv_beli.Columns.Add(colKode)
colKode.ReadOnly = True
dgv_beli.Columns.Add(colNama)
colNama.ReadOnly = True
dgv_beli.Columns.Add(colJenis)
colJenis.ReadOnly = True
dgv_beli.Sort(dgv_beli.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
dgv_beli.Rows.Add(colKode, colNama, colJenis)
but the datagridview stll empty
please, help me to fix this issue.. thanks before

Setting ReadOnly attribute to all Textboxes in Array of Controls

I have the following code looping through a variety of arrayed controls in a form:
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
If ctrlArray(r).ReadOnly = False Then
ctrlArray(r).ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next
I receive the error "'ReadOnly' is not a member of System.Windows.Forms.Control" when trying to set textboxes as read only.
Solved this right before I hit the submit button. Thought I would share anyway:
Dim tbx As TextBox
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
tbx = ctrlArray(r)
If tbx.ReadOnly = False Then
tbx.ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next

How to concat variable integer in control name in vb.net

Now I have a database and pull out that data and display it to form,i have a sequence of groupbox and radiobuttons, in each groupbox (groupbox1,groupbox2,etc...) there are 2 radio buttons namely rdbtn1Yes and rdbtn1No (then it increment +1 in next Groupbox). now i use for loop to go through every groupboxes and radio buttons. And this is my code:
Dim sqlda As New SqlDataAdapter("SELECT * FROM table1 WHERE column1= '" & lblWONo.Text & "'", Constr)
Dim sqlds As New DataSet
sqlds.Clear()
sqlda.Fill(sqlds)
If sqlds.Tables(0).Rows.Count > 0 Then
With sqlds.Tables(0).DefaultView.Item(0)
txtDateCreated.Value = .Item(0).ToString
txtComments.Text = .Item(1).ToString
'check column if it contain FALSE/TRUE value
'then toggle the radiobutton state to TRUE
'In this part i know there is another/easiest way to checked radio buttons to TRUE value
'and this is my code using looping (below):
If .Item(2) = False Then
rdbtn1No.Checked = True
Else
rdbtn1Yes.Checked = True
End If
If .Item(3) = False Then
rdbtn2No.Checked = True
Else
rdbtn2Yes.Checked = True
End If
If .Item(4) = False Then
opt3N.Checked = True
Else
opt3Y.Checked = True
End If
End With
End If
SAMPLE CODE FOR LOOPING:
Dim itemNo As Integer
Dim rdbtnSet As Integer = 1
Dim grpboxCnt As Integer = 1
For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
For itemNo = 2 To sqlds.Tables(0).Columns.Count
If .Item(itemNo) = True Then
rdbtn & rdbtnSet & "Yes".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
Else
rdbtn & rdbtnSet & "No".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
End If
Next
rdbtnSet += 1
grpboxCnt += 1
Next
Thats all. Thank you in advance!
Think about the use of a dictionary (id, control) to store your controls. Then iterate the dictionary and set your state.