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;

14
  • no one can't help me ? :( Aug 16, 2017 at 6:59
  • 1
  • this help me but shows me textfields of all lines. I need to show me second row if first(chebox) is checked. Aug 16, 2017 at 9:00
  • If you need logic in your datagrid, i would recommend using an observablecollection and using this as the itemsource of the datagrid. if the observablecollection is changed you can react to that with an event. In the Event method you can add/remove rows or use other logic to change the collection. the changes of the collection are automatically passed to the UI/Datagrid. Aug 16, 2017 at 9:13
  • No i don't need logic. I just need to get checked row... :( Aug 16, 2017 at 9:19

1 Answer 1

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;
3
  • How can set value of this column? Aug 17, 2017 at 6:57
  • i try with this : DataRow dr = dt.NewRow(); dr["BoolProperty"] = true; but not work Aug 17, 2017 at 7:20
  • 1
    Please ask a new question if you have another issue.
    – mm8
    Aug 17, 2017 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.