DataGridViewCell MouseClick Event On Particular Zone in C#

Amitava_Mukherjee
Amitava_Mukherjee used Ask the Experts™
on
I have a datagridview cell containing the following text

" + This is test"

Now I want to show a messagebox sowing message "+" if the mouse clicked on the + sign (In a single cell), and another messagebox "This is test" if mouse clicked on the rest portion. The whole string is showing in a single DataGridViewTextboxCell.

Is it possible? If it is, kindly send me the code in C#.

Thanking you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
@dj_alik: Sorry but thats not a valid answer.. what he is asking for is on click of '+' Sign A MessageBox should display the value '+' and on click of "This is test" a MessageBox should display the value "This is test"

Author

Commented:
dj_alik:

starlite551: is right. My question is something different

thanx
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Yes, it is possible. You have to split them into 2 different spans:
<span onclick="open('You clicked on +')">+</span><span onclick="open('You clicked on test')">This is test</span>

Open in new window

Author

Commented:
I am using Winform project, my question is, how can I trap the string portion in the datagridviewcell that it is clieked on the "+" portion or the remaining portion. Both are as a same string in a single datagridviewcell

thanx
Oops, sorry. I though it was a Web project.

Author

Commented:
No it is winform project
I have successfully made a project in windows forms for this solution here is the code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DataGridViewCellContentClickDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Panel panel1;

        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.panel1 = new System.Windows.Forms.Panel();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(12, 12);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dataGridView1.Size = new System.Drawing.Size(563, 250);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(13, 295);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(182, 132);
            this.panel1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(587, 439);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn c = new DataColumn("Test Value");
            dt.Columns.Add(c);
            DataRow r = dt.NewRow();
            r["Test Value"] = "+ This is test";
            dt.Rows.Add(r);
            dataGridView1.DataSource = dt;
        }        

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {            
            int xOffSet = (int)(Screen.PrimaryScreen.Bounds.Width-(Screen.PrimaryScreen.Bounds.Width - Cursor.Position.X)-this.Left-dataGridView1.Left-30);
            int xGridRatio = (int)(dataGridView1.Rows[0].Cells[0].ContentBounds.Width / 2);
            if(xOffSet < xGridRatio)
            {
                MessageBox.Show("+");
            }
            else
            {
                MessageBox.Show("This is test");
            }            
        }        
    }
}

Open in new window

In the Progam.cs Just change the startup form to Form1.

Author

Commented:
Perfect, exactly which was my need. thanx
You are welcome.. Actually I tried many combinations.. After hours of failures I found the right solution.. It needed perfect calculation to locate '+' sign and to locate 'This is test'

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial