2017-07-31 27 views
-1

グリッドビューのデータを取得するために、html agility packを使用しています。列ボタンを使用して行をコピーして、別のフォーム上の空白のグリッド表示に送信するだけです。データバインディングやSQLはありません。グリッドにデータを入力した後、列のボタンを押さずにデータグリッドビュー全体が表示されます。私が持っているコードは次のとおりです。1C#選択したDataGridview行の値をあるフォームから別のフォームのDataGridviewの行にコピーするにはどうすればよいですか?

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; 
} 

フォーム2

public Database(object DataSource) 
{ 
    InitializeComponent(); 
    InitDBTable(); 
    DB_GridView.DataSource = DataSource; 
} 
+0

[この方法を使用して正常に動作する必要があり(https://stackoverflow.com/questions/43923548/passing- value-from-multiple-textbox-to-string-on-another-form-or-call-the-val/43924222#43924222)。そのSOの投稿は文字列を渡すことですが、メソッドが渡す予定のパラメータ型を期待している限り、何かを渡すことができます。 –

+0

datagridview!= wpf datagrid!= gridview。どちらがあなたを使っていますか? – ASh

+0

多分、その広すぎる質問。各列の値が単純に入力された場合、どのように単一のdatagridviewの行の値を別のdatagridviewにコピーしますか? –

答えて

1

フォームはここでうまくいけば、あなたの方法であなたを取得する最小限の例です。

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

using System.Collections.Generic; 
using System.Windows; 

namespace PassingValuesFromFormToForm_45425412 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     List<dgvEntry> dgvList = new List<dgvEntry>(); 
     public MainWindow() 
     { 
      InitializeComponent(); 
      dgvList.Add(new PassingValuesFromFormToForm_45425412.dgvEntry { col1 = "blah blah", col2 = "blehbleh" }); 
      dataGrid.AutoGenerateColumns = true; 
      dataGrid.ItemsSource = dgvList; 
     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      Window1 win1 = new Window1((dgvEntry)dataGrid.Items[0]); 
      win1.Show(); 
     } 
    } 

    public class dgvEntry { 
     public string col1 { get; set; } 
     public string col2 { get; set; } 
    } 
} 

Window1.xaml

<Window x:Class="PassingValuesFromFormToForm_45425412.Window1" 
     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="Window1" Height="300" Width="300"> 
    <Grid> 
     <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="23,39,0,0" VerticalAlignment="Top" Width="181"/> 

    </Grid> 
</Window> 

Window1.xaml.cs

using System.Collections.Generic; 
using System.Windows; 

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

     public Window1(dgvEntry incomingItem) 
     { 
      InitializeComponent(); 
      dataGrid.AutoGenerateColumns = true; 
      dataGrid.ItemsSource = new List<dgvEntry> { incomingItem }; 
     } 
    } 
} 

ここにWinFormsのアップデートがあります。この場合も、これは最小限の例です。このサンプルの目的では、私はフォームデザイナーで何もしませんでした。 すべてはコードの後ろで行われます。

のForm1.cs

using System.ComponentModel; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 

     DataGridView dgv = new DataGridView(); 
     BindingList<dgvitem> itemsList = new BindingList<dgvitem>(); 
     bool SendAsRow = true;//just a variable to trigger the proper method in 'Dgv_CellContentClick' 
     Button independantButton = new Button(); 

     public Form1() 
     { 
      InitializeComponent(); 
      InitializeTheDGV(); 
      AddTheButton(); 
      itemsList.Add(new dgvitem { JustaTextField = "aksldjf sadfjasifuqw adsfasf" }); 
      itemsList.Add(new dgvitem { JustaTextField = "qwerioqu aisdfnvmz, oaa"}); 
     } 

     private void InitializeTheDGV() 
     { 
      dgv.Location = new Point(this.Location.X + 5, this.Location.Y + 5); 
      dgv.DataSource = itemsList; 
      dgv.AutoGenerateColumns = false; 
      this.Controls.Add(dgv); 
      dgv.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "My col header", Name="mycol1" }); 
      dgv.Columns.Add(new DataGridViewButtonColumn() { HeaderText = "click in this column", Name = "mycol2" }); 
      dgv.Columns["mycol1"].DataPropertyName = "JustaTextField"; 
      dgv.CellContentClick += Dgv_CellContentClick; 
     } 

     private void Dgv_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if (!(sender is DataGridView)) 
      { 
       return; 
      } 

      /* 
      * Experiment and Pick your poison 
      */ 
      if (SendAsRow) 
      { 
       Form2 f2r = new Form2(dgv.Rows[e.RowIndex]); 
       f2r.Show(); 
      } 
      else 
      { 
       Form2 f2 = new Form2((string)dgv.Rows[e.RowIndex].Cells[0].FormattedValue); 
       f2.Show(); 
      } 
      /**/ 
     } 


     private void AddTheButton() 
     { 
      independantButton.Location = new Point(this.Location.X + 5, this.Location.Y + dgv2.Height + 15); 
      independantButton.Click += IndependantButton_Click; 
      this.Controls.Add(independantButton); 
     } 

     private void IndependantButton_Click(object sender, System.EventArgs e) 
     { 
      /* 
      * Experiment and Pick your poison 
      */ 
      if (SendAsRow) 
      { 
       Form2 f2r = new Form2(dgv.SelectedRows[0]); 
       f2r.Show(); 
      } 
      else 
      { 
       Form2 f2 = new Form2((string)dgv.SelectedRows[0].Cells[0].FormattedValue); 
       f2.Show(); 
      } 
      /**/ 

     } 
    } 


    public class dgvitem 
    { 
     public string JustaTextField { get; set; } 
    } 
} 

にForm2.cs

using System.ComponentModel; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form2 : Form 
    { 
     DataGridView dgv2 = new DataGridView(); 
     BindingList<dgv2item> dgv2list = new BindingList<dgv2item>(); 

     //this is the 'default' constructor which takes no argument 
     public Form2() 
     { 
      InitializeComponent(); 
      MakeTheGrid(); 
     } 

     //this form constructor takes a String parameter so you can pass only a string 
     public Form2(string incomingText) 
     { 
      InitializeComponent(); 
      MakeTheGrid(); 
      dgv2list.Add(new dgv2item { coolBeans = incomingText });//add the incoming String to the itemList, which will in-turn update the DataGridView 
     } 


     //this form constructor takes a DataGridViewRow parameter so you can pass the whole row 
     public Form2(DataGridViewRow incomingRow) 
     { 
      InitializeComponent(); 
      MakeTheGrid(); 
      dgv2list.Add(new dgv2item { coolBeans = (string)incomingRow.Cells[0].FormattedValue});//add the value of the cell you want out of the row to the itemlist, which will in-turn update the DataGridView 
     } 


     private void MakeTheGrid() 
     { 
      dgv2.Location = new Point(this.Location.X + 15, this.Location.Y + 15);//it has to go somewhere... 
      dgv2.AutoGenerateColumns = true; 
      dgv2.DataSource = dgv2list;//define where to find the data 
      this.Controls.Add(dgv2);//add it to the form 
     } 
    } 


    public class dgv2item 
    { 
     public string coolBeans { get; set; } 
    } 
} 
+0

私はあなたがblaze_125をリストした例を好んだ。それは私の問題の要点だと思うものの背後にある論理を見るのを助けました。私のプログラムのコードは、私のコード内で何が起こっているのか分からないというあなたの例とは大きく異なっています。私は使用しようとしました:DB_Table.Rows.Add(Search_Table.Rows [e.RowIndex]);しかし、行を収集する手段として、「この行はすでに別の表に属しています」というエラーが表示されます。DataTableをDataSourceとともに使用しています。私はいくつかの研究を続け、解決策を見つけようとします。私が何かを横切った場合、私はここにそれを掲示するでしょう。 –

+0

あなたのオープニングポストで@ASh質問に従ってください。タグとあなたの投稿の内容は多少矛盾しています。私はあなたにWPFの例を挙げましたが、実際にはWPFではなくフォームで作業しているような気がします。あなたが実際にフォームで作業しているなら、別の答えを書きます。 WPFで作業しているかどうかを調べるには、Visual Studioのソリューションエクスプローラを参照してください。ソリューションエクスプローラに ".xaml"ファイルがリストされている場合は、WPFプロジェクトに参加します。そのファイル拡張子がリストされていない場合は、Formsで作業している可能性が高いです。 –

+0

あなたは正しいですblaze_125、私は誤解をお詫び申し上げます。間違ったタグを使用しました。あなたは実際にはコードは今私にとってはるかに理にかなっています。 AShの質問に答えるために、私はDataTableでDataGridViewを使用していて、DataPileクラス内で "set"変数を使用しています。私は、基準の検索を実行した後、DGV内のデータを生成しています。コピーボタンは列としてDGV内にあります。フォームを開くためのボタンが別にあります。選択モードにFullRowSelectを使用しています。 –

関連する問題