【问题标题】:How do I get a datagridview to update from database on form load?如何在表单加载时从数据库更新 datagridview?
【发布时间】:2019-11-22 17:18:28
【问题描述】:

我对 C# 和一般编码相当陌生。我已经查看了类似的问题,并没有太多的运气来解决这个问题。

我正在制作一个应用程序,将学生的出勤详细信息存储在数据库中的表格中。目前,当我运行它时,详细信息会从文本框添加到表格中。一个按钮会打开一个带有 datagridview 的单独表单,但其中的详细信息不会更新。如果我重新运行应用程序并打开第二个表单,则 datagridview 已更新。如何在应用程序运行时根据添加到表中的信息更新 datagridview?

这是向表格添加详细信息的代码

                using (SqlConnection sc = new SqlConnection())
                {
                    sc.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\corry\Desktop\StudentAttendanceBurton\Attendance.mdf;Integrated Security=True";
                    sc.Open();

                    using (SqlCommand com = sc.CreateCommand())
                    {
                        com.CommandText =
                          "insert into BUS102(\n" +
                          "  Name,\n" +
                          "  [Student ID],\n" +
                          "  Date)\n" +
                          "values(\n" +
                          "  @prm_Name,\n" +
                          "  @prm_Student_ID,\n" +
                          "  @prm_Date)";


                        com.Parameters.Add("@prm_Name", SqlDbType.NVarChar, 50).Value = student.Name;
                        com.Parameters.Add("@prm_Student_ID", SqlDbType.Int).Value = student.StudentID;
                        com.Parameters.Add("@prm_Date", SqlDbType.SmallDateTime).Value = student.Date;


                        com.ExecuteNonQuery();
                    }
                }

这是具有 datagridview 的表单的代码

public partial class AttendanceForm : Form

{

    public AttendanceForm()
    {
        InitializeComponent();
    }

    private void bUS102BindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.bUS102BindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.attendanceDataSet);

    }

    private void AttendanceForm_Load(object sender, EventArgs e)
    {

        // TODO: This line of code loads data into the 'attendanceDataSet.BUS102' table. You can move, or remove it, as needed.
        this.bUS102TableAdapter.Fill(this.attendanceDataSet.BUS102);

    }
}

【问题讨论】:

    标签: c# database datagridview


    【解决方案1】:
        public partial class Form1 : Form {
        private DataSet m_ds = new DataSet();
    
        public Form1() {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e) {
            using (SqlConnection conn = new SqlConnection(@"Data Source=YOURSql;Initial Catalog=YOURDB;Integrated Security=True")) {
    
                // set command
                SqlCommand cmd = new SqlCommand("SELECT * FROM YourTable", conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
    
                conn.Open();
                da.Fill(m_ds);
    
                // bind data to dataGrid
                dataGridView1.DataSource = m_ds.Tables[0];
    
                // refresh Data
                dataGridView1.Refresh();
                conn.Close();
            }
    
    
        }
    
        private void cmdChangeData_Click(object sender, EventArgs e) {
            // add new row explicit 
            DataRow nr = m_ds.Tables[0].NewRow();
            nr[0] = "0000";
            nr[1] = "xxxx";
    
            // add new row to DataSet (just in memory, NOT TO DB)
            m_ds.Tables[0].Rows.Add(nr);
    
            // Refresh Data
            dataGridView1.Refresh();
    
        }
    }
    

    【讨论】:

    • 谢谢!您为表单加载发布的第一批​​代码效果很好。
    【解决方案2】:

    你必须刷新datagridview

    this.dataGridView1.Refresh();
    

    【讨论】:

    • 我尝试将其添加到 AttendanceForm_Load 方法中,但在填充代码之前或之后都不起作用。我应该把它放在代码的什么地方?
    • 逻辑是:在Form.Load中填充数据,将数据适配器绑定到DataGridView并刷新数据
    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多