根据列表项

时间:2016-06-13 21:43:06

标签: c# sql sql-server datagridview

我有一个列表,当dgv1被填充时,streetId和streetName被填充,在dgv2中我有一个用于streetName的组合框,所以我填充了它的街道,但我有一个cabName cmbxcolumn我需要根据第一列过滤它选择(streetName).i有一个桌面摄像头:

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

但是当选择一个streetName,例如具有Id 10的streetName1时,过滤器会在cabName组合框中给出三个相同的同名cabcode 19的名称:

cabName3
cabName3
cabName3

我需要过滤它只显示一个名字我觉得过滤有误,是否有人有想法。

1 个答案:

答案 0 :(得分:0)

是的,应该是因为你的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;