Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys, Need Help
I have two form one is to enter information and the second one is to show the data and transfer to first one. The second form has many buttons of Goods name when I choose one of button the data related to that button and transfer to first form that has datagridview that show the info of Goods.

when I run the program and the transfer is working perfect but when I close the first form and open again when I open second form and transfer data
**No row can be added to a DataGridView control that does not have columns. Columns must be added first**

Please I need help

What I have tried:

C#
private void GoodsButton_Click(object sender, EventArgs e)
        {
            if (Function.DataTable("SELECT * FROM TABLENAME").Rows.Count > 0)
            {
                foreach (DataRow DataRow in DataTable.Rows)
                {
                    Button Button = new Button();
                    Button.Tag = DataRow["C"].ToString();
                    Button.Text = DataRow["N"].ToString();
                    Button.Font = new Font("Times New Roman", 10f, FontStyle.Bold);
                    Button.UseVisualStyleBackColor = false;
                    Button.ForeColor = Color.White;
                    Button.BackColor = Color.Gray;
                    Button.Margin = new Padding(4);
                    Button.Top = i * 25;
                    Button.Size = new Size(200, 50);
                    Button.Click += GetItemInfo_Click;
                    this.FLPanel.Controls.Add(Button);
                }
            }
        }


C#
private void GetItemInfo_Click(object sender, EventArgs e)
        {
            Button ButtonSender = sender as Button;
            if (ButtonSender != null)
            {
                Function.DataTable("SELECT * FROM TABLENAME WHERE C='" + ButtonSender.Tag + "'");
                foreach (DataRow DataRow in DataTable.Rows)
                {
                        Form1.GETINFOFORM.DataGridView.Rows.Add(Info1, Info2, ..., Infon);
                }
            }
        }
Posted
Updated 28-Feb-21 19:35pm
v2

Don't do it like that.
Instead, let one form tell the other that data is available, and let it deal with it's own controls.

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
Comments
Karam Ibrahim 28-Feb-21 12:40pm    
Dear OriginalGriff,

I have MainForm (isMidForm) and Form1 open inside MainForm and I open Form2 via Form1,
in this situation I should use the last one transfer between Child to Child.
Maciej Los 28-Feb-21 17:06pm    
5ed!
Karam Ibrahim 19-Jun-21 0:24am    
Thanks bro, you very kind helping me to solve many problems. I really appreciate that.
OriginalGriff 19-Jun-21 1:48am    
You're welcome!
"when I close the first form and open again when I open second form and transfer data"

The simple solution may be to not close the first form, but use 'Hide so it stays active, clearing whatever is in the Form as necessary.

I say "may" because your question and code do not give me enough information to fully understand what you are doing.

A DataGridview is a strongly typed collection of Columns: they must be defined at design-time, or added in run-time code before you add Rows. CellType and CellTemplate should be set to avoid errors: [^]

In general, I would avoid creating a bunch of Controls in run-time code; I would try to re-use an existing Form with a TableLayoutPanel populated with the Controls
 
Share this answer
 
Comments
Karam Ibrahim 11-Mar-21 3:56am    
Dear billWoodruff,

I had DatagridView with columns that had been added at design.
also I had table with Items (Goods) in SQL that connect to my project,
I had two forms both inside MidForm (Main Form).
The First one (Form1) had datagridview and button when I press the button the second forms (Form2) open and via the (code 1) that I mention it bring the data from the table that I had created in SQL and create Buttons on it.
So, When I press the buttons had been created, it transfer data for that Items (Good) to datagridview at (Form1) that show in (code 2)
Karam Ibrahim 19-Jun-21 0:26am    
Thanks dear, I solved my problem, dear OriginalGriff gave me the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900