[RESOLVED] Random Access Text Files into DataGridView In Vb.net
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 ?
Code:
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
Re: Random Access Text Files into DataGridView In Vb.net
Hi ,
this will read a Textfile with ; as a seperator
File format :
AdminID;Forename;Surname;etc...
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
With DataGridView1
.Columns.Add("1", "1")
.Columns.Add("2", "2")
.Columns.Add("3", "3")
.Columns.Add("4", "4")
.Columns.Add("5", "5")
'add the Columns you need
End With
For Each line As String In System.IO.File.ReadAllLines("C:\myTest.txt")
DataGridView1.Rows.Add(line.Split(";"))
Next
End Sub
regards
Chris
Re: Random Access Text Files into DataGridView In Vb.net
I used this code and my textfile is located here "C:\Users\Creator\Documents\Visual Studio 2010\Projects\Florestsystem\Florestsystem\bin\Debug\Admin.dat" but it says it couldnt find it but im sure that is the file location as i went on the properties
Re: Random Access Text Files into DataGridView In Vb.net
Also would i do it like this ?
With DataGridView1
.Columns.Add("AdminID", "AdminID")
.Columns.Add("Forename", "Forename")
.Columns.Add("3", "3")
etc .Columns.Add("4", "4")
.Columns.Add("5", "5")
'add the Columns you need
End With
For Each line As String In System.IO.File.ReadAllLines("C:\myTest.txt")
DataGridView1.Rows.Add(line.Split("AdminID;Forename"))
Next
because its not working
Re: Random Access Text Files into DataGridView In Vb.net
error message is Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
Re: Random Access Text Files into DataGridView In Vb.net
Quote:
Originally Posted by
CreatorAxor
Also would i do it like this ?
With DataGridView1
.Columns.Add("AdminID", "AdminID") 'yes you can create the Columns like this
.Columns.Add("Forename", "Forename")
.Columns.Add("Password", "Password")
etc .Columns.Add("4", "4")
.Columns.Add("5", "5")
'add the Columns you need
End With
'create a File like this
1;Chris;Chris2;MyPassword; etc
2;CreatorAxon;2Field;Pass; etc..
3; etc...
For Each line As String In System.IO.File.ReadAllLines("C:\myTest.txt") 'for Testing create a File
DataGridView1.Rows.Add(line.Split(";")) '<- this stays the way it is
Next
because its not working
regards
Chris
Re: [RESOLVED] Random Access Text Files into DataGridView In Vb.net
Hi,
forget this Post, I saw in another thread that Niya put you on the right track.
sure hope you appreciate the effort(Code) Niya gave you
regards
Chris
Re: [RESOLVED] Random Access Text Files into DataGridView In Vb.net
Sorry to not get back to you aswell, this works but the formatting is overlapping
Re: [RESOLVED] Random Access Text Files into DataGridView In Vb.net
Quote:
Originally Posted by
CreatorAxor
Sorry to not get back to you aswell, this works but the formatting is overlapping
well, if you know the Formatting is wrong the try to Fix it.
You have another Thread with the same question/problem. You will have to decide which Format you want to use.
also how many Admins are there ? seem's strange.
even in big companies I see at most 4-5, any more and things can get out of hand.
regards
Chris
2 Attachment(s)
Re: [RESOLVED] Random Access Text Files into DataGridView In Vb.net
i have 3 at max but i want to be able to remove and add and view admins. the problem I have using the code above and I like it as it makes logical sense. but I will show you with images the problem if you would know how to fix i would appreciate it :) Attachment 154597 it prints all in one record all the file in only adminId section. Currently i have two admins and they are both in the AdminID (all their information)
Re: [RESOLVED] Random Access Text Files into DataGridView In Vb.net
Hi,
you now have 3 Threads with this question/Problem !!
my advice would be to create a Database with one Table, or if you wish for each Admin ?
from looking at the images a Database Table would be better than a Textfile
EDIT:
if you still want to use a Textfile, then here a simple solution
Code:
Imports System.IO
Imports System.Text
Public Class Form1
'File Format is:
'1;Chris;Frank
'2;Creator;whatever
'3;Tom;Houston
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'open the File
For Each line As String In System.IO.File.ReadAllLines("C:\Admins.txt")
DataGridView1.Rows.Add(line.Split(";"))
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With DataGridView1
.Columns.Add("AdminID", "AdminID")
.Columns.Add("Firstname", "Firstname")
.Columns.Add("Lastname", "Lastname")
'add the Columns you need
End With
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'Save the Datagrid, and show the result
Dim filePath As String = "C:\Admins.txt"
Dim delimeter As String = ";"
Dim sb As New StringBuilder
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim array As String() = New String(DataGridView1.Columns.Count - 1) {}
'this is for the Header Text:
'If i.Equals(0) Then
'For j As Integer = 0 To DataGridView1.Columns.Count - 1
' array(j) = DataGridView1.Columns(j).HeaderText
'Next
'sb.AppendLine(String.Join(delimeter, array))
'End If
For j As Integer = 0 To DataGridView1.Columns.Count - 1
If Not DataGridView1.Rows(i).IsNewRow Then
'you should use Value.ToString !
'using FormattedValue.ToSting will allow empty Cells
'to be saved
array(j) = DataGridView1(j, i).FormattedValue.ToString
End If
Next
If Not DataGridView1.Rows(i).IsNewRow Then
sb.AppendLine(String.Join(delimeter, array))
End If
Next
File.WriteAllText(filePath, sb.ToString)
'Opens the file immediately if you want to see it
Process.Start(filePath)
End Sub
End Class
regards
Chris