5
Answers

Swap characters for file linked datagridview

Jose

Jose

14y
3.9k
1
I have a project started where a Windows form application searches for a specified directory (and any changes there of using a filemonitor) and lists the files in a datagridview.  Each file found is linked to the file location and the resulting file (in this case text files) is rendered to a textbox.  Currently the cellclickevent displays the file name of the file.  What I would like to do is search the contents of the file for a specific line and character location.  In my example, I need to display the text displayed on line 3 of the file starting at character 8 (Line3, Char 8), and only look for a maximum of say 40 characters beyond the 8th character position.  This character string will represent a name of a person, usually in this format "Lastname, Firstname". 

So as opposed to displaying the file name in the gridview (fi.Name), I want to swap the characters for the persons name as it appears on line 3, char 8 through 48.  Is anyone familiar with how to do this?  I still need to maintain the file link in the cellclick event.  Any help here is greatly appreciated.  Here is the code I'm using for these gridview which is bound to the filemonitor, and the cellclick event:

        public void DirMon_Load(object sender, System.EventArgs e)
        {
            FileMonitor.Path = Path.ToString();
            FileMonitor.Filter = Filter.ToString();
            FileMonitor.IncludeSubdirectories = IncludeSubs;
            FileMonitor.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            //Begin monitoring.
            FileMonitor.EnableRaisingEvents = true;

            //Get the list of existing files from the folder and list in the textbox
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\test");
            //to retrieve *.TXT files alone
            System.IO.FileInfo[] rgFiles = di.GetFiles("*.txt");

            foreach (System.IO.FileInfo fi in rgFiles)
            {
                int index = dataGridView1.NewRowIndex;
                dataGridView1.Rows.Add(fi.Name);
            }

            dataGridView1_CellContentClick(null, null);

        }
        public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            richTextBox1.Text = "";
            if (dataGridView1.CurrentCell.OwningColumn.Name.Equals("File"))
            {
                if (dataGridView1.CurrentCell != null)
                {
                    string filename = dataGridView1.CurrentCell.Value.ToString();
                    string fullpath = string.Format("{0}\\{1}", Path, filename);
                    StreamReader file = null;
                    try
                    {
                        file = new StreamReader(fullpath);
                        richTextBox1.LoadFile(fullpath,
                            RichTextBoxStreamType.PlainText);
                        // this is a hidden text box that the user does not see, used for printing
                        HiddentextBox1.Text = fullpath;
                    }
                    finally
                    {
                        if (file != null)
                            file.Close();
                    }
                }
            }
        }

Answers (5)