Hi all,


Im adding a column after the dataGridView is populated.
Then I want to add LinkLables to that column.
how to do this.
PLZ help me.

I've tried this.But it gives me an error.
this is my buton click.its ok.

private void button1_Click(object sender, EventArgs e)
        {
            Search ser = new Search();
           DataSet drr= ser.GetBookDetail(int.Parse(textBox3.Text));
            DataTable myDataTable = drr.Tables["table1"];
             dataGridView1.DataSource = myDataTable;

           
            DataColumn dc=new DataColumn("Author");
            myDataTable.Columns.Add(dc);

 }

But this is not working.

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            LinkLabel link = new LinkLabel();
           dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = link;
        }

Recommended Answers

Where are you getting the error at. If we know where your error is then we can better answer your question.

EDIT: Try this website. It is very informative about how to do this:

Jump to Post

All 2 Replies

I just made this code based on what I understood from your post.

using System;
using System.Windows.Forms;

namespace DataGridViewTest
{
    public partial class Form1 : Form
    {
        private DataGridView dataGridView1 = new DataGridView();
        private LinkLabel link1 = new LinkLabel();
        private LinkLabel link2 = new LinkLabel();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();

            link1.Text = "http://yahoo.com/";
            link2.Text = "http://hotmail.com/";

            DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
            col1.Name = "column1";
            col1.HeaderText = "REGULAR COLUMN";
            col1.Width = 100;
            dataGridView1.Columns.Add(col1);

            DataGridViewLinkColumn col2 = new DataGridViewLinkColumn();
            col2.Name = "column2";
            col2.HeaderText = "LINK COLUMN";
            col2.Width = 200;
            dataGridView1.Columns.Add(col2);

            dataGridView1.Rows.Add(2);
            dataGridView1.Rows[0].Cells[0].Value = "Hello";     // Row 0 Cell 0   Regular Column
            dataGridView1.Rows[0].Cells[1].Value = link1.Text;  // Row 0 Cell 1   Link Column
            dataGridView1.Rows[1].Cells[1].Value = link2.Text;  // Row 1 Cell 1   Link Column

            dataGridView1.Top = 100;
            dataGridView1.Left = 100;
            dataGridView1.Height = 200;
            dataGridView1.Width = 400;
            this.Controls.Add(dataGridView1);
        }

        void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                // Open the link in the default browser
                System.Diagnostics.Process.Start(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.