|
-
Mar 18th, 2007, 02:47 PM
#1
Thread Starter
New Member
[2005] reading and saving rtf control data to file
Hi,
Fairly new to .net so appologies if this is fairly simple, i have a windows form with a datagrid control. On the form there is also a rich text box control.
What i would like is for the rich text box control to be comments about each record selected. This needs to save the files localy, not in the datasource.
and then load and populate the rtf control if there is an existing file for that record.
Has anyone got any pointers or example code I could work from
Thanks in advance.
Last edited by firespeed81; Mar 18th, 2007 at 03:24 PM.
-
Mar 18th, 2007, 04:27 PM
#2
Fanatic Member
Re: [2005] reading and saving rtf control data to file
I don't know how you plan on filling you grid, but anyway each record must have an ID I would use that ID as the name of the file dscription and locate it in a specific folder. Here is the code to read the file and put it in the RTF
Code:
Dim reader As IO.StreamReader
Try
reader = IO.File.OpenText("Folder" & ID & ".txt")
While reader.Peek <> -1
Me.RichTextBox1.Text &= reader.ReadLine()
End While
Catch ex As Exception
Console.WriteLine("File not found")
End Try
-
Mar 18th, 2007, 08:21 PM
#3
Re: [2005] reading and saving rtf control data to file
You should store the file path in the database. When the user selects a record in your DataGridView (please use the proper names to avoid confusion, a DataGridView is NOT a DataGrid) you can load the associated file into the RTB:
vb Code:
Private filesByRecord As New Dictionary(Of DataRow, String)
Private Sub BindingSource1_CurrentChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BindingSource1.CurrentChanged
'The user just selected a record.
Dim row As DataRow = DirectCast(Me.BindingSource1.Current, DataRowView).Row
If row.IsNull("FilePath") Then
'There is no associated file.
Me.RichTextBox1.Clear()
Else
'This record has an associated file.
If Not Me.filesByRecord.ContainsKey(row) Then
'Cache the associated file.
Me.filesByRecord.Add(row, IO.File.ReadAllText(CStr(row("FilePath"))))
End If
'Load the file contents from the cache.
Me.RichTextBox1.Text = Me.filesByRecord(row)
End If
End Sub
This code assumes that you are using a BindingSource between your data and the grid. You don't have to do so but I strongly suggest that you do.
Last edited by jmcilhinney; Mar 18th, 2007 at 08:29 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|