Reputation: 593
I am trying to make a simple binding from SQL server to a DataGridView in C# but this code is not working (My programming skills are little rusty)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestProject
{
public partial class Form1 : Form
{
public IQueryable<boklet2> qry;
public AnalysisUpgradeEntities Ana = new AnalysisUpgradeEntities();
public class boklet2
{
public int _ID;
public int? _ItemSpecs;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = true;
}
private void button1_Click(object sender, EventArgs e)
{
qry = from an in Ana.analysis_pipes
select new boklet2
{
_ID = an.ID,
_ItemSpecs = an.itemSpecs
};
dataGridView1.DataSource = qry.ToList();
//dataGridView1.DataSource = qry;
}
}
}
Am I missing a library I should add with the word Using I also tried adding the dataGridView1.AutoGenerateColumns = True
but still no data is shown
Upvotes: 0
Views: 275
Reputation: 2317
Change this:
public class boklet2
{
public int _ID;
public int? _ItemSpecs;
}
to this
public class boklet2
{
public int _ID { get; set; }
public int? _ItemSpecs { get; set; }
}
and let me know
Explanation provided by @AdamVincent, thank you!
Fields are ordinary member variables or member instances of a class. Properties are an abstraction to get and set their values. Properties are also called accessors because they offer a way to change and retrieve a field if you expose a field in the class as private. A field is public int _ID;, and the fix was to add the accessors by using the auto property syntax public int _ID { get; set; }
Upvotes: 1