none
windows forms databinding RRS feed

  • Question

  • In this I am unable to add any records/modify any values in datagrid. datagrid not allowing me to add new rec. when I press the update button no effect.
    
    But the querying the data is fine.  Thanks.
    
    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;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    
    namespace CourseManager
    {
        public partial class CourseViewer : Form
      
        {
            //Create an ObjectContext instance based on SchoolEntity
            private SchoolEntities schoolContext;
            public CourseViewer()
            {
                InitializeComponent();
            }
    
            private void closeForm_Click(object sender, EventArgs e)
            {
                //Dispose the object context.
                schoolContext.Dispose();
                this.Close();
            }
    
            private void CourseViewer_Load(object sender, EventArgs e)
            {
    
    schoolContext = new SchoolEntities();
    
    {
        this.departmentList .DataSource = schoolContext.Departments.ToList();
        departmentList.ValueMember = "DepartmentID";
        departmentList.DisplayMember = "Name";
    }
    
    
            }
    
            private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
            {
                int ideptID = 0;
              
    
                try
                {
                    ideptID = (int)departmentList.SelectedValue;
                }
                catch { }
    
                if (ideptID > 0)
                {
                    schoolContext = new SchoolEntities();
                   //using (var ctx = new NorthwindEntities())
                    {
                        var q = from c in schoolContext .Courses 
                                where c.DepartmentID == ideptID
                                select c;
                        courseGridView.ReadOnly = false;
                        courseGridView.DataSource = q.ToList();
                    }
                }
    
                int icourseID = 0;
    
    
                try
                {
                    icourseID = (int)courseGridView.Rows[0].Cells[0].Value ;
                    
    
    
                }
                catch { }
    
                if (icourseID > 0)
                {
                    schoolContext = new SchoolEntities();
                    //using (var ctx = new NorthwindEntities())
                    {
                        var q = from s in schoolContext.StudentGrades 
                                where s.CourseID == icourseID
                                select s;
    
                        dataGridView1.DataSource = q.ToList();
                    }
                }
    
    
            }
    
            private void saveChanges_Click(object sender, EventArgs e)
            {
                try
                {
    
                    // Save object changes to the database, 
                    // display a message, and refresh the form.
                  
                    schoolContext.SaveChanges();
                    MessageBox.Show("Changes saved to the database.");
                    this.Refresh();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void courseGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
               
            }
    
            private void courseGridView_CellEnter_1(object sender, DataGridViewCellEventArgs e)
            {
                int icourseID = 0;
    
    
                try
                {
                    icourseID = (int)courseGridView.CurrentRow.Cells[0].Value;
    
    
    
                }
                catch { }
    
                if (icourseID > 0)
                {
                    schoolContext = new SchoolEntities();
                    //using (var ctx = new NorthwindEntities())
                    {
                        var q = from s in schoolContext.StudentGrades
                                where s.CourseID == icourseID
                                select s;
    
                        dataGridView1.DataSource = q.ToList();
                    }
                }
            }
    
           
          
        }
    }
    
    

    Saturday, November 26, 2011 2:25 AM

Answers

  • > In this I am unable to add any records/modify any values in datagrid. datagrid not allowing me to add new rec. [...] dataGridView1.DataSource = q.ToList();


     
    instead of ToList try to use ToBindingList
    below is an example
     

    using System.Data.Entity;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                var ds = new SchoolData();
                if (ds.Database.CreateIfNotExists())
                {
                    for (int i = 0; i < 5; i++)
                        ds.Schools.Add(new School { Name = "s"+i });
                    ds.SaveChanges();
                }
                ds.Schools.Load();
                var dg = new DataGridView 
                { 
                    Parent = this, 
                    Dock = DockStyle.Fill, 
                    DataSource = ds.Schools.Local.ToBindingList() 
                };
                this.Menu = new MainMenu();
                this.Menu.MenuItems.Add("save", (s, e) => ds.SaveChanges());
                this.Menu.MenuItems.Add("add", (s, e) =>  
                        ds.Schools.Add(new School { Name = "new" }) );
            }
        }
    
        public class School
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class SchoolData : DbContext
        {
            public DbSet<School> Schools { get; set; }
            protected override void OnModelCreating(DbModelBuilder mb)
            {
                base.OnModelCreating(mb);
                mb.Entity<School>().HasKey(t => t.Id).ToTable("Schools");
            }
        }
    }
    

     

     

    • Edited by Malobukv Saturday, November 26, 2011 4:49 PM
    • Marked as answer by yesganesh Monday, November 28, 2011 1:39 PM
    Saturday, November 26, 2011 4:48 PM