0

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

4
  • 1
    What does qry variable contains after running the query? Commented Nov 3, 2017 at 13:11
  • 1
    have you check that returned list has elements??? Commented Nov 3, 2017 at 13:12
  • My C# is rusty, but don't you need a Databind some place? Commented Nov 3, 2017 at 13:18
  • @NiranjanKala I cheched and it contains the 10 elements I added in the SQL server table Commented Nov 3, 2017 at 14:56

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Still, no data is shown in the DataGridView. I am confused what could possibly be wrong ?!! Its working now
Sorry my fault its working now, but what was the problem might I ask ?
@Tima, I do not know the official answer, but I'll go on a limp and say that I assume the DataGridView initializer does not know how to properly initialize the grid when the fields it needs to display don't have a setter because it's unable to create the initial instance of the field <- It's not exactly how I have it worded in my head but that's how it came out...
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; }
please update this code only response with some more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.