1

我正在使用此代码使我的列(从 dataTable 中的数据库中获取)作为链接列

编辑:

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);

    }

当我单击此列的任何链接(内容链接)时,我正在使用下面的代码打开表单,但事件没有被触发,我在哪里做错了?

 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();
     }
 }
4

1 回答 1

0

将列添加到DataGridView. 我了解您正在通过依赖默认类型 ( TextBoxColumn) 添加列,这在您的第一个代码中没有更改(您将单元格转换为DataGridViewLinkCell,而不是列)。因此,您的代码应使用以下修改:

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();
    }
}

无论如何,请记住,您所做的细胞类型修改并非在所有情况下都 100% 保存。如果您正在填充DataGridViewfrom aDataSource并且只执行一次单元格类型更改,则可以(最快/最简单的选项);在任何其他情况下(您必须手动添加列),您最好DataGridViewLinkColumn在添加列时在开始时设置给定列的类型(在这种情况下)。

于 2013-10-18T07:41:50.580 回答