0
votes

I have a WinForm with a DataGridView set to read-only. When the user double-clicks on a row (cell) I want to display a form in which the full details for the selected Row are shown.

But the CellDoubleClick Event is not firing. Somewhere on the web i've read that the event doesn't work when DataGridView is set to read-only, but even inserting a new DataGridView with default settings in MS Visual Studio doesn't help.

For Testing I used a sample code from MSDN, but the MessageBox doesn't open.

Here's my full Code:

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Projektmanager
{
    public partial class Projektmanager : Form
    {
        // Declare MySQL Connection
        private MySqlConnection connection;

        public Projektmanager()
        {
            InitializeComponent();

            if (this.OpenConn() == true)
            {
                string sql = "select Name as 'Auftragsnummer', Projektnummer, Gruppe as 'G-Nr.', (select name from Groups where NR=stdjobs.Gruppe limit 1) as 'Gruppe', Händler, Kunde, Land from stdjobs where NR like 'A%' order by NR desc;";
                MySqlDataAdapter MSDA = new MySqlDataAdapter(sql, connection);
                DataSet DS1 = new DataSet();
                MSDA.Fill(DS1);
                dataGridView1.DataSource = DS1.Tables[0];

                this.CloseConn();
            }
        }

        // open MySQL Connection Function
        private bool OpenConn()
        {
            // Some Stuff
        }

        //Close MySQL connection Function
        private bool CloseConn()
        {
           // More Stuff
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void refresh_DGV4()
        {
            // Much more Stuff
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            refresh_DGV4();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            refresh_DGV4();
        }

        private void DataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
        {
            System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
            messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex);
            messageBoxCS.AppendLine();
            MessageBox.Show(messageBoxCS.ToString(), "CellDoubleClick Event");
        }
    }
}

Can anyone tell me why the Event isn't recognized?

1
Have you verified the handle has been attached to the event using dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick; or dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(DataGridView1_CellDoubleClick);?OhBeWise
"To run the example code, paste it into a project that contains an instance of type DataGridView named DataGridView1. Then ensure that the event handler is associated with the CellDoubleClick event."Ivan Stoev
To expand upon OhBeWise's comment, look in your "[name].Designer.cs" file. This is what is called with the InitializeComponent() call. In there, you will see the datagridview logic. That is where it would have attached to the event. If it's not there, add it.Aaron
Thank You. I will try that tomorrow morning.Gerald Zehetner
Ok. I just added it to the *.Design.cs file and now it works.Gerald Zehetner

1 Answers

0
votes

From the OP comments:

As user Ivan Stoev pointed out and from the MSDN article, emphasis added:

To run the example code, paste it into a project that contains an instance of type [DataGridView] named DataGridView1. Then ensure that the event handler is associated with the [CellDoubleClick] event.

As stated by user OhBeWise, this might look like:

dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick;

or

dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(DataGridVi‌​ew1_CellDoubleClick)‌​;

To verify the above association is made, follow the advice given by user Aaron, formatting added:

[L]ook in your "[name].Designer.cs" file. This is what is called with the InitializeComponent() call. In there, you will see the [DataGridView] logic. That is where it would have attached to the event. If it's not there, add it.