本文介绍了在单个datagridview中显示来自相关表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有一些表:

I have some tables in the db:

Items             Manufacturers       Categories      Cities      Regions
==============    ================    ============    ========    ==========
ItemId            ManufacturerId      CategoryId      CityId      RegionId
ManufacturerId    CityId              NameCategory    RegionId    NameRegion
CategoryId        NameManufacturer                    NameCity
NameItem
Weight

我正在使用以下代码显示 DataGridView 中的项目列表:

I am displaying the list of the items in DataGridView using this code:

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from Items", connectionString);

SqlCommandBuilder cmdBldr = new SqlCommandBuilder(da);
da.Fill(ds, "Items");
dataGridView1.DataSource = ds.Tables[0];

我还有一个按钮可以将更改保存在 DataGridView ,其代码如下:

I also have button that save the changes in the DataGridView with this code:

da.Update(ds, "Items");

我想替换datagridview中的两列- ManufacturerId CategoryId 与相关的 NameManufacturer NameCategory 表。因此,这两列应该是ComboBox,并带有相关表中的所有可能名称-以便能够更改 Category Manufacturer

I want to replace two columns in datagridview - ManufacturerId and CategoryId with the NameManufacturer and NameCategory from related tables. So those two columns should be ComboBox with all possible names from related tables - to be able to change the Category or Manufacturer to other and save it using da.Update().

我还需要为datagridview添加三个组合框过滤器:
Category 城市区域,它们将按选定的值过滤datagridview中显示的项目在这些过滤器中。

I also need to add three combobox filters for the datagridview:Category, City and Region, that will filter the displayed items in the datagridview by selected values in those filters.

我不能使用向导创建器,我必须在代码中全部完成。
如果您能给我一些与此部分有关的想法,那就太好了。

I can't use wizard creator, I have to do it all in the code.If you could give me some ideas related to any part of this, will be great.

推荐答案

ComboBox列



对于要成为组合框的每一列,您都应创建并设置这些属性并将其添加到网格的


  • DataSource :要在组合框中显示的项目列表

  • DataPropertyName :的数据源属性名称

  • DisplayMember :要在其中显示的组合框的数据源列的名称组合框

  • ValueMember :组合框的数据源列的名称,当您从组合框中选择一个项目时使用其值

  • DataSource: list of items that you want to show in combo box
  • DataPropertyName: Name of a property of data source of grid, that you want to combobox bind to it.
  • DisplayMember: Name of a column of data source of combo box to show in combo box
  • ValueMember: Name of a column of data source of combo box to use its value when you select an item from combo box.

考试在这里是 CategoryId 列的代码:

For example here is the code for CategoryId column:

var categoryAdapter= new SqlDataAdapter("SELECT * FROM Categories", connectionString);
var categoryTable= new DataTable();
categoryAdapter.Fill(categoryTable);

var categoryComboBoxColumn=new DataGridViewComboBoxColumn();
categoryComboBoxColumn.Name="categoryComboBoxColumn";
categoryComboBoxColumn.HeaderText="Category";
categoryComboBoxColumn.DataSource = categoryTable;
categoryComboBoxColumn.DataPropertyName = "CategoryId";
categoryComboBoxColumn.DisplayMember= "NameCategory";
categoryComboBoxColumn.DisplayMember= "CategoryId";
this.dataGridView1.Columns.Add(categoryComboBoxColumn);



过滤器



要过滤网格,您可以分配到主数据表的 DefaultView.RowFilter

例如,要基于 CategoryId 进行过滤,可以创建 System.Windows。 Forms.ComboBox 控件并将其命名为 categoryComboBox 并将其绑定到类别以显示类别列表并设置其 DisplayMember NameCategory 及其 ValueMember CategoryId 在该组合框中,然后:

For example to filter based on CategoryId, you can create a System.Windows.Forms.ComboBox control and name it for example categoryComboBox and bind it to categories to show list of categories and set its DisplayMember to NameCategory and its ValueMember to CategoryId in that combo box and then:

var table = ((DataTable)dataGridView1.DataSource);
table.DefaultView.RowFilter =
    string.Format("CategoryId = {0}", categoryComboBox.SelectedValue);

您可以通过将过滤器设置为null或为空来重置过滤器。

You can reset the filter by setting it to null or empty.

此外,您还可以使用和/或创建表达式:

Also you can create expression with and / or:

var criterias = new List<string>();
if(categoryComboBox.SelectedIndex > 0)
    criterias.Add(string.Format("CategoryId = {0}", categoryComboBox.SelectedValue);

if(cityComboBox.SelectedIndex > 0)
    criterias.Add(string.Format("CityId = {0}", cityComboBox.SelectedValue);

var table = ((DataTable)dataGridView1.DataSource);
table.DefaultView.RowFilter = string.Join(" And " , criterias);

这篇关于在单个datagridview中显示来自相关表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:26