0

I have datagrid with all my basses information: 1. First column - checkbox 2.Second column - database name 3.info 4.info I need to execute query for every checked with chekbox database. In Winforms i do with this code :

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


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

and before executenonquery this line :

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

Can someone help me to convert to WPF version? I need to show me second column(row) if first column(chebox) is checked.

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>

This fill my 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;

1 Answers1

1

You could iterate through the DataView:

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

Of course your DataTable must contain a column called "BoolProperty" since you bind your CheckBox to this one. If it doesn't you need to add one:

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;
mm8
  • 163,881
  • 10
  • 57
  • 88