I have a collection with the fields(column) like "name" and "lastname", so i would like to update those fields(column) at the same time.
I have the next code to update only one field(column).
Dim fltr = Builders(Of BsonDocument).Filter.Eq(Of String)("name", t_update.Text.Trim)
collection.UpdateMany(fltr, New BsonDocument("name", t_name.Text.Trim))
I just solved it by using the method ADD(field, value). There is more information and tutorials with C#, that is why it is not easy to do it with VB.net.
collection.UpdateMany(fltr, New BsonDocument("$set", New BsonDocument("name", t_name.Text.Trim).Add("lastname", t_lastname.Text.Trim)))r
Related
I am trying to add columns dynamically to data grid view using VB.net. But the issue is I need to check that the column name already exists. before adding it if exists I want it to be cancelled automatically. I am using another form to select the Job Nos. which will add as Datagridview Header Text when it saved. () below is the code I am using to add a column to my datagridview. Hope you understand the question. I found some nearby answers in C# unfortunately I am not able to convert those correctly as my C# coding knowledge is little weak.
Thank You!
Dim FRMP = FrmEReview.ReviewGrid.Columns
Dim NOHRS As New DataGridViewTextBoxColumn
FRMP.Add(NOHRS)
NOHRS.HeaderText = Me.Cmb_CName.Text & "-" & Me.Cmb_DName.Text
NOHRS.Width = 160
The obvious option - the one you should have been able to work out for yourself - is to simply loop through the columns and test the HeaderText of each one:
Dim headerText = $"{Cmb_CName.Text}-{Cmb_DName.Text}"
Dim headerTextFound = False
For Each column As DataGridViewColumn In FrmEReview.ReviewGrid.Columns
If column.HeaderText = headerText Then
headerTextFould = True
Exit For
End If
Next
If Not headerTextFound Then
'...
End If
This is basically the code equivalent of what you'd do manually, which is why you should have been able to do it for yourself, at least mostly.
The not-so-obvious solution for a beginner is to use LINQ. LINQ is basically a means to flatten loops like this, so it leads to far more succinct code:
Dim headerText = $"{Cmb_CName.Text}-{Cmb_DName.Text}"
If FrmEReview.ReviewGrid.
Columns.
Cast(Of DataGridViewColumn)().
All(Function(dgvc) dgvc.HeaderText <> headerText) Then
'...
End If
I want to get all the table headers of my Data Class from my Data Context (named dc).
I tried a lot of different things:
And so on.
I went through a lot of different (stackoverflow) pages, but I'm stuck.
Any help is appreciated.
Solved it by this code:
So my GetType DataContext, should have been ProductionDataContext, since I renamed it to that. I was confused by the errors that VS gave, and didn't look further anymore.
Dim dc As New ProductionDataContext
Dim columnNames = dc.Mapping.MappingSource.GetModel(GetType(ProductionDataContext)).GetMetaType(GetType(tblInterventies)).DataMembers
I would like to convert ForEach to LINQ. Currently I'm using these two parts
If TypeOf e.FilterPopup Is RadListFilterPopup Then
Dim ePopup As RadListFilterPopup = DirectCast(e.FilterPopup, RadListFilterPopup)
Dim childList As New List(Of Object)()
For Each row As GridViewRowInfo In Me.grdCNCFilesRad.ChildRows
Dim value = row.Cells(e.Column.Index).Value
If Not childList.Contains(value) Then
childList.Add(value)
End If
Next
Dim newPopup As New RadListFilterPopup(e.Column)
For Each item As System.Collections.ArrayList In ePopup.MenuTreeElement.DistinctListValues.Values
If Not childList.Contains(item(0)) Then
newPopup.MenuTreeElement.DistinctListValues.Remove(item(0).ToString())
End If
Next
e.FilterPopup = newPopup
End If
How can I do the same with a LINQ query?
I don't know what your variable grdCNCFilesRad is type of, but I assume it is no .NET type. But when I read ChildRows then I can be sure that this is some sort of enumeration (somewhere in it's inheritance tree must be the interface IEnumerable).
So you can include System.Linq and apply a AsQueryable() at your ChildRows.
The rest is just a little bit of Linq (Select, Where, ToList()). That's it!
Edit:
The first part should be solved by this:
Dim childList =
Me.grdCNCFilesRad.ChildRows
.AsQueryble()
.Select(Function(row) row.Cells(e.Column.Index).Value)
.Distinct()
There is no need of converting ForEach with Linq if you go for performance issues.
Your existing foreach code looks good.
Note: Don't think Linq is better compared to for-each in performance.
Very new to VB.NET so please go easy :) Im currently working on an old application and im wondering if you could help me retrieve the data more efficiently. The below code works fine but everytime a new application variable is added it will need to be amended e.g dim groupthree, dim groupfour etc etc
Dim GroupOne As New Organisation(Application("testVar1"))
Dim GroupTwo As New Organisation(Application("testVar2"))
ddlGroups.AddDropDownListItem(GroupOne.Title, GroupOne.OrganisationID)
ddlGroups.AddDropDownListItem(GroupTwo.Title, GroupTwo.OrganisationID)
GroupOne = Nothing
GroupTwo = Nothing
Basically im wondering if someone could suggest a better way to retrieve the variable data (which are in a DB table). I was thinking about using a list but so far i have had no luck trying to implement it
Many thanks
Paul
I was thinking something like the follwoing if the application variable were calle dthe same in the db but although there are two instances it only shows one in the dropdown list
Dim myList As New List(Of Organisation)
Dim Group As New Organisation(Application("testVar1"))
myList.Add(Group)
For Each Item In myList
ddlGroup.AddDropDownListItem(Group.Title, Group.OrganisationID)
Group = Nothing
Next
Cheers
Paul
I've got a BindingSource for a DataSet. I'm fairly new to this whole binding business and databases, and it took me hours to figure out how to use BindingSource to get to an item, because the Row method was not included in the autocomplete. Not to confuse anyone, here's some sample code:
Dim somePreperty As String
Dim dataSet As New MyDataSet
Dim table As New MyDataSetTableAdapters.MyTableAdapter
Dim source As New BindingSource
source.DataSource = dataSet
source.DataMember = "SomeMember"
table.Fill(dataSet.SomeMember)
lablCabinet.DataBindings.Add("Text", source(0), "MemberID") '<This works fine>'
someProperty = source.Item(0).Row("ProductModel") '<So does this>'
The code runs perfectly and does exactly what I want. My problem is the following: When I've typed in source.Item(0)., autocomplete does not display Row in the list. Is this perhaps not the best way to do what I'm doing? Is there a reason it's hidden? Is this a good coding practice to do so? The fact that is wasn't there took me lots of time Googling, so I'm trying to figure out whether it's a Visual Studio glitch or my own.
Thanks in advance! = )
source.item(0) returns an object, so intellisense has no idea what is is.
You know what it should be, the compiler does not.
If you cast it first to a table or assing it to a table, intellisense will kick in.
So either:
ctype(source.item(0),datatable)
Or
dim tbl as datatable=source.item(0).