简体   繁体   English

删除Datagrid C#WPF中的特定行

[英]Delete a specific row in Datagrid C# WPF

I am working with a simple application with c# What I wan't to do is that when I retrieve data from SQL Database into Datagrid, i want some of rows to deleting by selecting them and then clicking a button. 我正在用c#处理一个简单的应用程序,我不想要做的是,当我从SQL数据库中将数据检索到Datagrid中时,我希望通过选择行然后单击按钮来删除某些行。

The code i used to retrieve data is shown below: 我用来检索数据的代码如下所示:

     SqlConnection conn = new SqlConnection("Server=MEO-PC;Database= autoser; Integrated Security = true");

        conn.Open();

        SqlCommand cmd = new SqlCommand("SELECT * From evidenc", conn);
        DataTable dt = new DataTable("dtList");
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        dtg.ItemsSource = dt.DefaultView;
        SqlDataAdapter adapt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapt.Fill(ds);

        conn.Close();

Also the code i tried for deleting the specific row is: 我尝试删除特定行的代码也是:

     if (dtg.SelectedIndex >= 0)
        {
            dtg.Items.RemoveAt(dtg.SelectedIndex);
        }

The erro i get is :Operation is not valid while ItemsSource is in use. 我得到的错误是:在使用ItemsSource时,操作无效。 Access and modify elements with ItemsControl.ItemsSource instead. 而是使用ItemsControl.ItemsSource访问和修改元素。

I don't know where is the problem cause I'am new to programming. 我不知道问题出在哪里,因为我是编程新手。 Thanks to everyone 谢谢大家

You need to remove the row from the DataTable . 您需要从DataTable删除该行。 Try this: 尝试这个:

DataRowView drv = dtg.SelectedItem as DataRowView;
if(drv != null)
{
    DataView dataView = dtg.ItemsSource as DataView;
    dataView.Table.Rows.Remove(drv.Row);
}

You can't remove an item from the Items collection when you have set the ItemsSource property. 设置ItemsSource属性后,您将无法从Items集合中删除项目。

You can use IEditableCollectionView to do this. 您可以使用IEditableCollectionView来执行此操作。 You can directly change the underlying collection, if it allows changes to be made, by using the methods and properties that IEditableCollectionView exposes, regardless of the collection's type. 如果允许进行更改,则可以使用IEditableCollectionView公开的方法和属性直接更改基础集合,而与集合的类型无关。

IEditableCollectionView iecv = CollectionViewSource.GetDefaultView(theDataGrid.ItemsSource) as IEditableCollectionView;

 while (theDataGrid.SelectedIndex >= 0)
            {
                int selectedIndex = theDataGrid.SelectedIndex;
                DataGridRow dgr = theDataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;
                dgr.IsSelected = false;

                if (iecv.IsEditingItem)
                {
                    // Deleting during an edit!
                    iecv.CommitEdit();
                    iecv.RemoveAt(selectedIndex);
                }
                else
                {
                    iecv.RemoveAt(selectedIndex);
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM