question

SindhuH-2474 avatar image
0 Votes"
SindhuH-2474 asked karenpayneoregon answered

.csv file data into textox in winform

Hello,

The purpose of my code is to display the value in .csv file according to the user input of row and column in windows form.
And I have been only finding the code with datagridview tool, which doesn't suffice the purpose of coding.
I need to display a certain value in the textbox

I would be obliged if anybody would help me find a way to solve this problem
Thank you

windows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please show your current efforts in a small code sample and include

  • What is the logic for displaying a certain value in the textbox?

  • Are you using .NET Framework or .NET Core Framework?

  • How are you reading the file?



0 Votes 0 ·

Hello,

I am using .Net Framework in Windows Form using Visual Basic
Below is my code :

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim Textbox(20) As System.Windows.Forms.TextBox

     Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Users\Admin\Documents\Sample.csv")
         MyReader.TextFieldType = FileIO.FieldType.Delimited
         MyReader.SetDelimiters(",")

         Dim currentRow As String()
         Dim i As Int16 = 0
         While Not MyReader.EndOfData
             Try
                 currentRow = MyReader.ReadFields()
                 Dim currentField As String
                 For Each currentField In currentRow
                     Textbox(i).Text = currentField
                 Next
                 i = i + 1
             Catch ex As Microsoft.VisualBasic.
                         FileIO.MalformedLineException
                 MsgBox("Line " & ex.Message &
                 "is not valid and will be skipped.")
             End Try
         End While
     End Using

 End Sub

End Class

Above is my code.
I have my numerical data in .csv file of marks
And I want to display the marks of a student in a certain subject and of a certain class, exclusively.
And this is how my code looks like.
Also, I am trying to display values in textbox , and I want to display the by accessing their index.
For eg, TextBox(1) = 20
TextBox(2) = 30 and so on..

Thank you

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Here is a proof of concept with mocked data that mot likely doesn't match your data but you will get the idea.

Container for data

 Public Class Student
     Public Property Id() As Integer
     Public Property FirstName() As String
     Public Property LastName() As String
     Public Property Course() As String
     Public Property ClassName As String
     Public Property Grade() As Decimal
    
     Public Overrides Function ToString() As String
         Return $"{FirstName} {LastName}"
     End Function
 End Class


Class to read data


 Public Class FileOperations
     Public Shared Function Read(studentIdentifier As Integer) As List(Of Student)
    
         Return (From line In File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Students.txt"))
                 Where line.Length > 0
                 Let Items = line.Split(","c)
                 Select New Student() With
                 {
                     .Id = CInt(Items(0)),
                     .FirstName = Items(1),
                     .LastName = Items(2),
                     .Course = Items(3),
                     .ClassName = Items(4),
                     .Grade = CDec(Items(5))
                 }).Where(Function(student) student.Id = studentIdentifier).ToList()
    
    
     End Function
 End Class

Form code, one button which reads/display data.

 Imports System.IO
    
 Public Class Form1
     Private Sub ReadStudentsFileButton_Click(sender As Object, e As EventArgs) _
         Handles ReadStudentsFileButton.Click
    
         Dim students = FileOperations.Read(1)
         For Each student As Student In students
             Debug.WriteLine($"{student.ClassName,-20}{student.Course,-20}{student.Grade} ")
         Next
    
     End Sub
 End Class

So now we can do things like sum and average, here is average

 Imports System.IO
    
 Public Class Form1
     Private Sub ReadStudentsFileButton_Click(sender As Object, e As EventArgs) _
         Handles ReadStudentsFileButton.Click
    
         Dim students = FileOperations.Read(1)
    
         Dim EnglishList = students.Where(Function(student) student.Course = "English")
    
         Dim Averagelist = EnglishList.Average(Function(student) student.Grade)
         Debug.WriteLine(Averagelist)
    
     End Sub
 End Class


Source in file

 1,Nile,Smith,English,Class1,100.00
 1,Nile,Smith,English,Class2,99.5
 2,Mary,Adams,English,Class2,99.00
 1,Nile,Smith,Computer Science,Class1,80.4
 2,Mary,Adams,English,Class3,90.25


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.