Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day everyone, please i am trying to count the number of records of a datagridview(dtuserpro) in a usercontrol profile and pass it to a label in another usercontrol dashboard but i get no value.

What I have tried:

C#
public partial class userprofile : UserControl
{
   private string count1;
   public string Countt
   {
      get { return count1; }
      set { count1 = value; }
   }

   public userprofile()
   {
      InitializeComponent();
   }

   SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\odt\Documents\Loanapp.mdf;Integrated Security=True;Connect Timeout=30");
   DataTable dt = new DataTable();

   private void profile_Load(object sender, EventArgs e)
   {
      int count = ((DataTable)this.dtuserpro.DataSource).Rows.Count;
      lblcount.Text = count.ToString();
      Countt = lblcount.Text;
   }

   private void dashboard_Load(object sender, EventArgs e)
   {
      userprofile up = new userprofile();
      metroLabel1.Text = up.Countt;
   }
Posted
Updated 26-May-19 12:09pm
v3

1 solution

Controls do not know about each other, and nor should they.

Your form is what should be passing data between controls. Your user control should expose the data it needs to as a property and also expose an event your form can subscribe to to be notified that the data in the property changed.

When the property value changes, the property setting can raise the event. The form gets notified and can chose to pick up the data from the property and send the data to the other control.

It's up to the form to determine what to do with the data, not the control exposing the data.
 
Share this answer
 
Comments
BillWoodruff 26-May-19 0:46am    
+5 Really like the model of event-subscription by the container !

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



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