c#数据网格视图绑定源

本文关键字:绑定 视图 网格 数据 数据网 | 更新日期: 2023-09-27 18:29:47

这是我程序的当前剪辑,实际上,datagridview已断开与数据表的连接,因此如果我更改dataviewgrid上的任何内容,它将不会反映在数据表中。。。如何使数据网格视图上的任何编辑都能反映到至少数据表?

        OpenFileDialog OFD = new OpenFileDialog();
        OFD.Title = "CSV File";
        OFD.Filter = "Spreadsheet | *.csv";
        OFD.ShowDialog();
        FileHelperEngine engine = new FileHelperEngine(typeof(csv_SeatingPlan));
        try
        {
            csv_SeatingPlan[] container = engine.ReadFile(OFD.FileName) as csv_SeatingPlan[];
            CSV_Seating_Plan = new List<csv_SeatingPlan>(container);
            DataTable DT_student_Records = new DataTable();
            DT_student_Records.Columns.Add("Exam_Period", typeof(string));
            DT_student_Records.Columns.Add("Exam_Code", typeof(string));
            DT_student_Records.Columns.Add("Student_ID", typeof(string));
            DT_student_Records.Columns.Add("Student_Name", typeof(string));
            DT_student_Records.Columns.Add("Candidate_Number", typeof(string));

            foreach (csv_SeatingPlan row in CSV_Seating_Plan)
            {
                DT_student_Records.Rows.Add(row.examperiod, row.exam_Code, row.id_Student, row.name_Student, row.candidatenum_Student);
            }
            DT_student_Records.DefaultView.Sort = "Candidate_Number ASC";
            this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 11);
            //bindingCSVSP.DataSource = DT_student_Records;
            this.dataGridView1.DataSource = DT_student_Records;

c#数据网格视图绑定源

为了将更改传播到基础数据源(在您的情况下是表),您应该使用BindingSource对象。如何做到这一点的示例在链接页面的底部。