【问题标题】:c# datagridview with a column type hyperlink after db read in winformc#datagridview在winform中读取db后带有列类型超链接
【发布时间】:2016-03-16 09:03:42
【问题描述】:

我有这种情况:

查询后我在 DataSet 中有这个表:

Name | Module | Date | Approvation
xx   |  xxx   | xxx  | xxxxxxxx
yy   |  yyy   | yyy  | yyyyyyyyy 


        DataTable dt = new DataTable();
        //  dgvApprovazione is a datagridview
        dgvApprovazione.DataSource = dt

现在在这种情况下,我在 4 列输入文本(字符串):名称、模块、日期、批准...

我想要列 Module 是一个 文件链接...然后 xxx 是一个链接,yyy 是一个链接...和其他..

我看过 DataGridViewLinkColumn 但我不知道这是否是一个好方法以及如何设置..

【问题讨论】:

    标签: c# datagridview datagridviewlinkcolumn


    【解决方案1】:

    DataGridViewLinkColumn 是要走的路,实现它应该很简单:

    this.dataGridView1.CellContentClick += DataGridView1_CellContentClick;
    this.dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete;
    
    private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewLinkCell)
        {
            System.Diagnostics.Process.Start(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string);
        }
    }
    
    private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            row.Cells["Module"] = new DataGridViewLinkCell();
        }
    }
    

    这个答案的主要来源来自this SO answer,减去条件检查。其他答案也很丰富。

    【讨论】:

    • 好的,谢谢... 一般来说,我可以滚动一列 datagridview 吗?例子 ; foreach(Datagridv.Columns[0] 中的行行。?????) { //something } how??
    • 您是否要遍历列的所有单元格?如果是,您必须像我在DataBindingComplete 中展示的那样进行操作 - 遍历所有行并访问所需列的单元格。否则,你是什么意思?
    • 您也可以遍历列:foreach (DataGridViewColumn col in dataGridView1.Columns),但这不会像遍历行那样让您访问单个单元格。
    • 抱歉...@OhBeWise 我想在一个列中滚动所有单元格...示例我有一个 4 列的 datagridview ...但我想要滚动列 [2]...而不是所有列。 foreach (DataGridViewRow row in this.dataGridView.Columns[2].**methodToGetCell**) 。有可能吗?
    • 我不知道。您可以使用 DataGridViewRow 上的 Frozen 属性进行垂直滚动或使用 DataGridViewColumn 进行水平滚动,但不能使用单个单元格。设置 Columns[2].Frozen = true 也会冻结第 0 列和第 1 列。
    猜你喜欢
    • 2014-03-02
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多