hi i am trying to get index of row through its value . i tried this code but its not working is there any way where i can get indexof row by its value.
Dim dgr As New DataGridViewRow()
dgr.CreateCells(DataGridView1, New Object() {"name", "DFD", "SDFDF"})
DataGridView1.Rows.Add(dgr)
txtNameIndex.Text = DataGridView1.Rows.IndexOf(dgr)
i want to add value and remove value by check box when the check box is checked then the value should be added in datagridviewd and when same check box is unchecked the value should be removed from datagridview
If chkbox.Checked = True Then
row = New String() {"2", "Product 2", "2000"}
DataGridView1.Rows.Add(row)
Else
Value = New String() {"2", "Product 2", "2000"}
DataGridView1.Rows.Remove(Value)
End If
This is how you get an index:
Dim i as Integer = DataGridView1.Rows.Add(dgr)
Or
Dim i as Integer = dgr.Index
For your checkbox code I recommend the top one since you aren't creating an object for the DataGridViewRow.
Related
i have ListBox1 and ListBox2 and textbox
I want the following when setting a value in a TextBox. If the value in ListBox is1, then items are moved from the current value to the last items in not the first. to listbox2
Dim foo As String
Dim index As Integer = 0
Dim good As Integer = 0
foo = TextBox1.Text
ListBox2.Items.Clear()
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString = foo Then
index = i
End If
If i >= index Then
ListBox2.Items.Add(ListBox1.Items(good).ToString)
End If
Next
As Lars has noted in a comment, the index is 0 at the beginning so the condition i >= index wil be always true. So you need to intialize it with Int32.MaxValue.
You add always the first item, because good is always 0. You should use the loop variable i:
ListBox2.Items.Add(ListBox1.Items(i).ToString())
Here is a LINQ version which simplifies the whole code, you don't need more:
ListBox2.Items.Clear()
Dim allAfter = ListBox1.Items.Cast(Of Object).SkipWhile(Function(item) Not TextBox1.Text.Equals(item))
ListBox2.Items.AddRange(allAfter.ToArray())
I'm trying to convert the first column from a Data Grid View to a list of string, then search through the list and highlight the corresponding row in red.
This is what I currently have
Dim search As String = txtsearch.Text
Dim lsttitlesort As New List(Of String)()
For i = 0 To dgvbooks.Rows.Count - 1
lsttitlesort.Add(dgvbooks(0, i).Value)
Next
For i As Integer = 1 To lsttitle.Count - 1
If lsttitlesort(i).ToLower.Contains(search.ToLower) Then
dgvbooks.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Next
The problem is that when I try to run it, it crashes and displays an error message saying
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I'm new to Data Grid Views: can you explain me how to fix it?
You can search directly on the DataGridView without using a list:
Dim search As String = txtsearch.Text
'iterate through all rows of the DataGridView to search the value.
For Each dgrBook As DataGridViewRow In dgvbooks.Rows
'you can skip the new row at the end (if available).
If dgrBook.IsNewRow Then
Continue For
End If
'check if first column contains the search value.
'if search value found on first column set the background color to red,
'otherwise reset the background to white.
If CStr(dgrBook.Cells(0).Value & "").ToLower.Contains(search.ToLower) Then
dgrBook.DefaultCellStyle.BackColor = Color.Red
Else
dgrBook.DefaultCellStyle.BackColor = Color.White
End If
Next
Protected Sub createTable()
Try
Dim listB As New ArrayList()
Dim dt As New DataTable
Dim info As SessioneUtente = Session(SessionVariables.InformazioniUtente)
Dim id As String = info.getIdDisciplinaDefault.ToString
dt = operations.G_Number(id)
If dt.Rows.Count = 0 Then
notifica.Text = "There is no number for this id"
Else
For Each row In dt.Rows
list.Add(row(0))
listB.Add(row(0))
Next
listB.Add(23)
listB.Add(24)
listB.Add(25)
Dim cell1 As New TableCell
Dim cell2 As New TableCell
Dim Nr As New Label
Dim Icon As New Label
Dim row1 As New TableRow
Dim row2 As New TableRow
For Each item In listB
If list.Contains(item) Then
Nr.Text = item
Icon.Text = "<img = src='../Images/Icon1.png' />"
cell1.Controls.Add(Nr)
cell2.Controls.Add(Icon)
row1.Cells.Add(cell1)
row2.Cells.Add(cell2)
Else
Nr.Text = item & vbLf
Icon.Text = "<img = src='../Images/Icon2.png' />"
cell1.Controls.Add(Nr)
cell2.Controls.Add(Icon)
row1.Cells.Add(cell1)
row2.Cells.Add(cell2)
End If
Next
tabel.Rows.Add(row1)
tabel.Rows.Add(row2)
notifica.Visible = False
End If
Catch ex As Exception
Dim log As New Log
log.writeLog("default", ex)
End Try
End Sub
I'm dynamically populating a table via VB.NET using an arraylist but the table gets only the last element of the list. The list takes some elements from database and the other I add by myself. I need some help about displaying all the element of the list in the dynamic table.
Thank you in advance!
I would try moving my variables that I was dimensioning inside of my foreach loop. It appears to me that you are overwriting them each time. You seem to be overwriting the same memory location on each loop. Moving them inside of the loop should create a new memory location for each item in the list. I am guessing that you are seeing the correct number of rows but they all have the same values which happen to be the last item in the list. This is because it is being overwritten and there are only pointers to that memory location being added on each loop. You also need to move where you are adding it into the table inside of the loop for each iteration because right now you are only adding the last thing you set the row to and when you move those variables into the loop it is going to throw you an error. You want to add each row into the table while it is in scope with the current data.
I'm trying to mix a column on a datagridview with 2 control types (Checkbox & TextBox), the data is coming from a Stored Procedure that I'm also writing so I have a little flexibility.
in the stored procedure I'm returning a blank column to act as a Selection column in the GridView, but I am encountering problems when trying to convert cells to the other type based on criteria......
I keep the problem is with the datatypes when converting between the control types, I have tried all sorts of different ways to convert value first, controls first, etc but nothing is working 100%.....
currently, I have the SP returning the string False in a column then in using this with the criteria to create a checkbox..... it works fine but the Value remains a string even after converting it to a Boolean, the datatype is also a Boolean on the checkbox but the value is String..... this has gone over my head now and I'm at a loss......
NewCntrl2 = New DataGridViewCheckBoxCell
NewCntrl2.Value = Convert.ToBoolean(DGV.Cells(0).Value)
NewCntrl2.ValueType = GetType(System.Boolean)
DGV.Cells(0) = NewCntrl2
this is the code converting the textbox-column cell to a checkbox cell
any ideas why the value of the checkbox is still a string ('False')...
the problem with what it's doing now is when I handle the cell click event I cannot check or uncheck the box using the Not Value technique
----EDIT----
This is the sub I'm using, it creates several different types of controls.
Public Sub posted_CreateControls()
Dim Cell_0 As String
Dim Cell_1 As String
Dim Cell_2 As String
Dim NewCntrl As DataGridViewButtonCell
Dim NewCntrl2 As DataGridViewCheckBoxCell
For Each Rw As DataGridViewRow In dgvPosted.Rows
With Rw
Cell_0 = .Cells(1).Value.ToString
Cell_1 = .Cells(2).Value.ToString
Cell_2 = .Cells(3).Value.ToString
'this column starts with a value 'False' returned by the SP,
'we don't want checkboxes on all rows and using false string was the only method
'i could find to easily do this
'if both assign and unassign id are present then we need a checkbox
Select Case Cell_0
Case String.Empty
.Cells(0).Value = String.Empty
Case Else
If Not Cell_1 = String.Empty Then
NewCntrl2 = New DataGridViewCheckBoxCell
NewCntrl2.Value = Convert.ToBoolean(.Cells(0).Value)
NewCntrl2.ValueType = GetType(System.Boolean)
.Cells(0) = NewCntrl2
Else
.Cells(0).Value = String.Empty
End If
End Select
'Create an Assign button for each row in columnindex 0(Assign) if ColumnIndex(2)(Edit) contains M, L or I
Select Case Cell_2
Case "M", "L", "I", "P"
NewCntrl = New DataGridViewButtonCell
.Cells(1) = NewCntrl
NewCntrl.Tag = Cell_0
NewCntrl.Value = "Assign"
End Select
'Create an UnAssign button in columnindex 1(UnAssign) if the value of columnindex 1(Unassign) is not empty
If Not Cell_1 = vbNullString Then
NewCntrl = New DataGridViewButtonCell
.Cells(2) = NewCntrl
NewCntrl.Tag = Cell_1
NewCntrl.Value = "UnAssign"
End If
'Create an Edit button on columnindex 2(Edit) if ColumnIndex 2(Edit) is not M, L or empty string
Select Case Cell_2
Case "M", "L", "P", vbNullString
'Do Nothing
Case Else
NewCntrl = New DataGridViewButtonCell
.Cells(3) = NewCntrl
NewCntrl.Tag = Cell_2
NewCntrl.Value = "Edit"
End Select
End With
Next
NewCntrl = Nothing
End Sub
This creates the checkboxes how I want them but the values remain as a string that is causing the cell. Click event to fail because I'm trying to set the checkbox value to true and the .value = not .value part is failing because for some reason the value remains a string but the checkboxes value type is in fact Boolean.....
Instead of returning false try returning:
`select CAST(0 as bit) as somefield`
Which will return a boolean field. I'd also recommend adding a check box column instead of adding the individual cells.
I just want to make Data Grid. Where first and second column of data grid is for storing single value and third column as combo box.
The code that i tried is
Dim productGrid As New DataGridView
ProductGrid.Columns(0).Name = "CB"
ProductGrid.Columns(1).Name = "ProductGroup"
ProductGrid.Columns(2).Name = "Product"
Dim i As Integer
With ProductGrid
If .Rows.Count = 0 Then Exit Sub
i = 1
Dim objValueItems As New DataGridViewComboBoxCell
objValueItems.Items.Add("Server")
objValueItems.Items.Add("Standalone")
objValueItems.Items.Add("Demo")
objValueItems.Items.Add("Anywhere")
ProductGrid.Item(2, i).Value = objValueItems
End With
I am getting the error on "ProductGrid.Item(2, i).Value = objValueItems" this line. Error is " Index was out of range.
Bear in mind that all the cells of a ComboBox DGV Column have the same contents, thus you are not assigning a list of items to a given cell, but to all of them (to a column). You have to include this when adding the given column. Sample code:
Dim productGrid As New DataGridView
With productGrid
.Columns.Add("CB", "CB") 'Text-type column
.Columns.Add("ProductGroup", "ProductGroup") 'Text-type column
Dim objValueItems As New DataGridViewComboBoxColumn
With objValueItems
.Name = "Product"
.Items.Add("Server")
.Items.Add("Standalone")
.Items.Add("Demo")
.Items.Add("Anywhere")
End With
.Columns.Add(objValueItems) 'ComboBox-type column
End With