locked
Hyperlink in databound datagridview RRS feed

  • Question

  • I have a windows form user control that is binding a datagridview from sql server query. How can I get one of the columns that I'm returning to display as a hyperlink. I've seen some snipets using datagridviewlinkcolumn, but they seem to be adding the column manually. I think I understand the cellclick event to open a new form after the cell in the datagridview has been clicked, just can't get the column as hyperlink. I'm a newbie and any help would be great. an example of my code is below....

     

    string strCon = "Data Source=test.com;Initial Catalog=test; User ID=sa";

    string strSQL = "select * from table1";

    SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);

    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

    DataTable table = new DataTable();

    dataAdapter.Fill(table);

    dbBindSource.DataSource = table;

    dbGridView.DataSource = dbBindSource

    Friday, October 19, 2007 7:41 PM

Answers

  •  

    Hi

    Below is a sample for how to use a DataGridViewLinkColumn, and I hope that can help you.

    Code Block

    namespace NewDGV

    {

        public partial class Form34 : Form

        {

            public Form34()

            {

                InitializeComponent();

            }

     

            DataTable dt = new DataTable();

     

            private void Form34_Load(object sender, EventArgs e)

            {

                dt.Columns.Add("id");

                dt.Columns.Add("link");

                dt.Rows.Add("1", "http://www.microsoft.com");

                dt.Rows.Add("2", "http://www.google.com");

                dt.AcceptChanges();

     

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.Columns.RemoveAt(1);

     

                DataGridViewLinkColumn dgvlc = new DataGridViewLinkColumn();

                dgvlc.DataPropertyName = "link";//bind to the correct data column

                dgvlc.HeaderText = "Link";

                dgvlc.Width = 260;

                this.dataGridView1.Columns.Add(dgvlc);

     

                this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);

            }

     

            void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

            {

                if (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)

                {

                    //do your stuff here

                    string link = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

                    System.Diagnostics.Process.Start(link);

                }

            }

        }

    }

     

    Best regards.
    Rong-Chun Zhang

    Monday, October 22, 2007 6:16 AM

All replies

  •  

    Hi

    Below is a sample for how to use a DataGridViewLinkColumn, and I hope that can help you.

    Code Block

    namespace NewDGV

    {

        public partial class Form34 : Form

        {

            public Form34()

            {

                InitializeComponent();

            }

     

            DataTable dt = new DataTable();

     

            private void Form34_Load(object sender, EventArgs e)

            {

                dt.Columns.Add("id");

                dt.Columns.Add("link");

                dt.Rows.Add("1", "http://www.microsoft.com");

                dt.Rows.Add("2", "http://www.google.com");

                dt.AcceptChanges();

     

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.Columns.RemoveAt(1);

     

                DataGridViewLinkColumn dgvlc = new DataGridViewLinkColumn();

                dgvlc.DataPropertyName = "link";//bind to the correct data column

                dgvlc.HeaderText = "Link";

                dgvlc.Width = 260;

                this.dataGridView1.Columns.Add(dgvlc);

     

                this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);

            }

     

            void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

            {

                if (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)

                {

                    //do your stuff here

                    string link = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

                    System.Diagnostics.Process.Start(link);

                }

            }

        }

    }

     

    Best regards.
    Rong-Chun Zhang

    Monday, October 22, 2007 6:16 AM
  • Thanks. It helps me.

    Wednesday, November 7, 2007 9:14 PM
  • Thanks Rong, it worked like charm...
    Friday, January 6, 2012 6:47 AM