Global variables using in other forms

Zeeshan Haider 21 Reputation points
2020-08-21T07:00:59.377+00:00

I am making a software i want a global variable so that i can use across one form to other so basically i want to call roll no. that is written in an event datagridview row header event: Roll_No = Convert.ToInt32(bunifuDataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()); this is the code and i want this Roll_no with its value on Form2.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,826 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,691 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 40,741 Reputation points
    2020-08-21T09:35:18.277+00:00

    You tagged you post with SQL Server; what's the relation to?

    In OO world you can use a Singleton to get a kind of globale variable: Implementing the Singleton Pattern in C#

    0 comments No comments

  2. Daniel Zhang-MSFT 9,611 Reputation points
    2020-08-24T05:23:58.037+00:00

    Hi Zeeshan Haider,

    >that is written in an event datagridview row header event

    What specific event did you execute it? And I made a test in dataGridView CellContentClick event you can refer to.
    Form1.cs:

     //Define global variables
        public static int Roll_No;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
                Roll_No = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());        
        }
    

    Form2.cs

     //Get value of Roll_No
        int value = Form1.Roll_No;
    

    Best Regards,
    Daniel Zhang

    0 comments No comments