Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView CellClick event is firing twice
 

DataGridView CellClick event is firing twice

For some reason my datagridview cellclick is firing twice. Did anyone of you encounter the same problem?
After clicking my DataGridViewButtonColumn and it will call the datagridview cellclick event it will go through it twice. I event tried calling the CellEndEdit event but it still not working. I can probably flag it but that is an ugly solution, am I missing some event calls?
Below is my code.

if (e.RowIndex >= 0 && e.ColumnIndex ==
           dgridViewTestStationEntry.Columns[(Int32)DgridTestEquipmentEntryColumns.XmlScriptButton].Index)
            {
                string xmlFile = BrowseXMLFile();
                if (!String.IsNullOrEmpty(xmlFile))
                {
                    dgridViewTestStationEntry.Rows[e.RowIndex].Cells[(Int32)DgridTestEquipmentEntryColumns.XMLScript].Value = xmlFile;
                }
            }
            else if (e.RowIndex >= 0 && e.ColumnIndex ==
           dgridViewTestStationEntry.Columns[(Int32)DgridTestEquipmentEntryColumns.AssetIDButton].Index)
            {
                Dictionary<string, object> assetID = BrowseAssetID();
                if (assetID != null)
                {

                    foreach (KeyValuePair<string, object> item in assetID)
                    {
                        dgridViewTestStationEntry.Rows[e.RowIndex].Cells[(Int32)DgridTestEquipmentEntryColumns.AssetID].Value = item.Key;
                        dgridViewTestStationEntry.Rows[e.RowIndex].Cells[(Int32)DgridTestEquipmentEntryColumns.AssetIDButton].Value = item.Value;

                    }

                }
            }
            

a-caridad-  Wednesday, October 07, 2009 8:51 PM

Hello,

Thanks for your post.

I performed a test on my side. I tried the following code, but I cannot reproduce the issue on my machine.

        DataTable dt = new DataTable();

        private void CellClickTest_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("name");
            dt.Columns.Add("price", typeof(decimal));
            dt.Columns.Add("volume", typeof(int));
            dt.Rows.Add("item1", 1.25, 3);
            dt.Rows.Add("item2", 2.25, 8);
            dt.Rows.Add("item3", 4.25, 6);
            dt.AcceptChanges();

            this.dataGridView1.DataSource = dt;

            DataGridViewButtonColumn bc = new DataGridViewButtonColumn();
            bc.HeaderText = " ";
            bc.Text = "Add";
            this.dataGridView1.Columns.Add(bc);

            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
        }

        int count = 0;

        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 3)
            {
                count++;
                this.lblcount.Text = count.ToString();
            }
        }

There must be something wrong in the other code rather than the CellClick event handler, please make sure that you didn't register the event twice. Could you please send me a simple sample that can reproduce this issue to my mail box[v-rzhang[at]microsoft.com]? I will perform test on my side and keep you updated.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg[at]microsoft.com



Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer bya-caridad- 13 hours 39 minutes ago
  •  
Rong-Chun Zhang  21 hours 10 minutes ago

Hello,

Thanks for your post.

I performed a test on my side. I tried the following code, but I cannot reproduce the issue on my machine.

        DataTable dt = new DataTable();

        private void CellClickTest_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("name");
            dt.Columns.Add("price", typeof(decimal));
            dt.Columns.Add("volume", typeof(int));
            dt.Rows.Add("item1", 1.25, 3);
            dt.Rows.Add("item2", 2.25, 8);
            dt.Rows.Add("item3", 4.25, 6);
            dt.AcceptChanges();

            this.dataGridView1.DataSource = dt;

            DataGridViewButtonColumn bc = new DataGridViewButtonColumn();
            bc.HeaderText = " ";
            bc.Text = "Add";
            this.dataGridView1.Columns.Add(bc);

            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
        }

        int count = 0;

        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 3)
            {
                count++;
                this.lblcount.Text = count.ToString();
            }
        }

There must be something wrong in the other code rather than the CellClick event handler, please make sure that you didn't register the event twice. Could you please send me a simple sample that can reproduce this issue to my mail box[v-rzhang[at]microsoft.com]? I will perform test on my side and keep you updated.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg[at]microsoft.com



Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer bya-caridad- 13 hours 39 minutes ago
  •  
Rong-Chun Zhang  21 hours 10 minutes ago
Hi Rong  - I was about to send you my sample code but then the I cannot reproduce the error. I went back to my code and found 2 event handlers. I didn't notice that the designer created another events. Now it is working great! Thanks!
a-caridad-  13 hours 39 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• How to autosize a DataGridView to fit in both directions?
• Adding a Data Connection that's specific to the Project/Form and making the Data Source path dynamic...
• list boxes more HELP needed.
• update dataview to dataset
• Could I modify the command created by aqlcommandbuilder?
• DataGridViewComboBoxColumn selected value problem
• does datagridview.datasource override datagridviewcombobox.datasource?
• Displaying selected data in a detail form.
• SqlRowUpdatedEventHandler Delete Information
• datagridviewcheckboxcolumn