Zarwalski
Zarwalski

Reputation: 69

Calling an existing DataSet into a winform in c#

This is a bit of an odd one as I know how to fix the issue in the code, but it involved changing the "DO NOT CHANGE" code in the form.designer

I have a data set I want creating outside the form.

Jewels ds = new LoadDataTable().Load();

I then make the form using form designer. After setting all data up, the table is empty.

this is because in the form.designer the line

this.jewels = new JewelsOfExile.Jewels();

exists,

if i edit this code to this.jewels = ds;

it fixes it and uses my existing dataset. Great!!

But this messes over with the designer and breaks the tool. If i make any changes to the tool it reverts my changes and makes new variables (jewels1)

How do i go about loading in my Dataset without manually changing the designer.

full code snippets below

using System;
using System.Windows.Forms;
using JewelsOfExile.Tables;

namespace JewelsOfExile
{

static class Program
{        
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Jewels ds = new LoadDataTable().Load();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(ds));
    }
}
}

using System.Windows.Forms;

namespace JewelsOfExile
{
    public partial class Form1 : Form
    {
        public Form1(Jewels ds)
        {
            InitializeComponent(ds);
        }
    }
}

and the code I shouldnt edit

private void InitializeComponent(Jewels ds)
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.jewels = ds;
        this.affixesBindingSource = new System.Windows.Forms.BindingSource(this.components);
etc.....
}

Helped resolved thanks to Archer,

Added an onload event in the form and removed the creation in main.

        private void Form1_Load(object sender, System.EventArgs e)
    {
        Jewels jewels = new LoadDataTable().Load();
        affixesBindingSource.DataSource = jewels;
    }

Upvotes: 1

Views: 449

Answers (1)

Zarwalski
Zarwalski

Reputation: 69

Helped resolved thanks to Archer,

Added an onload event in the form and removed the creation in main to the load event.

    private void Form1_Load(object sender, System.EventArgs e)
{
    Jewels jewels = new LoadDataTable().Load();
    affixesBindingSource.DataSource = jewels;
}

Upvotes: 1

Related Questions