Datagridviewcell content detect by C#

Amitava_Mukherjee
Amitava_Mukherjee used Ask the Experts™
on
I asked a question to EE you may find below:

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#.


EE answered me the following (starlite551: had gave me the solution)

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");
            }



It is working fine when the column is the left first column in datagridview but do not work if it is not the left most column

any suggestion?

thanking you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
LOL. Not saying it's impossible but just too tedious to do it. Why you just can't make any click and use an option for messagebox to allow user to select, then you do what you wish to do.

Using such concept, your programming work will be hell lot than what other need to do, you are killing yourself. You must identify the left bound of the actual cell which the user clicks and work from there and maybe even your end users don't know there's a difference clicking on the text or "+" sign itself.

Author

Commented:
Client is GOD, and we do as client wish :-(

thanx

Author

Commented:
So Experts,

No solution?
Here are some modifications to starlite551's code to work with any column.
private void Form1_Load(object sender, EventArgs e)
    {
      DataTable dt = new DataTable();
      DataColumn c = new DataColumn("Test Value");
      dt.Columns.Add(c);
      DataColumn d = new DataColumn("Test Value2");
      dt.Columns.Add(d);
      DataRow r = dt.NewRow();
      r["Test Value"] = "+ This is test";
      r["Test Value2"] = "+ This is another test";
      dt.Rows.Add(r);
      dataGridView1.DataSource = dt;
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
      int headerColumnWidth = 30;
      int xOffSet = (int)(Cursor.Position.X - this.Left - dataGridView1.Left - headerColumnWidth);
      for (int i = 0; i < e.ColumnIndex; i++)
        xOffSet -= dataGridView1.Rows[0].Cells[e.ColumnIndex].ContentBounds.Width;
      int xGridRatio = (int)(dataGridView1.Rows[0].Cells[e.ColumnIndex].ContentBounds.Width / 2);
      if (xOffSet < xGridRatio)
      {
        MessageBox.Show("+");
      }
      else
      {
        string s = dataGridView1.Rows[0].Cells[e.ColumnIndex].Value as string;
        s = s.Substring(2);
        MessageBox.Show(s);
      }
    }

Open in new window

Author

Commented:
thanx

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