如何在表单加载时从数据库更新datagridview?

时间:2019-11-22 17:18:28

标签: c# database datagridview

我对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);

    }
}

2 个答案:

答案 0 :(得分:0)

您必须刷新datagridview

if 'my_file.xls' in request.files:
    # do something

else:
    # return error

答案 1 :(得分:0)

    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();

    }
}