Datagrid选择不能使用WPF C# [英] Datagrid selecteditems not working WPF C#

查看:71
本文介绍了Datagrid选择不能使用WPF C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据网格中删除所选行,也需要从数据表中删除。我尝试了这段代码,但它没有被删除。

datagrid.SelectedItems返回null。所以它没有执行Foreach循环



我尝试过:



从数据网格和数据表中删除行的代码

我的数据网格名称是dataGrid

I need to delete the selected row from the datagrid and also from the datatable. I tried this code, but it's not getting deleted.
datagrid.SelectedItems returns null. So it's not executing the Foreach loop

What I have tried:

Code to delete a row from datagrid and datatable
My Datagrid name is dataGrid

<pre>private void button_Click_1(object sender, RoutedEventArgs e)
        {
            string db = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\Users\Lavanya\Documents\BMDatabase.accdb;Persist Security Info=False";
            OleDbConnection oledb = new OleDbConnection(db);
            OleDbCommand olecmd = new OleDbCommand("Select * From ACTOR", oledb);
            try
            {
                OleDbDataAdapter oleda = new OleDbDataAdapter();
                oleda.SelectCommand = olecmd;
                DataTable dbtable = new DataTable();
                oleda.Fill(dbtable);
                //BindingSource bsource = new BindingSource();
                //bsource.DataSource = dbtable;

                dataGrid.ItemsSource = dbtable.DefaultView;
                oleda.Update(dbtable);
                int drow = dbtable.Rows.Count;
                int dcolumn = dbtable.Columns.Count;
                // System.Windows.MessageBox.Show(drow.ToString("G"));
                // System.Windows.MessageBox.Show(dcolumn.ToString("G"));
                //prepare the list of rows that need to delete
                List<DataRow> listOfRowsToDelete = new List<DataRow>();
                foreach (DataRowView drRow in this.dataGrid.SelectedItems)
                {
                    listOfRowsToDelete.Remove(drRow.Row);
                }
                //delete all the rows from the datatable
                foreach (DataRow drRow in listOfRowsToDelete)
                {
                    dbtable.Rows.Remove(drRow);
                }
                //  if (dataGrid != null)
                // {
                //   int selectedIndex = dataGrid.SelectedIndex;
                // dbtable.Rows.RemoveAt(selectedIndex);
                // bsource.RemoveAt(selectedIndex);
                //var row = dbtable.Rows[selectedIndex];
                //row.Delete();
                //oleda.Update(dbtable);

                // System.Windows.MessageBox.Show(selectedIndex.ToString("G"));

                // }
                //else
                //   {
                // if (dataGrid.SelectedItems.Count >= 1)
                //{
                //  while (dataGrid.SelectedCells.Count > 0)
                //{
                //     int selectedIndex = dataGrid.SelectedIndex;
                //    dbtable.Rows.RemoveAt(selectedIndex);
                // bsource.RemoveAt(selectedIndex);
                //    var row = dbtable.Rows[selectedIndex];
                //    row.Delete();
                //    oleda.Update(dbtable);
                //  }
                //}
                // }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);



            }
        }







<pre><DataGrid x:Name="dataGrid" ItemsSource="{Binding Properties}" SelectedItem="{Binding SelectedProperty,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="238" Margin="10,120,0,0" VerticalAlignment="Top" Width="733" />

推荐答案

如果您要将DataGrid用作选择器,那么您需要处理选定的单元格已更改或选择更改事件。



您应该这样做还了解等待状态和事件排序。处理用户输入。



DataGrid.SelectedCellsChanged Event(System.Windows.Controls)| Microsoft Docs [ ^ ]



Selector.SelectionChanged事件(System.Windows.Controls.Primitives)| Microsoft Docs [ ^ ]
If you're going to use the DataGrid as a "selector" then you need to handle the "selected cell changed" or "selection changed" event.

You should also learn about "wait states" and event sequencing. Handling user input.

DataGrid.SelectedCellsChanged Event (System.Windows.Controls) | Microsoft Docs[^]

Selector.SelectionChanged Event (System.Windows.Controls.Primitives) | Microsoft Docs[^]


这篇关于Datagrid选择不能使用WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆