添加的值没有显示在Gridview上?c#

本文关键字:Gridview 显示 添加 | 更新日期: 2023-09-27 18:06:01

我是编程界和Stack Overflow的新手,所以请原谅我的"菜鸟"问题。

我有这个方法在这里(链接到Windows窗体)和想法是添加信息到文本框,并将其添加到Gridview和数据库。问题是添加的信息中只有一部分(自动生成的ID)显示在(ID)

上。
 private void btnAddNewAdmin_Click(object sender, EventArgs e)
        {
            if (!(txtAdminFname.Text == string.Empty || txtAdminLname.Text == string.Empty ||
                  txtPassword.Text == string.Empty))
            {
                MessageBox.Show("ERROR: all the boxes have to be filled and try again.");
            }
            // Here it will ADD a new Admin based on the information entered in the form, the New ID for the Admin is generated by the program.
                try
                {
                using (Hawthor_HS_Entities db = new Hawthor_HS_Entities())
                {
                    Administrator addAdministrator = new Administrator();// currently adding only the AID and nothing else.
                    {
                        addAdministrator.FName = txtAdminFname.Text;
                        addAdministrator.LName = txtAdminLname.Text;
                        addAdministrator.Password = txtNewPass.Text;
                    }
                        db.Administrators.Add(addAdministrator);
                        db.SaveChanges();
                        DataGridLoad();
                        MessageBox.Show("Success! New Administrator was Added");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Error: Could not Add new Administrator. Please try again.");
                }
            }

我有一个"Edit"方法,似乎工作只是好的,一旦行被创建(只有ID)。

DataGridLoad()

private void DataGridLoad()
        {
            using (Hawthor_HS_Entities db = new Hawthor_HS_Entities())
            {
                // Loads information into the DataGridView from the Administrators db.
                var adminLIST = (from c in db.Administrators
                    select new
                    {
                        AID = c.AID,
                        FirstName = c.FName,
                        LastName = c.LName
                    }).ToList();
                ADDdataGridView.DataSource = adminLIST;

                // loads information into the AdminID combo box in the EDIT tab.
                cboAdminID.DisplayMember = "AID";
                cboAdminID.ValueMember = "AID";
                cboAdminID.DataSource = adminLIST;
                //loads information into the AID combo box in the DELETE tab.
                cboAID.DisplayMember = "AID";
                cboAID.ValueMember = "AID";
                cboAID.DataSource = adminLIST;
            }
        }

添加的值没有显示在Gridview上?c#

根据您的评论,您在数据库中成功获取数据,但您有一个问题,以正确加载数据,所以

让我分享一个例子,也许它会帮助你。如果没有,请告诉我, 你可以使用下面的代码来加载你的datagridview
var context=new Hawthor_HS_Entities();
BindingSource bi = new BindingSource();
bi.DataSource = context.Administrators;
dataGridView1.DataSource = bi;
dataGridView1.Refresh();

您可以在SaveChanges()执行后一次从模型类获得ID。在您的示例中,您可以从addAdministrator对象中获取ID,例如

try
{
    using (Hawthor_HS_Entities db = new Hawthor_HS_Entities())
    {
        Administrator addAdministrator = new Administrator();// currently adding only the AID and nothing else.
        {
            addAdministrator.FName = txtAdminFname.Text;
            addAdministrator.LName = txtAdminLname.Text;
            addAdministrator.Password = txtNewPass.Text;
        }
        db.Administrators.Add(addAdministrator);
        db.SaveChanges();
        DataGridLoad();
        int Id = addAdministrator.ID; // you can able to get the id for the record you attempt to save.
        MessageBox.Show("Success! New Administrator was Added");
    }
}
catch (Exception)
{
    MessageBox.Show("Error: Could not Add new Administrator. Please try again.");
}