最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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; }
}
 

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; }
}
 

Share Improve this question edited Feb 15 at 19:40 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 15 at 18:20 Jamie GarciaJamie Garcia 2945 silver badges19 bronze badges 3
  • 5 Did someone describe what an Instance of a class is? -> You cannot do this: Main _main = new();, that's not the Instance of the Main Form that you have already created and shown, that a new, unrelated, Instance of it. You need a reference to the Instance of the already existing Main Form, where the DataGridView is presenting your data -- You should not add public methods to the .Designer.cs file. Those methods should (really) be part of the main file – Jimi Commented Feb 15 at 18:45
  • ok let me look around for that since everything i read was showing examples like the one i have – Jamie Garcia Commented Feb 15 at 18:46
  • The following may be of interest: stackoverflow/a/75951980/10024425 – It all makes cents Commented Feb 16 at 5:48
Add a comment  | 

2 Answers 2

Reset to default 2

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.

I would do without a BindingSource in this case. You should create a BindingList<Product> and bind that to the grid. That will handle the binding-related events to move data in both directions and binding that list will cause the grid to generate the appropriate columns.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论