Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 932
poy Avatar asked Sep 17 '26 18:09

poy


1 Answers

 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;
                }
            }
        }
like image 146
Marius Krämer Avatar answered Sep 21 '26 16:09

Marius Krämer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!