1

I'm currently working on an "advanced" search form for my application. I'm using a datagridview to display the results. The idea is to be able to search thoroughly (with lots of options).

Currently I fill the datagridview like this:

    dgvData.AutoGenerateColumns = false;

        if (cbbKlant.SelectedItem != null)
        {
            dgvData.DataSource = StatistiekManagement.getOpdrachten((klant)cbbKlant.SelectedItem);

            //ID kolom
            DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
            id.Name = "ID";
            id.DataPropertyName = "opdracht_id";

            //Plaatsen kolom
            DataGridViewTextBoxColumn plaatsen = new DataGridViewTextBoxColumn();
            plaatsen.Name = "Plaatsen";
            plaatsen.DataPropertyName = "aantal_personen";

            //Vertrekplaats kolom
            DataGridViewTextBoxColumn vertrek = new DataGridViewTextBoxColumn();
            vertrek.Name = "Vertrek";
            vertrek.DataPropertyName = "locatie_id";

            this.dgvData.Columns.Add(id);
            this.dgvData.Columns.Add(plaatsen);
            this.dgvData.Columns.Add(vertrek);
        }

My problem here is I want to add information to the datagridview from another table. For example: I have a contract and this contract has a location. How do I display the location into this datagridview?

I also use LINQ TO SQL to get my data from the database.

Thanks, Thomas

4

2 回答 2

1

您不能将 datagridview 绑定到两个不同的源。

您的解决方案是编写一条 SQL 语句来连接两个不同的表,然后用它填充一个数据集。然后,您可以将其用作您的数据源。

于 2012-07-04T15:19:54.837 回答
1

有几种方法可以附加位置。最简单的方法是您可以将 Location 作为属性添加到绑定的基类中。它不必是数据库中的一个字段,只是一个可以绑定的属性。如果继承不是一种选择,有时封装可以工作。

除了上述之外,您始终可以将非绑定列添加到(绑定)datagridview。它应该显示的值可以来自您喜欢的任何来源。显示值的一种方法是使用 datagridview 的 cellformatting 事件:

        //inside initialization void
        dgvData.CellFormatting+=new DataGridViewCellFormattingEventHandler(dgvData_CellFormatting);
        dvcol = new DataGridViewTextBoxColumn();
        dgvData.Columns.Add(dvcol);
    }

    DataGridViewColumn dvcol;

    void dgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (dvcol != null && e.RowIndex != -1 && e.ColumnIndex == dvcol.Index)//where Column1 is your combobox column
        {
            var rec = (YourRecordTypeSuchAsContract)dgvData.Rows[e.RowIndex].DataBoundItem;
            e.Value = ""; //get description based on the rec
        }
    }
于 2012-07-04T15:44:40.643 回答