I've a datagridview shown, I would like to change the font of that row to bold when a user hover over any cell on that row. I know I can use the rowenter and rowleave but some how it changed the font bold to the whole grid not just the row the mouse pointer was on.

    private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
       var dGV = sender as DataGridView;
       if (dGV.Rows[e.RowIndex].Selected)
       {
          System.Windows.Forms.DataGridViewCellStyle boldStyle = new System.Windows.Forms.DataGridViewCellStyle();
         boldStyle.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold);
         dGV.Rows[e.RowIndex].DefaultCellStyle = boldStyle;
       }
    }

Ok, I kinda figured out about half of the what I wanted. But my problem is now when the mouse leave the row, it didn't change the row back to the way it was before. Below is my code, could someone tell me what I'm doing wrong? Common guys, I know there are some sharp guys out there in the forum.

    private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
    {
        DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y);

        if (hti.RowIndex >= 0 && hti.RowIndex != OldRow)
        {
            dataGridView1.Rows[OldRow].Selected = false;
            System.Windows.Forms.DataGridViewCellStyle boldStyle = new System.Windows.Forms.DataGridViewCellStyle();
            boldStyle.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Regular);

            dataGridView1.Rows[OldRow].DefaultCellStyle = boldStyle;

            dataGridView1.Rows[hti.RowIndex].Selected = true;

            //System.Windows.Forms.DataGridViewCellStyle boldStyle = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridView1.Rows[OldRow].DefaultCellStyle = boldStyle;

            boldStyle.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold);

            dataGridView1.Rows[hti.RowIndex].DefaultCellStyle = boldStyle;
            OldRow = hti.RowIndex;
        }
    }

Hi Nicewave!

What you are going to want to do is listen for the CellMouseEnter and CellMouseLeave events. This way, you can set the rows to the appropriate styles on enter and leave.

I have attached a sample project that shows how this can be done.

using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class DataGridWithBoldForm : Form
    {
        private DataSet _data;

        private DataGridViewCellStyle _normalStyle;

        private DataGridViewCellStyle _boldStyle;

        public DataGridWithBoldForm()
        {
            InitializeComponent();

            // define styles
            _normalStyle = new DataGridViewCellStyle();
            _normalStyle.Font = new System.Drawing.Font("Arial", 10F, FontStyle.Regular);

            _boldStyle = new DataGridViewCellStyle();
            _boldStyle.Font = new System.Drawing.Font("Arial", 10F, FontStyle.Bold);

            // get data
            _data = new DataSet("People");
            _data.ReadXml(@".\People.xml");

            // bind data to control
            dataGridView1.DataSource = _data;
            dataGridView1.DataMember = "Person";

            // add event listeners
            dataGridView1.CellMouseEnter += new DataGridViewCellEventHandler(dataGridView1_CellMouseEnter);
            dataGridView1.CellMouseLeave += new DataGridViewCellEventHandler(dataGridView1_CellMouseLeave);
        }

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            var rowIndex = e.RowIndex;

            if (rowIndex < 0 || rowIndex >= dataGridView1.Rows.Count)
            {
                return;
            }

            dataGridView1.Rows[rowIndex].DefaultCellStyle = _boldStyle;
        }

        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            var rowIndex = e.RowIndex;

            if (rowIndex < 0 || rowIndex >= dataGridView1.Rows.Count)
            {
                return;
            }

            dataGridView1.Rows[rowIndex].DefaultCellStyle = _normalStyle;
        }

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.