Reputation: 37
this question is related to my previous questions
I'm currently working on a Registration form where all the details of a student are save in a text file.
I populated the combobox using a textfile.
The format of these values are for example: (code~school name) SCH001~Saint Thomas College
this is how I saved the details in a text file:
Dim firstname, lastname, email, mobile, level, currentschool, currenttrack, institution1, institution2, institution3, institution4, institution5, institution6, courses1, courses2, courses3, macaddress, eventcode, idseq, filename, logfiledirectory, targetdirectory, log, configdir, printschool As String
firstname = txtFName.Text
lastname = txtLName.Text
email = txtEmail.Text
mobile = txtMobile.Text
level = cmbLevel.Text
currenttrack = cmbCurrentTrack.Text
printschool = cmbCurrentSchool.Text
currentschool = cmbCurrentSchool.SelectedValue
institution1 = cmbInstitution1.SelectedValue
institution2 = cmbInstitution2.SelectedValue
institution3 = cmbInstitution3.SelectedValue
institution4 = cmbInstitution4.SelectedValue
institution5 = cmbInstitution5.SelectedValue
institution6 = cmbInstitution6.SelectedValue
courses1 = cmbCourse1.SelectedValue
courses2 = cmbCourse2.SelectedValue
courses3 = cmbCourse3.SelectedValue
If mobile = "" Then
mobile = "09000000000"
End If
If firstname = "" Or lastname = "" Or email = "" Or level = "" Or currentschool = "" Or currenttrack = "" Then
MessageBox.Show("Please enter the required fields!", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
I have a datagridview where all the saved details in a text file are shown.
here's a screenshot of my datagridview:
WHAT I WANT TO DO: I want to retrieve the School name using the current school code (refer to the image) from the source text file(the one I used to populated my comboboxes). Instead of the school code, I want the School name to be shown in the datagridview. Is there a possible way to do it?
I hope you guys understand what im trying to say. Im new here.
Upvotes: 0
Views: 671
Reputation: 654
If you want to produce the same output as Nobody's answer but without looping, you can use LINQ, this also uses less lines of code but achieves the same output
Dim query = filecontents.Where(Function(x) x.Contains(schoolcode)).Select(Function(x) x.Split("~")(1)).FirstOrDefault
Return query.ToString()
Upvotes: 1
Reputation: 3247
Again just like I answered last time, use this function:
Function FetchSchoolName(schoolcode As String, filepath As String) As String
Dim filecontents = File.ReadAllLines(filepath)
For Each line As String In filecontents
If (line.Contains(schoolcode.Trim())) Then
Return line.Split("~"c)(1)
End If
Next
Return String.Empty
End Function
Example text entry:
sch001~abcinstitute
sch002~myacademy
sch003~someschoolname
Then pass the schoolcode(which you get from the datagridview) and filepath in the top-most function
MessageBox.Show(FetchSchoolName("sch002", "C:\Users\nobody\Desktop\somefile.txt"))
Output:
myacademy
Now in respect to your DataGridView
, wherever you are filling(source binding) it use the above method
Upvotes: 2