我有俩表
table1 filed1 filed2
1 22
2 2323
3 343
4 2323
table2 filed1 filed2
1 22
3 343
通过 gridview 绑定 table1 和 checkbox 并且 让gridview中的checkbox 根据table2 默认选中(即table2中有的行默认选中)
最简单、方便的方法就是通过SQL查询返回DataTable,比如:
select a.*,IsExists=case when exists(select 1 from table2 where filed1=a.filed1 and filed2=filed2) then 1 else 0 end
from table1 a
该SQL语句返回结果将是:
filed1 filed2 IsExists
1 22 1
2 2323 0
3 343 1
4 2323 0
============================
select a.field1,a.field2,cast(case when b.field1 is null then 0 else 1 end as bit) as state from table1 a
left join table2 b on b.field1=a.field1
结果:
field1 field2 state
----------- ----------- -----------
1 22 1
2 2323 0
3 343 1
4 2323 0
直接绑定state
<Columns>
<asp:CheckBoxField DataField="state" />
</Columns>