Question transfer content from Form2's TextBox to Form1's DataGridView

titojd

Well-known member
Joined
Oct 12, 2018
Messages
63
Programming Experience
1-3
Hello guys, here I am again asking for support.
I have a Form1 that contains a DataGrid and a 'Call' Button that calls Form2, on Form2 I have 2 TextBox and an 'OK' Button

I need to transfer the contents of the TextBox from Form2 to the Grid of Form1 when clicking the OK Button,

I did a lot of research and didn't find anything that fits my question, could someone help me with this?
a hug.
 
Firstly, please post your questions in the most appropriate forum that you can. Based on the content, this appears to be specific to Windows Forms, so it is obviously not a C# general question. I have moved this thread to the most appropriate form.

Secondly, please be precise with your terminology. There is a WinForms control named DataGrid but it has been obsolete for almost two decades, so it seems unlikely that you are using it. Are you actually using a DataGridView? If so, please specify that. If we provide advice specific to a DataGrid then it likely won't work for you in that case. If you really are using a DataGrid then I take back everything I said, but I do have to ask why?
 
One thing you need to learn about programming is to start general and then get specific. Forms are objects, just like any other. How do you usually get data out of an object? You get a property or you call a method. That's what you do here. Your second form should expose the data via a property and then the first form gets the data from that property and does whatever you want with it. That might look something like this in the first form:
C#:
using (var dialogue = new Form2())
{
    if (dialogue.ShowDialog() == DialogResult.OK)
    {
        var data = dialogue.Data;
        
        // Use data here.
    }
}
I'll leave it to you to implement the property. It should be read-only unless you need to set an initial value when creating the dialogue and it should just return the text directly from the TextBox.
 
Hello, Thanks for replying, I will be more careful when using "DataGrid", I am using DataGridView,
My question is not very objective, so I will rephrase it here.

I have Form1 with a Button that Calls Form2,
in Form2 I have a TextBox array with 5 indices and an OK Button.

I can do it from Form1 to Form2 with textBox, but the other way around I can't do it, which would be from form2 to form1 and with DataGridView,

I need to pass the values from the Form2 array to the DataGridView that is on Form1.

I tried the following way, but I was unsuccessful.

C#:
public Form1(string valor)
        {
            InitializeComponent();
            dgResultado.Rows.Add(valor);         
        }

in Form2
C#:
private TextBox[] Aposta = new TextBox[5];

 private void GeraControles()
        {
            int i, esquerda = 50, topo = 60;
            for (i = 0; i < 5; i++)
            {
                Aposta[i] = new TextBox();
                Aposta[i].Left = esquerda;
                Aposta[i].Top = topo;
                Aposta[i].Anchor = new AnchorStyles();
                Aposta[i].BackColor = Color.Gray;
                Aposta[i].ForeColor = Color.White;
                Aposta[i].Size = new Size(45, 30);
                Aposta[i].Font = new Font("", 15, FontStyle.Bold);
                Aposta[i].TabIndex = 2;
                Aposta[i].MaxLength = 2;
                Aposta[i].BorderStyle = BorderStyle.FixedSingle;
                Aposta[i].KeyPress += new KeyPressEventHandler(keypressed);
                this.Controls.Add(Aposta[i]);
                esquerda += (Aposta[i].Width + 5);
                Aposta[i].Tag = i;
            }
        }

private void btnOK_Click(object sender, EventArgs e)
        {           
            frmApostas form = new frmApostas(Aposta.ToString());
            if (form.ShowDialog() == DialogResult.OK)
            {
                AddRows(form.ToString());
            }
            //Se não retornar OK (um botão cancelar por exemplo) não faz nada
        }
 
I've already told you what to do and you've just ignored it. Form2 does not and should not know that Form1 even exists. As I have already told you, Form2 simply exposes the data, via a property or method. When the dialogue closes, Form1 gets the data from that property or method and then it populates its own grid. What happens to the data is no concern of Form2.
 
Hi @jmcilhinney, the use of dynamic TextBoxes is because there is already one in the project, but it can be in the designer too, no problem,
I thought it would be better to pass the data to the other form
 
You would generally only create the controls in code if there was some aspect of them that you didn't know until run time; usually the number of them. If you know everything about them at design time, there's no reason not to create them in the designer.
 
I've already told you what to do and you've just ignored it. Form2 does not and should not know that Form1 even exists. As I have already told you, Form2 simply exposes the data, via a property or method. When the dialogue closes, Form1 gets the data from that property or method and then it populates its own grid. What happens to the data is no concern of Form2.

@jmcilhinney,
I unfortunately don't know how to do this, I'm trying to understand the logic, but thank you for your explanation.
Thank you very much. hug
 
I've provided a code example of displaying a dialogue and getting a property value from that dialogue after it has closed. I would hope that you would already understand how to do those things but you have that example anyway. What is the specific issue now? Do you not know how to define a property? That's pretty fundamental to .NET programming and pretty much every beginners tutorial will cover that so you really ought to know. Here is the relevant page from a tutorial I regularly recommend. If that's not the problem, what is?

I don't know whether the code you posted in post #4 was an attempt to implement my instructions from post #3 but it's not doing what I said. I said that that code goes in Form1, not in Form2. There's also no property being used there. Calling ToString on a form isn't going to help because that's not going to return any custom data. I suggest that you go back and read post #3 again and try to do what it actually says to do. If you don't understand something then be specific about that, so we know what to address.
 

Latest posts

Back
Top Bottom