Home > database >  the object of type A cannot be converted to type B
the object of type A cannot be converted to type B

Time:02-05

I am making a programme with which I want to start Windows services. I search all services for a name (for example "VMware") and write them in a list. Now I want to start a service. I then get the error message "The object of type "WpfApp1.myService" cannot be converted to type "System.Data.DataRowView"."

When I start all services it works. Here is my code (XAML):

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="650" Width="800">
<StackPanel Orientation="Vertical" >
    
    <DataGrid Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="False"Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"RowBackground="#fff" FontWeight="Bold" Foreground="#525252"ScrollViewer.CanContentScroll="True" Height="390" MaxHeight="390"AlternatingRowBackground="#f2f2f2" BorderBrush="#000" BorderThickness="1"ScrollViewer.HorizontalScrollBarVisibility="Visible"ScrollViewer.VerticalScrollBarVisibility="Auto">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Path='ProductId'}" IsReadOnly="True" />
            <DataGridTextColumn Header="Product" Binding="{Binding Path='Product'}" IsReadOnly="True" />
            <DataGridTextColumn Header="Category" Binding="{Binding Path='Category'}" IsReadOnly="True" />
            <DataGridTextColumn Header="Price" Binding="{Binding Path='Price'}" IsReadOnly="True" />
            <DataGridTemplateColumn Header="start">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Name="btnStart" Content="start" Click="btnStrt_Click" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="start all services" Click="Button_Click"/>
</StackPanel>

And here the CS:

using System;
using System.Collections.ObjectModel;
using System.Data;
using System.ServiceProcess;
using System.Windows;
using System.Windows.Controls;



namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                ObservableCollection<myService> items = new ObservableCollection<myService>();

                ServiceController[] scServices;
                scServices = ServiceController.GetServices();


                string meineServices = "VMware";

                foreach (ServiceController scTemp in scServices)
                {
                    if (scTemp.DisplayName.Contains(meineServices))
                    {
                        items.Add(new myService() { ProductId = "1", Product = scTemp.ServiceName, Category = scTemp.DisplayName, Price = scTemp.Status.ToString() });
                    }
                }
                dataGrid.ItemsSource = items;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void btnStrt_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRowView dataRowView = (DataRowView)((Button)e.Source).DataContext;
                ObservableCollection<myService> items = new ObservableCollection<myService>();

                ServiceController[] scServices;
                scServices = ServiceController.GetServices();


                string meineServices = dataRowView[1].ToString();

                foreach (ServiceController scTemp in scServices)
                {
                    if (scTemp.DisplayName.Equals(meineServices))
                    {
                        scTemp.Start();
                    }
                }

                MessageBox.Show("Dienst gestartet");

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

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ObservableCollection<myService> items = new ObservableCollection<myService>();

                ServiceController[] scServices;
                scServices = ServiceController.GetServices();


                string meineServices = "VMware";

                foreach (ServiceController scTemp in scServices)
                {
                    if (scTemp.DisplayName.Contains(meineServices))
                    {
                        scTemp.Start();
                    }
                }


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

    }
    public class myService
    {
        public string ProductId { get; set; }
        public string Product { get; set; }
        public string Category { get; set; }
        public string Price { get; set; }


    }
}

Thanks for your help!

CodePudding user response:

I solved it like this:

<Button Name="btnStart" Content="Starten" CommandParameter="{Binding Path='Dienstname'}" Click="btnStarten_Click" />

and in the CS:

object Row_Anzeigename = ((Button)sender).CommandParameter;
string meineServices = Row_Anzeigename.ToString();

Thank you for pushing me in the right direction.

  • Related