Get RepositoryItem (As Control) From Devexpress-GridviewCell - vb.net

I'm using the devexpress Gridcontrol/Gridview which has 4 columns
Name: String
Description: String
Action: RepositoryItemLookUpEdit
Info: RepositoryItemHyperLinkEdit
Right now i want to write a function which updates the Action-column but only if the Value is contained in the datasource of the RepositoryItemLookUpEdit
So i started writing the code and this is how i far i got:
For i As Integer = 0 To GridViewDD.RowCount - 1
Dim j As Integer = i
Dim rItemlookup As RepositoryItemLookUpEdit = CType(GridViewDD.GetRow(i), DataRowView).Item("Actions")
If CType(rItemlookup.DataSource, List(Of String)).Contains(curraction) Then
// Do update of the datasource here (which works)
End If
Next
GridControlDD.RefreshDataSource()
My problem lies at the line:
Dim rItemlookup As RepositoryItemLookUpEdit = CType(GridViewDD.GetRow(i), DataRowView).Item("Actions")
Question:
How can i get the RepositoryItemLookUpEdit of a cell in devexpress (or its datasource)?
Note:
The datasource of my gridview (GridViewDD) is a List And my datasource of the RepositoryItemLookUpEdit in Action is always a List(Of String)
Note 2:
The contents of my datasource may vary from row to row

You can easily get your RepositoryItem from GridColumn.ColumnEdit property.
Here is example:
Dim rItemlookup As RepositoryItemLookUpEdit = GridViewDD.Columns("Action").ColumnEdit
'...
For i As Integer = 0 To GridViewDD.RowCount - 1
Dim j As Integer = i
If CType(rItemlookup.DataSource, List(Of String)).Contains(curraction) Then
'... Do update of the datasource here (which works)
End If
Next
GridControlDD.RefreshDataSource()

I've found it. thanks to 'many' hours of browsing the devexpress-forums. Using Gridviewinfo and GridDataRowInfo you can easily access the controls 'hidden' inside the grid
In my case the code looks as following
Dim gvInfo As GridViewInfo = GridViewDD.GetViewInfo()
Dim rInfo As GridDataRowInfo = gvInfo.RowsInfo.FindRow(i)
Dim rItemlookup As RepositoryItemLookUpEdit = rInfo.Cells(GridViewDragDrop.Columns.Item("Actions")).Editor
you can now use rItemlookup to change or access its properties.
I hope this may be of some use to anyone.

Related

Form control change to runtime object VB.net

I have a form what contains about 10-15 CheckedListBox objects on a Panel named PanelFilterBoxes.
Each CheckedListBox can contains lots of items (1 to max ~2000).
I have to reload regulary the items of this controls based upon a datatable's column unique values (plus some extras).
Now I have a code like this (this below is just a shorten extract):
For c = 0 To PanelFilterBoxes.Controls.Count - 1
'--get unique datas from datatable
Dim dtUnique() As String = ....
'--change items of the control
Dim contr As CheckedListBox = PanelFilterBoxes.Controls.Item(c)
contr.Items.Clear()
contr.Items.Add("*All", True)
contr.Items.Add("*Search: ", False)
For Each myValue As String In dtUnique
'--add items with checked true
contr.Items.Add(myValue, True)
Next
Next
It is not so long, but takes some times. I'd like to try what if I create a runtime object (I can put data in a runtime created object faster than a form object), and "replace" the control on the form with this?
So my "big challenge" how can I do this?
I tried this ways, but not works :/
For c = 0 To PanelFilterBoxes.Controls.Count - 1
'--get unique datas from datatable
Dim dtUnique() As String = ....
'--change items of the control
Dim contr As CheckedListBox = PanelFilterBoxes.Controls.Item(c)
'--runtime temporary object
Dim tempContr As new CheckedListBox
contr.Items.Clear()
tempContr .Items.Add("*All", True)
tempContr .Items.Add("*Search: ", False)
For Each myValue As String In dtUnique
'--add items with checked true
tempContr .Items.Add(myValue, True)
Next
'--1 - this doesn't works, but doesn't produce any error
contr=tempContr
'--2 - it works, but the items aren't checked
contr.Items.AddRange(tempContr.Items)
Next
Anybody has a solution for this?
Thank you

Listview - add File type & Last modified Subitems

I'm trying to add "file type" and "last modified" to my Listview when adding items in It same as in Explorer, but I don't find what property should be assigned to SubItem. Here is my code:
For Each MyFile As IO.FileInfo In ItemDirectory.GetFiles
Dim lvi As New ListViewItem
lvi.Tag = mFile.FullName
lvi.Text = mFile.Name
lvi.ImageKey = CacheShellIcon(mFile.FullName)
Listview1.Items.Add(lvi)
lvi.SubItems.Add("File type ??")
lvi.SubItems.Add(mFile.LastAccessTime.ToShortDateString & " " & mFile.LastAccessTime.ToShortTimeString) 'This isn't same as last modified ?
Next
If somebody knows how to do It please let me know, I want to have this in my Details view.
The linked answer provides an all-purpose way to get all the extended properties. With 300+ elements in newer Windows versions it is clearly overkill to fetch them all if you are only interested in one or two. This returns just the file type. A better approach might be to pass a "shopping list" of desired property names.
As before, you need to add a reference to Microsoft Shell Controls and Automation or Microsoft Shell Folder View Router based on your OS version.
Imports Shell32
Imports SHDocVw
Partial Friend Class Shell32Methods
Friend Shared Function GetShellFileProperty(filepath As String, index As Int32) As String
Dim shell As New Shell32.Shell
Dim shFolder As Shell32.Folder
shFolder = shell.NameSpace(Path.GetDirectoryName(filepath))
' get shell data for this file, cast to folder item
Dim shFolderItem = DirectCast(shFolder.Items().Item(Path.GetFileName(filepath)),
Shell32.ShellFolderItem)
If shFolderItem IsNot Nothing Then
Return shFolder.GetDetailsOf(shFolderItem, index)
Else
Return String.Empty
End If
End Function
...
End Class
Usage:
Dim lvi As ListViewItem
Dim fileType As String
For Each f As String In Directory.EnumerateFiles("C:\Temp\ShellTest")
fileType = Shell32Methods.GetShellFileProperty(f, 9)
lvi = New ListViewItem
lvi.Text = Path.GetFileName(f)
lvi.SubItems.Add(fileType)
lvFiles.Items.Add(lvi)
Next
Ideally, you'd want to create an Enum for the properties so the code could avoid magic numbers:
fileType = Shell32Methods.GetShellFileProperty(f, Shell32FileProps.FileType)
As noted elsewhere, the index of the ones >260 or so can change depending on the OS version. That could be easily modified to accept an Enum/Int array and return a list of values so as to prevent iterating all 300+ propertied to get one or three.
For filetype you can use lvi.SubItems.Add(MyFile.Extension)
and for the "last modified" date, of course the last modified! :D
lvi.SubItems.Add(MyFile.LastWriteTime.ToShortDateString)
Last write and last access are not the same ;)
I figured out another solution, I think this one is easier, at least for me :
Public Function ExProperty(filepath As String, PropertyItem As Integer)
Dim arrHeaders As New List(Of String)()
Dim shell As New Shell
Dim rFolder As Folder = shell.[NameSpace](Path.GetDirectoryName(filepath))
Dim rFiles As FolderItem = rFolder.ParseName(Path.GetFileName(filepath))
'I needed only File type so I looped to 2 only (2 is the file type in my case - Windows 10 -
' to see all available properties do a loop
' 0 To Short.MaxValue - 1" and then extract whatever property you like)
For i As Integer = 0 To 2
Dim value As String = rFolder.GetDetailsOf(rFiles, i).Trim()
arrHeaders.Add(value)
Next
Dim DesiredProperty As String
DesiredProperty = arrHeaders.Item(PropertyItem)
Return DesiredProperty
End Function
Usage with Listview just simply (this adds File type subitem):
Listview1_Item.SubItems.Add(ExProperty(filepath, 2))
As in all solutions, a reference to Microsoft Shell Controls and Automation must be set.

Datagridview to report viewer using parameter VB.net

I try to copy all columns in my datagridview (DGView7) to my report viewer.
I used this code, but it didn't work
Dim myAL As New ArrayList()
Dim row As List(Of String)
For i As Integer = 0 To Basicfor.DGView7.RowCount() - 1
row = New List(Of String)
For j As Integer = 0 To Basicfor.DGView7.ColumnCount() - 1
row.Add(Basicfor.DGView7.Rows(i).Cells(j).ToString)
Next j
myAL.Add(row)
Next i
Dim Params(0) As ReportParameter
Params(0) = New ReportParameter("Ref", CType(myAL.ToArray(GetType(String)), String()))
Me.ReportViewer1.RefreshReport()
It shows an error. I tried to change to this code string() -> Single(), but single is not defined
Params(0) = New ReportParameter("Ref", CType(myAL.ToArray(GetType(String)), Single()))
Can anyone resolve this? Thanks in advance
You're calling
Params(0) = New ReportParameter("Ref", CType(myAL.ToArray(GetType(String)), String()))
And wanting to pass in an array of strings(). That's my assumption at least
However you're passing an array of list of strings.
Try
Dim myStrings = myAL.Cast(Of List(Of String)).SelectMany(Function(r) r).ToArray()

Access a form's control by name

not sure whether the title of this post is accurate.
I'm trying to access windows form controls and their properties by "composing" their name within a loop, but I can't seem to find the related documentation. Using VB.net. Basically, say I have the following:
Dim myDt As New DataTable
Dim row As DataRow = myDt.NewRow()
row.Item("col01") = Me.label01.Text
row.Item("col02") = Me.label02.Text
'...
row.Item("colN") = Me.labelN.Text
I'd like to write a for loop instead of N separate instructions.
While it's simple enough to express the left-hand side of the assignments, I'm stumped when it comes to the right-hand side:
For i As Integer = 1 to N
row.Item(String.format("col{0:00}", i)) = ???
' ??? <- write "label" & i (zero-padded, like col) and use that string to access Me's control that has such name
Next
As an extra, I'd like to be able to pass the final ".Text" property as a string as well, for in some cases I need the value of the "Text" property, in other cases the value of the "Value" property; generally speaking, the property I'm interested in might be a function of i.
Cheers.
You could use the ControlsCollection.Find method with the searchAllChildren option set to true
For i As Integer = 1 to N
Dim ctrl = Me.Controls.Find(string.Format("label{0:00}", i), True)
if ctrl IsNot Nothing AndAlso ctrl.Length > 0 Then
row.Item(String.format("col{0:00}", i)) = ctrl(0).Text
End If
Next
An example on how to approach the problem using reflection to set a property that you identify using a string
Dim myLabel As Label = new Label()
Dim prop as PropertyInfo = myLabel.GetType().GetProperty("Text")
prop.SetValue(myLabel, "A Label.Text set with Reflection classes", Nothing)
Dim newText = prop.GetValue(myLabel)
Console.WriteLine(newText)

Adding a datarow by dynamically reading a text file

I have a program that reads a text file and creates a datatable dynamically based on the columns in the file.
This seems to work fine.
I then try to read the rest of the text file, creating a data row to add to my table. I'm using the code below but it's not working for me.
view plaincopy to clipboardprint?
Dim strFieldData As New ArrayList
Dim obj As New Object()
'this code give me 12 separate elements
strFieldData = SplitDelimitedLine(strLineOfFile, ",", "|")
Dim dr As DataRow
For i As Integer = 0 To strFieldData.Count - 1
dr = DataForSQLTable.NewRow()
dr.ItemArray(i) = strFieldData.Item(i)
Next
'however when I look at the value of dr.itemarray I see System.DBNull for all the fields
DataForSQLTable.Rows.Add(dr)
I'm obviously missing the trick. Can anyone help me please?
It looks like you're only adding the last item. You need to move the addition line inside the for loop.
For i As Integer = 0 To strFieldData.Count - 1
Dim dr as DataRow = DataForSQLTable.NewRow()
dr.ItemArray(i) = strFieldData.Item(i)
DataForSQLTable.Rows.Add(dr)
Next