从数据网格获取行的详细信息时出现问题

时间:2012-09-04 04:16:05

标签: c# .net wpf wpf-controls wpfdatagrid

我有一个使用以下内容声明的数据网格:

<DataGrid AutoGenerateColumns="True" Margin="10,174,12,35" Name="dataGridArchiveQueue" Visibility="Visible" 
AlternatingRowBackground="#01000000" 
BorderBrush="#FF688CAF" 
HorizontalGridLinesBrush="#37000000" 
VerticalGridLinesBrush="#37000000" 
CanUserAddRows="False" 
CanUserDeleteRows="False" 
IsReadOnly="True" 
SelectedItem="{Binding SelectedItemArchiveGrid}" Grid.ColumnSpan="2">
     <DataGrid.Resources>
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                                         Color="LightBlue" />
     </DataGrid.Resources>
</DataGrid>

我使用以下内容填充此数据网格:

public DataView ArchiveEvents(string status, string sourceLibrary, string destinationLibrary)
        {
            var queryString = sourceLibrary == destinationLibrary &&
                                 (!String.IsNullOrEmpty(sourceLibrary) && !String.IsNullOrEmpty(destinationLibrary))
                                    ? @"SELECT  archive_queue_id AS 'ID',
                                                [status] AS 'Status' , 
                                                source_site AS 'Source Site', 
                                                source_url AS 'Source Library', 
                                                destination_site AS 'Destination Site' ,
                                                destination_url AS 'Destination Library',
                                                CASE WHEN recurring_job = 1
                                                THEN 'Yes'
                                                ELSE 'No'
                                                END AS 'Recurring Job' ,
                                                isnull(cast(last_run AS varchar(30)), 'Never') AS 'Last Run' ,
                                                isnull(cast(created_by AS varchar(30)), 'Unknown') AS 'Created By' ,
                                                created_date AS 'Created Date' ,
                                                isnull(last_modified_by, '') AS 'Last Modified By' ,
                                                isnull(cast(last_modified_date AS varchar(30)),'') AS 'Last Modified Date'
                                        FROM    marlin.archive_queue
                                        WHERE   upper([status]) LIKE '%' + upper(@status) + '%'
                                                AND upper(source_url) LIKE '%' + upper(@source_library) + '%'
                                                or upper(destination_url) LIKE '%' + upper(@destination_library) + '%'"
                                    : @"SELECT  archive_queue_id AS 'ID',
                                                [status] AS 'Status' , 
                                                source_site AS 'Source Site', 
                                                source_url AS 'Source Library', 
                                                destination_site AS 'Destination Site' ,
                                                destination_url AS 'Destination Library',
                                                CASE WHEN recurring_job = 1
                                                THEN 'Yes'
                                                ELSE 'No'
                                                END AS 'Recurring Job' ,
                                                isnull(cast(last_run AS varchar(30)), 'Never') AS 'Last Run' ,
                                                isnull(cast(created_by AS varchar(30)), 'Unknown') AS 'Created By' ,
                                                created_date AS 'Created Date' ,
                                                isnull(last_modified_by, '') AS 'Last Modified By' ,
                                                isnull(cast(last_modified_date AS varchar(30)),'') AS 'Last Modified Date'
                                        FROM    marlin.archive_queue
                                        WHERE   upper([status]) LIKE '%' + upper(@status) + '%'
                                                AND upper(source_url) LIKE '%' + upper(@source_library) + '%'
                                                AND upper(destination_url) LIKE '%' + upper(@destination_library) + '%'";


            using (var connection = new SqlConnection(GetConnectionString()))
            {
                using (var cmd = new SqlCommand(queryString, connection))
                {
                    connection.Open();

                    cmd.Parameters.AddWithValue("status", status != "Anything" ? status : "");
                    cmd.Parameters.AddWithValue("source_library", sourceLibrary != "Anything" ? sourceLibrary : "");
                    cmd.Parameters.AddWithValue("destination_library", destinationLibrary != "Anything" ? destinationLibrary : "");


                    using (var reader = cmd.ExecuteReader())
                    {
                        var dt = new DataTable();
                        dt.Load(reader);

                        connection.Close();
                        return dt.DefaultView;
                    }
                }
            }
        }

我现在正在尝试获取所选行的内容,以便我可以通过按钮编辑或删除它。为此,我有以下属性:

public DataView SelectedItemArchiveGrid { get; set; }

但不知道如何访问此属性以获取当前所选项目的详细信息。

1 个答案:

答案 0 :(得分:1)

您需要将SelectedItemArchiveGrid的绑定属性类型从DataView更改为DataRowView,并且需要在XAML上使用RelativeSource进行绑定

<强> XAML

SelectedItem="{Binding DataContext.SelectedItemArchiveGrid, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"

<强> C#

private DataRowView _selectedItemArchiveGrid;
public DataRowView SelectedItemArchiveGrid
{
    get
    {
        return _selectedItemArchiveGrid;
    }
    set
    {
        _selectedItemArchiveGrid = value;
    }
}