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

Time:02-24

Hi everyone please I want you're help if it's possible :

In the first Form I have a DataGridView and TextBoxes that send data to another DataGridView in the second form when I click the button Télécharger PDF without selecting rows it select all automatically:

  • The problem is that I want to add all value of the first column for example "Arduino scotch" in cell of the second DataGridView.
  • I think it will be the same way for the total I want to add the Sum in one cell of the form2.

Form1

Form2

Form1:

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

Form2 :

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

    private void A_commande_etat_Load(object sender, EventArgs e)
    {

    }
}

CodePudding user response:

In Form2 add a method that takes the dataGridView1 from Form1 and loops through the deciered columns and creates the wanted concatenated/summed values and then add it as a new row for the dataGridView in Form2.

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
        }
    }
  • Related