select 从 datagridview 到其他 datagridview 中的单元格的一列的所有值

select all values of one column from datagridview to a cell in other datagridview

大家好,如果可能的话,我想请你帮忙:

在第一个表单中,我有一个 DataGridViewTextBoxes,当我单击按钮 Télécharger PDF 没有 selecting 行它 select 全部自动:

表格 1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
     private void button1_Click(object sender, EventArgs e)
    {
         Form2 f2 = new Form2();

            string firstcol = textBox3.Text;
            string secondcol = textBox1.Text;
            string thirdtcol = dataGridView1.CurrentRow.Cells[0].ToString();
            string fourcol = dataGridView1.CurrentRow.Cells[4].ToString();

   //thirdcole and fourcol are not working even for 1 row

            string[] rows = { firstcol, secondcol, thirdtcol, fourcol };

            f2.dataGridView1.Rows.Add(rows);

            f2.Show();

            this.Hide();
        
    }

表格 2 :

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void A_commande_etat_Load(object sender, EventArgs e)
    {

    }
}

Form2 中添加一个方法,该方法从 Form1 获取 dataGridView1 并循环遍历 deciered 列并创建所需的 concatenated/summed 值,然后将其添加为Form2dataGridView 的新行。

Form1

public partial class Form1 : Form
    {
        Form2 f2; //Don't create a new f2 object each time button is pressed
        public Form1()
        {
            InitializeComponent();

            f2 = new Form2();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f2.Show();
            //Send the datagridview of Form1 to Form 2, let Form2 do all the logic
            f2.loadColumns(this.dataGridView1); 
        }
    }

Form2

using System.Text;

public partial class Form2 : Form
    {
      
        public Form2()
        {
            InitializeComponent();
        }

        public  void loadColumns(DataGridView dataGridView)
        {
            StringBuilder buildArticleString = new StringBuilder();

            int articleColIndex = 0; //Your article col index in Form1
            int totalColIndex = 4; //Your total col index in Form1
            int totalSum = 0;
            
            foreach(DataGridViewRow currentRow in dataGridView.Rows)
            {
                if (currentRow.Cells[articleColIndex].Value == null || currentRow.Cells[totalColIndex].Value == null)
                    continue;

                //Retrive cell value at article column for current row, append to stringbuilder
                buildArticleString.Append(currentRow.Cells[articleColIndex].Value.ToString());
                buildArticleString.Append(" + ");


                int currentTotalValue = 0;
                //Checks if value in cell can be converted to int, skips if not
                if (Int32.TryParse(currentRow.Cells[totalColIndex].Value.ToString(), out currentTotalValue))
                {
                    totalSum += currentTotalValue;
                }
            }

            buildArticleString.Remove(buildArticleString.Length - 3, 3); //Remove trailing " + ";

            //Make sure this matches your columns in Form2
            string[] rowValues = new string[] {"", "", buildArticleString.ToString(), totalSum.ToString(), "", "" }; 
            dataGridView1.Rows.Add(rowValues); //Add new row to Form2
        }
    }