4 Replies - 283 Views - Last Post: 17 December 2017 - 07:28 AM Rate Topic: -----

#1 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Random Access Text Files into DataGridView In Vb.net

Posted 16 December 2017 - 10:45 AM

I am using random access and text files to store administrator details. Is there a way to just completely take all the data from the text file and format it in the data grid view instead of what I am doing which follows this procedure :
1) Search Admin ID
2) Outputs to textbox
3) Click "Add Admin to Datagrid view"
4) Shows the details there

this process is long winded and inefficient. Furthermore the records on the data grid are whipped and you have to re-add admins on the data grid when i stop debugging.

My question is
1) Is there a way of taking the data straight away from the text file and skipping the step of the procedure above and formatting it in the datagrid view

or

2) taking the procedure above but when I un debug the records can still be there ?



    Private Sub Administrator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        table.Columns.Add("AdminID", Type.GetType("System.String"))
        table.Columns.Add("Forename", Type.GetType("System.String"))
        table.Columns.Add("Surname", Type.GetType("System.String"))
        table.Columns.Add("Password", Type.GetType("System.String"))
        table.Columns.Add("Date Of Birth", Type.GetType("System.String"))
        table.Columns.Add("Contact Number", Type.GetType("System.String"))
        table.Columns.Add("Emergency Contact", Type.GetType("System.String"))
        table.Columns.Add("Address One", Type.GetType("System.String"))
        table.Columns.Add("Address Two", Type.GetType("System.String"))
        table.Columns.Add("PostCode", Type.GetType("System.String"))
        table.Columns.Add("Hour Salary", Type.GetType("System.String"))



        DataGridView1.DataSource = table

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_AddAdminDataGrid.Click

        table.Rows.Add(Tbx_AdminID.Text, Tbx_Forename.Text, Tbx_Surname.Text, Tbx_Password.Text, Tbx_DateOfBirth.Text, Tbx_ContactNumber.Text, Tbx_EmergencyContact.Text, Tbx_AddressOne.Text, Tbx_AddressTwo.Text, Tbx_PostCode.Text, Tbx_HourSalary.Text)
        DataGridView1.DataSource = table
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim index As Integer
        index = e.RowIndex
        Dim selectedrow As DataGridViewRow
        selectedrow = DataGridView1.Rows(index)
        Tbx_AdminID.Text = selectedrow.Cells(0).Value.ToString()
        Tbx_Forename.Text = selectedrow.Cells(1).Value.ToString()
        Tbx_Surname.Text = selectedrow.Cells(2).Value.ToString()
        Tbx_Password.Text = selectedrow.Cells(3).Value.ToString()
        Tbx_DateOfBirth.Text = selectedrow.Cells(4).Value.ToString()
        Tbx_ContactNumber.Text = selectedrow.Cells(5).Value.ToString()
        Tbx_EmergencyContact.Text = selectedrow.Cells(6).Value.ToString()
        Tbx_AddressOne.Text = selectedrow.Cells(7).Value.ToString()
        Tbx_AddressTwo.Text = selectedrow.Cells(8).Value.ToString()
        Tbx_PostCode.Text = selectedrow.Cells(9).Value.ToString()
        Tbx_HourSalary.Text = selectedrow.Cells(10).Value.ToString()
    End Sub

    Private Sub Btn_UpdateAdmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_UpdateAdmin.Click

        Dim newdatarow As DataGridViewRow
        newdatarow = DataGridView1.Rows(index)

        newdatarow.Cells(0).Value = Tbx_AdminID.Text
        newdatarow.Cells(1).Value = Tbx_Forename.Text
        newdatarow.Cells(2).Value = Tbx_Surname.Text
        newdatarow.Cells(3).Value = Tbx_Password.Text
        newdatarow.Cells(4).Value = Tbx_DateOfBirth.Text
        newdatarow.Cells(5).Value = Tbx_ContactNumber.Text
        newdatarow.Cells(6).Value = Tbx_EmergencyContact.Text
        newdatarow.Cells(7).Value = Tbx_AddressOne.Text
        newdatarow.Cells(8).Value = Tbx_AddressTwo.Text
        newdatarow.Cells(9).Value = Tbx_PostCode.Text
        newdatarow.Cells(10).Value = Tbx_HourSalary.Text


    End Sub

This post has been edited by WizWazard: 16 December 2017 - 10:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Random Access Text Files into DataGridView In Vb.net

#2 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Random Access Text Files into DataGridView In Vb.net

Posted 16 December 2017 - 04:07 PM

I am using random file access to store admin details. For formatting my file I have used the following code. However my records are saved in one row so that when I click the button to show the textfile data in the datagridview it shows everything in a row. Is there a way I can format it so that when there is a new ID and new admin it will go to the next line of the data grid view ?
I am using the following code


  With DataGridView1
            .Columns.Add("AdminID", "AdminID")
            .Columns.Add("Forename", "Forename")
            .Columns.Add("Surname", "Surname")
            .Columns.Add("Password", "Password")
            .Columns.Add("Date Of Birth", "Date Of Birth")
            .Columns.Add("Contact Number", "Contact Number")
            .Columns.Add("Emergency Contact", "Emergency Contact")
            .Columns.Add("AddressOne", "AddressOne")
            .Columns.Add("AddressTwo", "AddressTwo")
            .Columns.Add("PostCode", "PostCode")
            .Columns.Add("Hour Salary", "Hour Salary")
          
        End With


        For Each line As String In System.IO.File.ReadAllLines("C:\Users\ahmed\Documents\Visual Studio 2010\Projects\SkettySurgeryGPMedicalSystem\SkettySurgeryGPMedicalSystem\bin\Debug\Admin.dat")
            DataGridView1.Rows.Add(line.Split("AdminID;Forename;Surname;Password;DateOfBirth;ContactNumber;EmergencycContact;AddressOne;AddressTwo;PostCode;HourSalary"))
        Next

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6444
  • View blog
  • Posts: 26,064
  • Joined: 12-December 12

Re: Random Access Text Files into DataGridView In Vb.net

Posted 16 December 2017 - 05:32 PM

I have merged your two topics. Questions about the same code should be in the same thread.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6444
  • View blog
  • Posts: 26,064
  • Joined: 12-December 12

Re: Random Access Text Files into DataGridView In Vb.net

Posted 16 December 2017 - 05:44 PM

If you have a well-formed csv you can treat it as a data source and Fill a DataTable. You can use a BindingSource to help navigate it.

Load CSV To DataTable For Random Access
Was This Post Helpful? 0
  • +
  • -

#5 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Random Access Text Files into DataGridView In Vb.net

Posted 17 December 2017 - 07:28 AM

I am still really confused my friend. Would I keep the same way of how I approached to accessing the file ? I am just having problems with the padding as it is printing the entire file out in one line and when i output this to a datagrid view it is doing the same and printing out in one line. Sorry to be a pain is there certain steps i need to follow? I tried to understand the link you posted however I dont know what is relevant
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1