【问题标题】:having duplicate values when filtering a combobox based on selection from a list items根据从列表项中的选择过滤组合框时具有重复值
【发布时间】:2016-06-13 21:43:06
【问题描述】:

我有一个列表,其中填充了 dgv1 时填充了 streetId 和 streetName,在 dgv2 中我有一个用于 streetName 的组合框列,所以我在其中填充了街道,但是我有一个 cabName cmbxcolumn 我需要根据第一列对其进行过滤selection(streetName)。我有一台台式相机:

camIp cabCode streetId camType camCode
  11.5    34       9    camtype1  PTZ
  11.7    34       9    camtype2   C
  12.1    19      10    camtype2   A
  12.2    19      10    camtype3   B
  12.3    19      10    camtype4   PTZ

还有一个表 cabCode:

cabCode  cabName
 19      cabName1
 34      cabName2
 35      cabName3

这是我尝试过的: 我为相机创建了一个数据表:

DataTable CamerastblUsage = new DataTable();
SqlCommand cmd5 = cnn.CreateCommand();
cmd5.CommandText = "SELECT cam.camIp,cam.cabCode,c.cabName,cam.streetId ,cam.camType,cam.camCode from camera cam , cab c where cam.cabCode = c.cabCode";

BindingSource unfilteredCamerasUsageBS = new BindingSource();
DataView undv2 = new DataView(CamerastblUsage);
unfilteredCamerasUsageBS.DataSource = undv2;
Column23.DisplayMember = "cabName";
Column23.ValueMember = "camIp";
Column23.DataSource = unfilteredCamerasUsageBS;

// this binding source is where I perform my filtered view
BindingSource filteredCamerasUsageBS = new BindingSource();
DataView dv6 = new DataView(CamerastblUsage);
filteredCamerasUsageBS.DataSource = dv6;

然后我使用了 cellbeginedit 事件:

if (e.ColumnIndex == Column23.Index )
{
//filter cabCode combobox based on streetId selected in column index 0
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView3[e.ColumnIndex, e.RowIndex];

dgcb.DataSource = filteredCamerasUsageBS;
this.filteredCamerasUsageBS.Filter = "streetId = " +
Convert.ToString(this.dataGridView3[e.ColumnIndex - 1, e.RowIndex].Value.ToString());
}

但在选择街道名称时,例如 ID 为 10 的街道名称 1,过滤器会在 cabName 组合框列中给出三个相同 cabcode 19 的相同名称:

cabName3
cabName3
cabName3

我需要过滤它只显示一个名字我认为过滤有误,有人知道吗。

【问题讨论】:

    标签: c# sql sql-server datagridview


    【解决方案1】:

    是的,应该是因为在您的 camera 表中 cabCode = 19 重复了 3 次,并且您正在加入,因此产生了重复的数据。看起来你可以在这里使用ROW_NUMBER() 函数

    SELECT * FROM (
    SELECT cam.camIp,
    cam.cabCode,
    c.cabName,
    cam.streetId ,
    cam.camType,
    cam.camCode,
    ROW_NUMBER() OVER(PARTITION BY cam.camCode ORDER BY cam.camCode) AS rn
    from camera cam 
    join cab c 
    on cam.cabCode = c.cabCode ) XXX
    WHERE rn = 1;
    

    【讨论】:

    • 它不工作,你试过吗?有没有办法将cab表作为数据源,然后通过其他方式进行绑定和过滤
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2020-10-09
    • 1970-01-01
    • 2018-02-12
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    相关资源
    最近更新 更多