I work with VisualStudio19, C# and WPF. I have a method to copy the selected Datarow from Table one, to Table two with a button_click, but now I want to copy the row when I double click it. I will use the same method as button_click for that so I need only an Eventhandler which start my method on doubleclick
The Tables:
public MainWindow()
{
InitializeComponent();
//First Table for all values
tableAllVar.Columns.Add("Parameter", typeof(string));
tableAllVar.Columns.Add("Adresse", typeof(string));
tableAllVar.Columns.Add("Groesse", typeof(string));
dgTable1.ItemsSource = tableAllVar.DefaultView;
//second Table for selected values
tableSelectedVar.Columns.Add("Parameter", typeof(string));
tableSelectedVar.Columns.Add("Adresse", typeof(string));
tableSelectedVar.Columns.Add("Groesse", typeof(string));
dgTable2.ItemsSource = tableSelectedVar.DefaultView;
}
The two Tables left ist the table I want to copy the row and right is the aimtable for the row.
My method to copy the selected Datarow with a Button
private void ButtonAddVar_Click(object sender, RoutedEventArgs e)
{
string[] daten = new string[3];
if (dgTable1.SelectedItem != null)
{
DataRowView datarow = (DataRowView)dgTable1.SelectedItem;
daten[0] = (string)datarow.Row.ItemArray[0];
daten[1] = (string)datarow.Row.ItemArray[1];
daten[2] = (string)datarow.Row.ItemArray[2];
bool rowExists = ContainDataRowInDataTable(tableSelectedVar, datarow.Row);
if (rowExists == false)
{
tableSelectedVar.Rows.Add(daten[0], daten[1], daten[2]);
//anzahl++;
}
else
{
MessageBox.Show("Die Variable ist bereits Teil der Variablenüberwachung", null, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
MessageBox.Show("Bitte eine Variable aus der linken Liste mit der Maus auswählen",null,MessageBoxButton.OK,MessageBoxImage.Information);
}
And here the way i create my Datagrid in the xaml
<DataGrid Name="dgTable1" HorizontalAlignment="Left" Height="180" Margin="5,30,0,0" VerticalAlignment="Top" Width="150" AutoGenerateColumns="False" FontSize="6" ColumnHeaderHeight="15" IsReadOnly="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Parameter" Binding="{Binding Path=Parameter}" Width="auto" />
<DataGridTextColumn Header="Adresse" Binding="{Binding Path=Adresse}" Width="auto"/>
<DataGridTextColumn Header="Größe" Binding="{Binding Path=Größe}" Width="auto"/>
</DataGrid.Columns>
</DataGrid>
I saw that the DataGrid has an Eventhandler for Double_Click but i don't know how i can use them for the DataTable? When i can't use them for Table how can i make a new Eventhandler for that event?
As you know DataGrid has a Double_Click Eventhandler for copy one row you can use selected row property.
private void dgTable1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
string[] daten = new string[3];
if (dgTable1.SelectedItem != null)
{
DataRowView datarow = (DataRowView)dgTable1.SelectedItem;
daten[0] = (string)datarow.Row.ItemArray[0];
daten[1] = (string)datarow.Row.ItemArray[1];
daten[2] = (string)datarow.Row.ItemArray[2];
}
else
MessageBox.Show("please select again");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.