简体   繁体   中英

datagridview Link cellcontent click not working

I am using this code to make my column(fetched from database in dataTable) as linkcolumn

edit:

void show_visits()
    {
        try
        {
            con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sonorepo.mdb");
            con.Open();
        }
        catch (Exception err)
        {
            MessageBox.Show("Error:" + err);
        }
        this.pid = Convert.ToInt32(db.GetPatientID(cmbPatientName.SelectedItem.ToString()));
        cmd1 = new OleDbCommand("Select Patient_ID,VisitNo,VisitDate,remark from Patient_Visit_Details WHERE Patient_ID=" + pid, con);
        dt = new DataTable();
        adp1 = new OleDbDataAdapter(cmd1);
        adp1.Fill(dt);
        this.dataGridViewVisits.DataSource = dt;
        foreach (DataGridViewRow row in dataGridViewVisits.Rows)
        {
            DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
            linkCell.Value = row.Cells[2].Value;
            row.Cells[2] = linkCell;
        }
        this.dataGridViewVisits.CellContentClick+=new DataGridViewCellEventHandler(this.CellContentClick);

    }

and I am using below code to open form when i click on any link(content link) of this column ,but event is not getting triggered ,where I am doing mistake ?

 private void CellContentClick(object sender, DataGridViewCellEventArgs e)
 {
     if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewLinkColumn))
     {
         int pid = Convert.ToInt32(dataGridViewVisits.Rows[e.RowIndex].Cells["Patient_ID"].Value);
         ViewR viewrepofrm = new ViewR(pid);
         viewrepofrm.MdiParent = this.ParentForm;
         viewrepofrm.Show();
     }
 }

The type of the column is set when adding it to the DataGridView . I understand that you are adding columns by relying on the default type ( TextBoxColumn ) and this is not changed in your first code (you are converting cells into DataGridViewLinkCell , not columns). Thus, your code should work with the following modification:

private void CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && ((DataGridView)sender)[e.ColumnIndex, e.RowIndex].GetType() == typeof(DataGridViewLinkCell))
    {
         int pid = Convert.ToInt32(dataGridViewVisits.Rows[e.RowIndex].Cells["Patient_ID"].Value);
         ViewR viewrepofrm = new ViewR(pid);
         viewrepofrm.MdiParent = this.ParentForm;
         viewrepofrm.Show();
    }
}

In any case, bear in mind that the kind of cell-type modification you are doing is not 100% save in all the situations. If you are populating the DataGridView from a DataSource and you perform the cell type change just once, it is OK (quickest/easiest option); in any other situation (where you have to add the columns manually), you should better set the type for the given column ( DataGridViewLinkColumn in this case) at the start, when adding the column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM