刷新Datagrid OnClick复选框

时间:2019-03-22 07:07:16

标签: c# sql wpf

我需要刷新数据网格和DB SQL表之间的连接。当我单击数据网格上显示的某些项目的复选框时,我必须刷新所有数据网格。所有...为什么?

因为如果未单击用于修改复选框状态的单击,则状态不会更改。为此,我认为可能是为了简化操作,可以刷新SELECT查询。这使我可以将数据库表显示到数据网格中。

那我该怎么办呢?我应该刷新查询还是刷新数据网格?这是复选框的代码:

//CHECKBOX MAINLIST
private void Row_DoubleClick_ex(object sender, MouseButtonEventArgs e)
{
    sqliteCon.Open();
    if (sqliteCon.State == System.Data.ConnectionState.Open)
    {
        //PER IDENTIFICARE LA LINEA CORRENTE:
        var currentRowIndex = ListTable.Items.IndexOf(ListTable.CurrentItem);
        string q = @"UPDATE tabList
                     SET selection = (
                         CASE WHEN (SELECT selection 
                                    FROM tabList 
                                    where idL = @CURRENT) = 0
                              THEN 1
                              ELSE 0
                         END)
                     WHERE idL = @CURRENT";
        // This doesn't work:
        CollectionViewSource.GetDefaultView(ListTable.ItemsSource).Refresh();
        SqlCommand cmd = new SqlCommand(q, sqliteCon);
        currentRowIndex = currentRowIndex + 1;
        cmd.Parameters.AddWithValue("@CURRENT", currentRowIndex);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Item Checked");
    }
    sqliteCon.Close();
}
CollectionViewSource.GetDefaultView(ListTable.ItemsSource).Refresh(); 

我已经尝试实现它,但是它不起作用。这是XAML

 <DataGrid AutoGenerateColumns="True" x:Name="ListTable" Binding.SourceUpdated="ListTable_SourceUpdated"  Grid.ColumnSpan="2" ColumnWidth="165" Margin="0,0,94,-12.5"  >
                        <DataGrid.ItemContainerStyle>

                            <Style TargetType="DataGridRow">

                                <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick_ex"/>
                            </Style>
                        </DataGrid.ItemContainerStyle>
                    </DataGrid>

有查询

//Primo caricamento tabella dati
        try
        {
            sqliteCon.Open();
            string Query = "select tabList.selection,tabList.nomeItem , tabList.resItem from tabList ";

            SqlCommand createCommand = new SqlCommand(Query, sqliteCon);
            createCommand.ExecuteNonQuery();
            SqlDataAdapter dataAdp = new SqlDataAdapter(createCommand);
            DataTable dtlb = new DataTable("tabList");
            dataAdp.Fill(dtlb);
            ListTable.ItemsSource = dtlb.DefaultView;
            dataAdp.Update(dtlb);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        sqliteCon.Close();

0 个答案:

没有答案