When I bind to the GridControl I want to get rid of columns showing something like:
System.Data.Objects.DataClasses.EntityCollection``1[Model.PersonEmails]
Instead I would like in this case to get the total number of emails based on the parent entity.
Sorry for the delay
I've already found a solution using the GridView1_CustomColumnDisplayText event handler.
Yes, it's third party control.
Private Sub GridView1_CustomColumnDisplayText(sender As Object, e As DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs) Handles GridView1.CustomColumnDisplayText
If e.Column.FieldName = "PersonEmails" Then
Dim Entities = DirectCast(e.Value, System.Data.Objects.DataClasses.EntityCollection(Of PersonEmails))
If Not Entities Is Nothing Then
If Entities.Count = 1 Then
Dim x As PersonEmail = Entities.FirstOrDefault()
e.DisplayText = x.Email
Else
e.DisplayText = Entities.Count
End If
End If
End If
End Sub
Related
I'm not an experienced VBA programmer but I've been trying to create an Excel Spreadsheet that is able to manage a basketball team.
In it I've got a primary userform where I have declared an array, 'selectedPlayers'.
This primary userform has a for loop that starts up the secondary userform 'i' times.
I have not been able to access the primary userform's 'i' and 'selectedPlayers' from the secondary one.
I've been able to find a workaround the 'i' by creating a non-visible textbox in the first userform, that I'm able to reference from the second one.
I've tried declaring both of them as public, but yet I'm not able to call upon it from the second userform.
part of the code for the first userform:
i = 0
Do While Not i = Int(txtNumberPlayers)
frmGameDataSecondary.Show
i = i + 1
Loop
second userform:
Private Sub cmdDone_Click()
frmGameData.selectedPlayers(frmGameData.i) = lbxPlayer.Value
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.lbxPlayer
For Each LR In LO.ListRows
exitSequence = False
For k = 1 To Int(frmGameData.txtNumberPlayers)
If frmGameData.selectedPlayers(k) = blablabla.Value Then
exitSequence = True
End If
Next k
If !exitSequence Then
.AddItem blablabla.Value
End If
Next LR
End With
End Sub
The main problem is that array contents are cleared after the sub is finished.
I was also messing around with this idea and there is a really good thread I started with tons of great information from various awesome people
Calling an Array Upon User Form Terminate/Close VBA
Forms in VBA are Objects, and can be treated like any other Class module. This means that you can add properties to them. If you need to pass information back from a form, all you need to do is grab a reference to it, then Hide it instead of Unload it. Treat it like a dialog and let the calling code handle it's create and destruction (I'm assuming from your code that it is modal).
Something like this:
In the first UserForm:
For i = 0 To 1
Dim second As frmGameDataSecondary
Set second = New frmGameDataSecondary
second.Show
'Execution suspends until the second form is dismissed.
selectedPlayers(i) = second.Player
Unload second
Next i
In the second UserForm:
Private mPlayer As String
'This is where your returned information goes.
Public Property Get Player() As String
Player = mPlayer
End Property
Private Sub cmdDone_Click()
mPlayer = lbxPlayer.Value
'Control passes back to the caller, but the object still exists.
Me.Hide
End Sub
You can declare properties inside of parent form which will manipulate the array from outside. The child form needs to have a reference to parent so it can call this properties. HTH
Parent form
Option Explicit
' I have not been able to access the primary userform's
' 'i' and 'selectedPlayers' from the secondary one
Private selectedPlayers As Variant
Public Function GetMyArrayValue(index) As Variant
GetMyArrayValue = selectedPlayers(index)
End Function
Public Sub SetMyArrayValue(index, newValue)
selectedPlayers(index) = newValue
End Sub
Private Sub UserForm_Click()
Dim i
i = 0
Do While Not i = Int(txtNumberPlayers)
With New secondaryUserForm
Set .ParentForm = Me
.SetIndex = i
.Show
End With
i = i + 1
Loop
End Sub
Private Sub UserForm_Initialize()
selectedPlayers = Array("A", "B", "C")
End Sub
Child form
Option Explicit
Private m_parent As primaryUserForm
Private m_index As Integer
Public Property Let SetIndex(ByVal vNewValue As Integer)
m_index = vNewValue
End Property
Public Property Set ParentForm(ByVal vNewValue As UserForm)
Set m_parent = vNewValue
End Property
Private Sub cmdDone_Click()
' frmGameData.selectedPlayers(frmGameData.i) = lbxPlayer.Value
m_parent.SetMyArrayValue m_index, "lbxPlayer.Value"
Unload Me
End Sub
In my winforms application, I loop through the cells of a datagridview and add a tooltip for specific cells based on values in other columns. I do this in the cellformatting event handler, and it worked perfectly. Here is the code:
Private Sub dgvResults_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvResults.CellFormatting
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "TradeInValue"
DirectCast(dgvResults.Rows(e.RowIndex).Cells("TradeInValue"), DataGridViewTextBoxCell).ToolTipText = "Min = " & CDec(dgvResults.Item("BB_Min", e.RowIndex).FormattedValue).ToString("$#####.##") & ", Max = " & CDec(dgvResults.Item("BB_Max", e.RowIndex).FormattedValue).ToString("$#####.##")
If Not IsNothing(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue) AndAlso dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString.Trim.Length > 0 AndAlso CInt(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString) <> -1 Then
If dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Length > 0 Then
Dim ValueParts() As String = dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Split("|")
'Dim selectedTrim As String = ValueParts(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue)
End If
End If
End Select
End Sub
Then, I added code in the cellpainting event handler to hide specific images, again based on values in the datagridview. Here is that code.
Private Sub dgvResults_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvResults.CellPainting
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "VIN_Pic"
If dgvResults.Rows(e.RowIndex).Cells("VIN_Value").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
Case "EmailDisplayImage"
If dgvResults.Rows(e.RowIndex).Cells("ListingContactEmail").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
End Select
End If
End Sub
When this code as added, the tooltips no longer display. The CellToolTipTextNeeded event fires, and it shows the correct text in the argument, but it never displays. Comment out the lines that assign a new image to the datagridviewimagecells, and the tooltips start displaying again.
I hope this explanation was sufficient. Any ideas?
With the below code, you will run into memory and GDI object leaks very easily as a new Bitmap instance is created every time the cell is painted. Also this is causing your ToolTip to be not displayed.
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
Define the empty bitmap in class level or add as embedded resource and use it.
Dim emptyBitmap As New Bitmap(1, 1);
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = emptyBitmap
I posted a question earlier but it is so complicated that I think that's the reason why nobody tries to answer it. So I decided to put this in the simplest way I can. So here is my code:
Private Sub frmcrc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = GetConnect()
Dim com As New SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader
conn.Open()
com.CommandText = "Select * from tblCRClearance where crc_no = '" & frmComm.txtCRCNum1.Text & "'"
com.Connection = conn
dr = com.ExecuteReader
dr.Read()
txtCRCNo.Text = dr!crc_no
txtCLTAcctNo.Text = dr!crc_clientno
lblrootpayee.Text = dr!crc_rootpayee
txtPayeeName.Text = dr!crc_payeename
txtClientName.Text = dr!crc_clientname
txtProjName.Text = dr!crc_projectname
txtCommType.Text = dr!crc_commtype
txtGrossAmt.Text = dr!crc_grossamount
txtWTax.Text = dr!crc_withheld
txtCALType1.Text = dr!crc_caliqtype1
txtCALDate1.Text = dr!crc_caliqdate1
txtCALNo1.Text = dr!crc_caliqnum1
txtCALAmt1.Text = dr!crc_caliqamt1
txtCALDesc1.Text = dr!crc_caliqdesc1
txtCALType2.Text = dr!crc_caliqtype2
txtCALNo2.Text = dr!crc_caliqnum2
txtCALAmt2.Text = dr!crc_caliqnum2
txtCALDate2.Text = dr!crc_caliqdate2
txtCALDesc2.Text = dr!crc_caliqdesc2
txtCALType3.Text = dr!crc_caliqtype3
txtCALDate3.Text = dr!crc_caliqdate3
txtCALNo3.Text = dr!crc_caliqnum3
txtCALAmt3.Text = dr!crc_caliqamt3
txtCALDesc3.Text = dr!crc_caliqdesc3
txtCALIQ.Text = dr!crc_caliqtotal
txtNetAmt.Text = dr!crc_netamount
txtVoucherNum.Text = dr!crc_voucherno
txtCRLNum.Text = dr!crc_crlno
txtDate.Text = dr!crc_date
txtCPFNo.Text = dr!crc_cpfno
txtPreparedBy.Text = dr!crc_preparedby
txtCheckedBy.Text = dr!crc_checkedby
txtApprovedBy.Text = dr!crc_approvedby
txtGrossPrice.Text = dr!crc_grossprice
txtLess1.Text = dr!crc_lessprice1
txtDesc1.Text = dr!crc_lessdesc1
txtLess2.Text = dr!crc_lessprice2
txtDesc2.Text = dr!crc_lessdesc2
txtLess3.Text = dr!crc_lessprice3
txtDesc3.Text = dr!crc_lessdesc3
txtNetSellingP.Text = dr!crc_netsellingprice
txtRate.Text = dr!crc_rate
txtGrossRel.Text = dr!crc_grossrelease
txtDed1.Text = dr!crc_ded1
txtDDesc1.Text = dr!crc_deddesc1
txtDed2.Text = dr!crc_ded2
txtDDesc2.Text = dr!crc_deddesc2
txtDed3.Text = dr!crc_ded3
txtDDesc3.Text = dr!crc_deddesc3
txtDed4.Text = dr!crc_ded4
txtDDesc4.Text = dr!crc_deddesc4
txtDed5.Text = dr!crc_ded5
txtNetRel.Text = dr!crc_netrelease
txtCollectibles.Text = dr!crc_collectibles
conn.Close()
txtCLTAcctNo.Text = frmComm.cmbAcctNo1.Text
txtCRLNum.Text = frmComm.txtCRLNo.Text
txtProjName.Text = frmComm.txtUnitCode1.Text
txtClientName.Text = frmComm.cmbClientName1.Text + " " + frmComm.Label13.Text
txtCommType.Text = frmComm.cmbParticulars1.Text
txtPayeeName.Text = frmComm.cmbPayee01.Text
Me.lblrootpayee.Text = frmComm.lblrootpayee.Text
End Sub
It works like this: the sql query gets the data with the crc number the same as in txtCRCNum1. It works just as how I want it to, but I have some issues about this. I have 30 textboxes just like txtCRCNum1, so there are going to be 30 different values of crc number, depending on which textbox will I get its value. I want to have a conditional statement wherein it checks if the textbox that should contain the crc number is empty. If it is empty, then the fields will display just do this:
txtCLTAcctNo.Text = frmComm.cmbAcctNo1.Text
txtCRLNum.Text = frmComm.txtCRLNo.Text
txtProjName.Text = frmComm.txtUnitCode1.Text
txtClientName.Text = frmComm.cmbClientName1.Text + " " + frmComm.Label13.Text
txtCommType.Text = frmComm.cmbParticulars1.Text
txtPayeeName.Text = frmComm.cmbPayee01.Text
Me.lblrootpayee.Text = frmComm.lblrootpayee.Text
If not empty, then it will execute the sql query part.
How should I put up the conditional statement so that it will work as I have described?
You can add procedures and methods to forms just like any other class. In this case, since the CRCForm in all cases depends on a crc value, we will pass it in the constructor.
Public Class frmcrc
' a crc value property
Private _crcVal As String = "" ' I am guessing it is string
' the constructor
public Sub New(crcval As String)
' This call is required by the designer.
InitializeComponent()
_crcVal = crcval
End Sub
' etc
End Class
When the user clicks one of the 30 (?!) textbox/button pairs. This requires a form instance rather than the VB default instance (ie frmcrc.Show) which is bad anyway.
Dim f As New frmCrc(txtCRCNum1.Text)
f.Show
The form now has the value it needs to work with whether it is from TB1, Tb2 or TB22. The form need not know or care where it came from. The query would use _crcVal in the SQL.
The existing form load code then just needs to deal with cases where _crcVal is String.Empty. If it is not String.Empty then just post the frmComm parts (which probably ought to be a class) Else perform the query and display results.
You could also add a CrcValue property to the form to execute the query and display the results without creating a new form instance, just use the new crcvalue.
EDIT
A class for the "other" stuff - I cant tell what the data represents, I'll call it BaseAcct:
Public|Friend Class BaseAcct
Public Property AccountNumber As String
Public Property UnitCode As String
' etc
End Class
Newer versions support auto implemented properties where that is all you need to define a property. Older versions, you will need to code a Get and Set. You could also add procedures like FetchData and DisplayData to have the class get the data from one form and post it to another, specifying which form as an parameter. The ELSE you have to copy data from one Form to the other could then be:
Dim BAcct As New BaseAcct
' ... etc
If _crcVal <> String.Empty Then
' ...
Else
BAcct.Display(Me) ' let the class post the data to this form
End If
The crcVal can be one of the Properties in the class, but even if it is, I would require that it be passed to frmcrc in the constructor since that form seems to be meaningless without it. That is, your code should not be able to create a form instance without that value.
First thing, you should be aware of all the 30 textboxes ids'. I dont know where would you place the if statement. but i would suggest you to have a button_click event where you will check all the textboxes.
Another thing for all the different 30 textboxes, Do you have the same textboxes like txtCRCNo,txtCLTAcctNo,lblrootpayee,etc.,
If it so , then you can allow only one textbox at a time.
So make textbox_TextChanged event for all the textboxes,while typing into a textbox all other textboxes should be empty.
Note: put all the textboxes in a panel.
like,
Sub txtCRCNum1_TextChanged()
txtCRCNum2.Text=""
txtCRCNum2.Enabled=False
' and so on
End Sub
Now it is easy to make your solution on button_click
Sub button_click()
Dim ctrl As New Control
For Each ctrl In Panel1.Controls
If TypeOf ctrl Is TextBox Then
If ctrl.Text <> "" Then
'sql query with that textbox
End If
End If
Next
I have a ListView control set up in details mode, and on a button press I would like to retrieve all column values from that row in the ListView.
This is my code so far:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim items As ListView.SelectedListViewItemCollection = _
Me.ManageList.SelectedItems
Dim item As ListViewItem
Dim values(0 To 4) As String
Dim i As Integer = 0
For Each item In items
values(i) = item.SubItems(1).Text
i = i + 1
Next
End Sub
But values just comes out as an empty array. Any ideas? I just want the values array to be filled with the data of that ListView row.
Cheers.
Here's the single-line solution:
mylistview.Items.Cast(Of ListViewItem).Select(Function(lvi As ListViewItem)lvi.SubItems(1).Text).ToArray()
You need to iterate the SubItems, not the selected items. Fix:
If Me.ManageList.Items.Count <> 1 Then Exit Sub
Dim row As ListViewItem = Me.ManageList.Items(0)
Dim values(0 To row.SubItems.Count-1) As String
Dim i As Integer = 0
For Each item As ListViewItem.ListViewSubItem In row.SubItems
values(i) = item.Text
i = i + 1
Next
Just to check, are you aware that item.SubItems(1).Text will get the texts from the second column? And that since you're using SelectedItems it'll only look at the currently selected items in the ListView.
Edit:
Also, are you sure there will always just be a maximum of 5 selected items in the ListView?
Otherwise you'll have problems with
Dim values(0 To 4) As String
Old question, I know... maybe this helps for someones reference.. stumbled upon this question in a search for something else.
This works... not sure if the original poster somehow used an empy colelciton of selected items...
Also, dynamically resizing array based on selected items to overcome unforseen bugs...
Dim valueArray(mylistview.SelectedItems.Count - 1) As String
Dim i As Integer
For Each Item As ListViewItem In mylistview.SelectedItems
valueArray(i) = Item.Text
i += 1
Next
To get subitems, use item.subitems(1).text ... subitems(2).text.. etc
Cheers
I have a wizard control in wich I am adding a user control containing a simple table with
some input fields based on users entry of how many children they have. ex: how many kids do you have so I add the user control ascx based on that loop
that goes into step 5 of my wizard wich is also in a masterpage.
I then use findcontrol to atttempt the get to those input boxes so i can save the data into my db, findcontrol allway comes up null, even though the user control in visable and recreated on page load after post back.
any help greatly appreciated.
find control button:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbchildren As Integer = CInt(Howmanychildren.Text)
For i As Integer = 1 To numbchildren - 1
Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1_Child_Name"), TextBox)
'Dim textbox2 As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Howmanychildren"), TextBox)
If textbox IsNot Nothing Then
Response.Write("Found TextBox1 <br>")
Dim val As String = textbox.Text
Response.Write(val & "<br>")
Else
Response.Write("not found" & "<br>")
End If
' Insert into DB
'SaveValueToDatabase(val)
Next
End Sub
user control added function on dropdown :
Protected Sub Doyouhavechildren_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Doyouhavechildren.SelectedIndexChanged
Dim numbchildren As Integer = CInt(Howmanychildren.Text)
Dim cnt As Integer = 1
'Panel1.Controls.Clear()
Select Case Doyouhavechildren.SelectedIndex
Case 0
ViewState.Add("Doyouhavechildren", numbchildren)
Do While cnt <= numbchildren
Dim uc As Web.UI.UserControl = DirectCast(Page.LoadControl("MinorChild.ascx"), Web.UI.UserControl)
uc.ID = "Minor_" + cnt.ToString()
Wizard1.ActiveStep.Controls.Add(uc)
cnt = cnt + 1
Loop
Exit Select
Case 1
Exit Select
End Select
End Sub
user control:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="MinorChild.ascx.vb" Inherits="MinorChild" %>
Name
Age
SS#
DOB
the find control works in the howmanychildren field that is static
I figured it out myself
basicly, you have to referance the container, thats what everyone everywhere else where saying but I kept ignoring the answer
the correct code is
Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1").FindControl("Child_Name"), TextBox)
you have to referance the user control name first then search within it , even though the client source is disceptive.