c# WPF 从数据网格执行对许多基础的查询



我有包含所有低音信息的数据网格: 1. 第一列 - 复选框 2.第二列 - 数据库名称 3.info 4.info 我需要对每个检查的 chekbox 数据库执行查询。 在Winforms中,我使用此代码:

foreach (DataGridViewRow item in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = item.Cells[0] as DataGridViewCheckBoxCell;

if (Convert.ToBoolean(chk.Value) == true)
{

在执行之前不查询此行:

connection.ChangeDatabase(item.Cells[1].Value.ToString());

有人可以帮助我转换为WPF版本吗? 如果选中第一列(chebox(,我需要向我显示第二列(行(。

XAML :

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="490,0,0,0" VerticalAlignment="Top" Height="142" Width="728" SelectionChanged="dataGrid_SelectionChanged" >
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox"/>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>

这填充了我的数据网格:

con.server = this.server;
con.user = this.user;
con.password = this.password;
con.OpenConnection();
con.SqlQuery(Properties.Resources.databaseCatalogResource);
DataTable dt = con.QueryEx();
con.da.Fill(dt);
dataGrid.ItemsSource = dt.DefaultVie

w;

您可以遍历DataView

DataView dv = dataGrid.ItemsSource as DataView;
foreach(DataRowView drv in dv)
{
if(Convert.ToBoolean(drv["BoolProperty"]))
{
//row is checked...
}
}

当然,您的DataTable必须包含一个名为"BoolProperty"的列,因为您将CheckBox绑定到此列。如果没有,则需要添加一个:

con.server = this.server;
con.user = this.user;
con.password = this.password;
con.OpenConnection();
con.SqlQuery(Properties.Resources.databaseCatalogResource);
DataTable dt = con.QueryEx();
con.da.Fill(dt);
dt.Columns.Add(new DataColumn("BoolProperty", typeof(bool))); //<---
dataGrid.ItemsSource = dt.DefaultView;