poy
poy

Reputation: 10507

C# DataGridViewLinkCell Display

Is it possible to have a DataGridViewLinkCell display something like search but the link be http://google.com?

I would rather the DataGridView not be littered with actual links.

Upvotes: 3

Views: 5277

Answers (1)

Marius Krämer
Marius Krämer

Reputation: 446

 private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewLinkColumn c = new DataGridViewLinkColumn();
            dataGridView1.Columns.Add(c);
            dataGridView1.Rows.Add();
            dataGridView1.Rows[0].Cells[0].Value = "search";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            switch (dataGridView1[e.ColumnIndex,e.RowIndex].Value.ToString())
            {
                case ("search"):
                    Process.Start("http://www.google.com");
                    break;
            }
        }

Or this way, to avoid large switch case:

        class customcolumn : System.Windows.Forms.DataGridViewLinkColumn
        {
            public Dictionary<int, string> urls = new Dictionary<int, string>();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int row_index = 0;
            int column_index = 0;

            customcolumn c = new customcolumn();
            dataGridView1.Columns.Add(c);
            dataGridView1.Rows.Add();

            //Add Link-name here:
            dataGridView1.Rows[row_index].Cells[column_index].Value = "search";
            //Add Link here:
            ((customcolumn)(dataGridView1.Columns[column_index])).urls.Add(row_index, "http://www.google.com");
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            foreach (KeyValuePair<int, string> url in ((customcolumn)(dataGridView1.Columns[e.ColumnIndex])).urls)
            {
                if (url.Key == e.RowIndex)
                {
                    Process.Start(url.Value);
                    break;
                }
            }
        }

Upvotes: 3

Related Questions