Search code examples
c#winforms

DataGridView in Windows Forms not showing any data - tried a lot of answers of previous questions


I'm using latest of all version for VS and .NET SDK.

UPDATE: this is a course assignment, and they do not want a SQL database used. I was told we just bind the data.

I've been stumped for weeks. I have looked at other questions and either they helped a little, by fixing how I had the add function or they're just not the same set up.

I have a datagridview on a main screen then a product form that opens and a user can add the product's info. When they click save it should add a row to the main page's datagrid. I can see when debugging all the values pass through but I'm not sure why it is not displaying any row on the datagridview itself.

Main.Designer.cs:

private void InitializeComponent()
{
    productGridView = new DataGridView();

    productBindingSource = new BindingSource(components);
    productBindingSource.DataSource = typeof(Product);

    productGridView.DataSource = productBindingSource;
}

public DataGridView productGridView;
public BindingSource productBindingSource;

public void AddRows(int id, string name, float price, int instock, int min, int max)
{
     this.productBindingSource.Add(new Product { ProductId = id, ProductName = name, 
     ProductPrice = price, InStock = instock, Max = max, Min = min });
     
     this.productGridView.Refresh();
}

The product form file - I only added relevant method w/in that file:

public void Save_Click(object sender, EventArgs e)
{
      int _id = Convert.ToInt32(Id.Text);
      string _name = ProductName.Text;
      float _price = float.Parse(ProductPrice.Text);
      int _qty = Convert.ToInt32(InStock.Text);
      int _min = Convert.ToInt32(Min.Text);
      int _max = Convert.ToInt32(Max.Text);

      Main _main = new(); 

      _main.AddRows(_id, _name, _price, _qty, _min, _max);
      Close();
}

And this is the product class file - Product.cs:

public class Product
{ 
     public int ProductId { get; set; }
     public string? ProductName { get; set; }
     public double ProductPrice { get; set; }
     public int InStock { get; set; }
     public int Min { get; set; }
     public int Max { get; set; }
}
 

enter image description here


Solution

  • You could declare the main form in a previous class, like in a login form or in the Program class (Program.cs), as it is the real main class. Like:

    public static Main mainform =  new Main();
    
    namespace YourProjectName
    {
        static class Program
        {
            public static Main mainForm = new Main();
    
            static void Main()
            {
                Application.Run(mainForm);
            }
        }
    }
    

    When you are in the ProductForm, you will be able to use that same shown main as

    Program.mainForm.AddRows(_id, _name, _price, _qty, _min, _max) 
    

    in your void_save click function.

    What you can do to solve this is to have a static main form, when an instance is static you can work on it like REALTIME, what I mean is, when ever you are interacting in another form, it doesn't matter if this form is a child form, once the target form is static, you will be able to access it and also its public functions.

    I had a similar problem, as I wanted to perform some changes in my project to work like RealTime from any Screen etc.

    When you create a new instance of the main form, you will not able to see any shown data because it will be a different instance.