C # Comment copier la valeur d'une ligne datagridview sélectionnée d'un formulaire dans une ligne datagridview d'un autre formulaire?

c# datagridview html-agility-pack winforms

Question

J'utilise le pack d'agilité HTML pour obtenir les données pour la vue en grille. Je veux simplement copier une ligne en utilisant un bouton de colonne pour l'envoyer à une grille vierge dans un autre formulaire. Il n'y a pas de liaison de données ou SQL. Après avoir rempli la grille, je reçois la vue complète de la grille de données sans appuyer sur le bouton de la colonne. Le code que j'ai est:

Formulaire 1

private void LeadsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Database DB = new Database(LeadsDataGridView.DataSource);

    var senderGrid = (DataGridView)sender;

    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
    {
        DB.Row = LeadsDataGridView.CurrentRow;
    }

    Search_Table.AcceptChanges();

    LeadsDataGridView.DataSource = Search_Table;
}

Formulaire 2

private void LeadsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Database DB = new Database(LeadsDataGridView.DataSource);

    var senderGrid = (DataGridView)sender;

    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
    {
        DB.Row = LeadsDataGridView.CurrentRow;
    }

    Search_Table.AcceptChanges();

    LeadsDataGridView.DataSource = Search_Table;
}

Réponse acceptée

Voici un exemple minimal qui, espérons-le, vous mettra sur la bonne voie.

MainWindow.xaml

<Window x:Class="PassingValuesFromFormToForm_45425412.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:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="61,60,0,0" VerticalAlignment="Top" Width="259"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="29,13,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs

<Window x:Class="PassingValuesFromFormToForm_45425412.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:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="61,60,0,0" VerticalAlignment="Top" Width="259"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="29,13,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

Window1.xaml

<Window x:Class="PassingValuesFromFormToForm_45425412.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:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="61,60,0,0" VerticalAlignment="Top" Width="259"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="29,13,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

Window1.xaml.cs

<Window x:Class="PassingValuesFromFormToForm_45425412.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:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="61,60,0,0" VerticalAlignment="Top" Width="259"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="29,13,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

Voici une mise à jour pour WinForms. Encore une fois, ceci est un exemple minimal. Pour les besoins de cet exemple, je n’ai rien fait dans Form Designer. Tout se fait par code derrière.

Form1.cs

<Window x:Class="PassingValuesFromFormToForm_45425412.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:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="61,60,0,0" VerticalAlignment="Top" Width="259"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="29,13,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

Form2.cs

<Window x:Class="PassingValuesFromFormToForm_45425412.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:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="61,60,0,0" VerticalAlignment="Top" Width="259"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="29,13,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>



Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi