【问题标题】:Get an entire row from DataGrid从 DataGrid 获取整行
【发布时间】:2014-03-04 17:12:19
【问题描述】:

我将解释我的问题:我有一个 DataGrid,它从一个包含来自 Project 2010 服务器的信息的 DataSet 填充。我想从我的 DataGrid 中选择一个或多个行,当用户单击一个按钮时,这些行将从原始 DataGrid 中删除,并发送到第二个。

我已经搜索了很多,但我不知道如何做到这一点!也许使用 DataGrids 不是一个好主意或 idk,但我对 WPF 很陌生,这对我来说似乎是最好的方法!

因此,如果您能给我一个提示,将数据从 DataGrid 移动到另一个,或者任何其他方法来做到这一点,那就太好了!

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    是的,一开始处理数据网格有时会很痛苦,但我还是更喜欢使用它。

    删除单行并不难,我通常从数据库中删除然后重新加载数据网格,也许这会对你有所帮助:

    将此添加到 xaml 中的数据网格:

    <DataGrid AutoGenerateColumns="False" CommandManager.PreviewExecuted="dataGrid1_PreviewDeleteCommandHandler" Name="dataGrid1" />
    

    然后在代码中创建方法如下:

    private void dataGrid1_PreviewDeleteCommandHandler(object sender, ExecutedRoutedEventArgs e)
    {
        if (e.Command == DataGrid.DeleteCommand)
        {
            //Get the row data before deleting 
            DataGridRow selectedRow = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(dataGrid1.SelectedIndex);
            DataRowView rv = (DataRowView)selectedRow.Item;
    
            // First cell in the row is usually the ID of the row (you can modify that in your database)
            string recNoToDelete = rv.Row[0].ToString();
            if (recNoToDelete == "")
            {
                e.Handled = false;
                return;
            }
            if (MessageBox.Show("Are you sure to delete this row?","", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
    
                SqlConnection Conn = new SqlConnection(connectionString);
                SqlCommand Comm = new SqlCommand("", Conn);
    
                // Delete
                Comm = new SqlCommand("", Conn);
                Comm.CommandText = "Delete from Table where field=@field";
                Comm.Parameters.AddWithValue("@field", recNoToDelete);
                Conn.Open();
                Comm.ExecuteNonQuery();
                Conn.Close();
    
                // Reload all the data again in the datagrid after deletion of that record
                ReloadData();
                }
                else
                {
                    //record is not valid or has been deleted or manipulated
                    //cancel the delete operation
                    e.Handled = true;
                    return;
                }
        }
    }
    

    但是,当用户单击该行并按“删除”键时,这将起作用。至于删除多个列,我仍然没有弄清楚,但是当我这样做时,我会更新答案。

    我希望这至少可以帮助你开始。

    【讨论】:

    • 嗯,也许我对问题的解释很糟糕。我不想从数据库中删除任何内容。事实上,被选中的行将从第一个 Datagrid 中消失,并将被放入另一个 Datagrid。我不知道这是否可能,或者我是否需要使用 Datagrids 以外的其他东西,就像我说我对 WPF 相当陌生。如果我的问题无法理解,请原谅我的英语不完美。
    【解决方案2】:

    关于如何删除多行;

    首先使选择模式在dataGrid上扩展

    SelectionMode="Extended"
    

    然后 selectedItems 属性将返回选定对象的列表(取决于您绑定到 DataGrid 的内容)

    迭代列表并从数据库中删除项目并刷新。 例如:

     For Each item As <object> In Me.MyDataGrid.SelectedItems
         --delete record--
     Next
    

    【讨论】:

      【解决方案3】:

      我有我的答案:使用 DataGrid 是不好的方法,我必须直接使用 DataSet!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        • 2019-12-21
        • 2012-11-11
        相关资源
        最近更新 更多