-1

I have a cell with two or more phone numbers split by a semicolon. I need that each telephone inside the cell realize a especific action on click. Exemple: if I click in especific number it will show a message box with this number.

  • Not sure, but possible duplicate of [this Question](https://stackoverflow.com/questions/10896623/hyperlink-cell-in-winforms-datagridview).And if not, it will help alot. – Cataklysim Nov 01 '17 at 13:45
  • I try Split the String and put the value inside the cells in diferents labels. I did think to take label ID at identify when i click. – Robert Garcia Nov 01 '17 at 13:49
  • But the cell not acept. This is the code => ClientDataGridView.Rows.Add(new object[] { Name, labelPhone1.ToString()+""+""+labelPhone2.ToString(), addres } – Robert Garcia Nov 01 '17 at 14:07

1 Answers1

0

Use the approach of Master/Details. Split the phone list and store it in the string collection. Bind this collection to a suitable control such as a ListBox. Handle the selection change in this control.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        DataGridView peopleDataGridView;
        ListBox phonesListBox;
        List<Person> people;

        public Form1()
        {
            //InitializeComponent();

            peopleDataGridView = new DataGridView { Parent = this, Dock = DockStyle.Top };
            phonesListBox = new ListBox { Parent = this, Dock = DockStyle.Bottom };

            people = new List<Person> {
                new Person { Id=1, Name="John", Phones=new List<string> { "123", "456" } },
                new Person { Id=2, Name="Smit", Phones=new List<string> { "789", "012" } }
            };

            var peopleBindingSource = new BindingSource();
            peopleBindingSource.DataSource = people;
            peopleDataGridView.DataSource = peopleBindingSource;

            var phonesBindingSource = new BindingSource(peopleBindingSource, "Phones");
            phonesListBox.DataSource = phonesBindingSource;

            phonesListBox.SelectedIndexChanged += PhonesListBox_SelectedIndexChanged;
        }

        private void PhonesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(phonesListBox.SelectedItem.ToString());
        }
    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<string> Phones { get; set; }
    }
}
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49