5 Replies - 1840 Views - Last Post: 09 May 2014 - 04:24 AM

#1 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-February 13

DataGridView ForColor /select

Posted 08 May 2014 - 02:27 PM

Hello, I have a question. Is it possible to change the forcolor of one or more text lines in a datagridview or even better to "select" words.

The idea is that a user can search for a word or couple of words and the search results (from a database) are displayed in a datagridview and only the searched text is displayed in another color in the datagridview.

Or even better the searched text is displayed as if it was selected just like the search functionality in Internet Explorer. (see attachment)

Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView ForColor /select

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3326
  • View blog
  • Posts: 11,248
  • Joined: 12-December 12

Re: DataGridView ForColor /select

Posted 08 May 2014 - 02:46 PM

Investigate the CellFormatting event. Here is an example.

I would store the search-value as a field (or property) of the form, and then, probably, Invalidate the DGV. I haven't investigated further but I'm assuming that this would cause the DGV to re-execute the CellFormatting event and change the cell-colours, because the event will refer to the search-value. (I may be wrong at this stage.)

I'm not sure that you can paint individual words, probably not easily.

This post has been edited by andrewsw: 08 May 2014 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3326
  • View blog
  • Posts: 11,248
  • Joined: 12-December 12

Re: DataGridView ForColor /select

Posted 08 May 2014 - 03:17 PM

Yeah, works fine (took 5 mins ;))
    public partial class Form1 : Form {
        string searchTerm;

        public Form1() {
            InitializeComponent();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            if (e.Value != null && searchTerm != null) {
                string stringValue = (string)e.Value;
                stringValue = stringValue.ToLower();
                if ((stringValue.IndexOf(searchTerm) > -1)) {
                    e.CellStyle.ForeColor = Color.Red;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            searchTerm = textBox1.Text;
            dataGridView1.Invalidate();

        }
    }


Amended slightly to clear the colour when the search-term is empty:
    public partial class Form1 : Form {
        string searchTerm = String.Empty;

        public Form1() {
            InitializeComponent();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            if (e.Value != null && !String.IsNullOrWhiteSpace(searchTerm)) {
                string stringValue = (string)e.Value;
                stringValue = stringValue.ToLower();
                if ((stringValue.IndexOf(searchTerm) > -1)) {
                    e.CellStyle.ForeColor = Color.Red;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            searchTerm = textBox1.Text;
            dataGridView1.Invalidate();
        }
    }

Was This Post Helpful? 0
  • +
  • -

#4 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-February 13

Re: DataGridView ForColor /select

Posted 09 May 2014 - 02:34 AM

Thank you for your reply and time. I'll look at the link yo gave me. I find it difficult to understand what you wrote, I'm used to vb.net in stead of C#, and don't have a lot of experience with vb.net yet.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3326
  • View blog
  • Posts: 11,248
  • Joined: 12-December 12

Re: DataGridView ForColor /select

Posted 09 May 2014 - 02:56 AM

If you had posted in the VB.NET forum I would have given the code in that language ;)
Was This Post Helpful? 0
  • +
  • -

#6 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-February 13

Re: DataGridView ForColor /select

Posted 09 May 2014 - 04:24 AM

Serious.. I really thought I did... Hum guess I have to copy past than ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1