C# 插入后如何在datagridview中立即刷新或显示?
在所有文本框中输入数据后,单击submit按钮后,它不会立即显示在datagridview中,我需要重新打开表单以查看新插入的行。要输入什么代码进行刷新 跟随@user32297代码。通过添加grdPatient.Update();和grdPatient.Refresh();单击“确定”以成功插入后,仍无法刷新C# 插入后如何在datagridview中立即刷新或显示?,c#,button,datagridview,insert,refresh,C#,Button,Datagridview,Insert,Refresh,在所有文本框中输入数据后,单击submit按钮后,它不会立即显示在datagridview中,我需要重新打开表单以查看新插入的行。要输入什么代码进行刷新 跟随@user32297代码。通过添加grdPatient.Update();和grdPatient.Refresh();单击“确定”以成功插入后,仍无法刷新 using System; using System.Collections.Generic; using System.ComponentModel; using Syste
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.SqlClient;
using System.Configuration;
namespace GRP_02_03_SACP
{
public partial class patient : Form
{
// Data Table to store employee data
DataTable Patient = new DataTable();
// Keeps track of which row in Gridview
// is selected
DataGridViewRow currentRow = null;
SqlDataAdapter PatientAdapter;
public patient()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (btnSubmit.Text == "Clear")
{
btnSubmit.Text = "Submit";
txtpFirstName.Focus();
}
else
{
btnSubmit.Text = "Clear";
int result = AddPatientRecord();
if (result > 0)
{
MessageBox.Show("Insert Successful");
grdPatient.Update();
grdPatient.Refresh();
}
else
MessageBox.Show("Insert Fail");
}
}
private int AddPatientRecord()
{
int result = 0;
// TO DO: Codes to insert customer record
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
String strCommandText = "INSERT PATIENT(pFirstName, pLastName, pContact, pAddress, pCity, pZip, pNationality, pRace, pIC, pGender, pDOB, pBloodType, pEmail) "
+ " VALUES (@pFirstName,@pLastName,@pContact,@pAddress,@pCity,@pZip,@pNationality, @pRace, @pIC, @pGender, @pDOB, @pBloodType, @pEmail)";
SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);
updateCmd.Parameters.AddWithValue("@pFirstName", txtpFirstName.Text);
updateCmd.Parameters.AddWithValue("@pLastName", txtpLastName.Text);
//updateCmd.Parameters["@clientid"].Direction = ParameterDirection.Output;
updateCmd.Parameters.AddWithValue("@pContact", txtpContact.Text);
updateCmd.Parameters.AddWithValue("@pAddress", txtpAddress.Text);
updateCmd.Parameters.AddWithValue("@pCity", txtpCity.Text);
updateCmd.Parameters.AddWithValue("@pZip", txtpZip.Text);
updateCmd.Parameters.AddWithValue("@pNationality", txtpNationality.Text);
updateCmd.Parameters.AddWithValue("@pRace", txtpRace.Text);
updateCmd.Parameters.AddWithValue("@pIC", txtpIC.Text);
updateCmd.Parameters.AddWithValue("@pGender", txtpGender.Text);
updateCmd.Parameters.AddWithValue("@pDOB", txtpDOB.Text);
updateCmd.Parameters.AddWithValue("@pBloodType", txtpBloodType.Text);
updateCmd.Parameters.AddWithValue("@pEmail", txtpEmail.Text);
// STEP 3 open connection and retrieve data by calling ExecuteReader
myConnect.Open();
// STEP 4: execute command
// indicates number of record updated.
result = updateCmd.ExecuteNonQuery();
// STEP 5: Close
myConnect.Close();
return result;
}
private void patient_Load(object sender, EventArgs e)
{
LoadPatientRecords();
}
private void LoadPatientRecords()
{
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
string strCommandText = "SELECT pFirstName, pLastName, pContact, pAddress, pCity, pZip, pNationality, pRace, pIC, pGender, pDOB, pBloodType, pEmail, pUsername, pPassword FROM Patient";
PatientAdapter = new SqlDataAdapter(strCommandText, myConnect);
//command builder generates Select, update, delete and insert SQL
// statements for MedicalCentreAdapter
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(PatientAdapter);
// Empty Employee Table first
Patient.Clear();
// Fill Employee Table with data retrieved by data adapter
// using SELECT statement
PatientAdapter.Fill(Patient);
// if there are records, bind to Grid view & display
if (Patient.Rows.Count > 0)
grdPatient.DataSource = Patient;
}
}
}
尝试在每次插入后刷新datagrid
datagridview1.update();
datagridview1.refresh();
希望这对你有帮助 成功插入后使用
LoadPatientRecords()
请尝试下面的代码
private void btnSubmit_Click(object sender, EventArgs e)
{
if (btnSubmit.Text == "Clear")
{
btnSubmit.Text = "Submit";
txtpFirstName.Focus();
}
else
{
btnSubmit.Text = "Clear";
int result = AddPatientRecord();
if (result > 0)
{
MessageBox.Show("Insert Successful");
LoadPatientRecords();
}
else
MessageBox.Show("Insert Fail");
}
}
我不知道您是否解决了问题,但解决这个问题的一个简单方法是重建datagridview的数据源(它是一个属性)。例如:
grdPatient.DataSource=MethodThatReturnList()
您可以将datagridview
DataSource
设置为null
,然后重新绑定它
private void button1_Click(object sender, EventArgs e)
{
myAccesscon.ConnectionString = connectionString;
dataGridView.DataSource = null;
dataGridView.Update();
dataGridView.Refresh();
OleDbCommand cmd = new OleDbCommand(sql, myAccesscon);
myAccesscon.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable bookings = new DataTable();
da.Fill(bookings);
dataGridView.DataSource = bookings;
myAccesscon.Close();
}
只需要像这样再次填充datagrid:
this.XXXTableAdapter.Fill(this.DataSet.XXX);
如果使用automaticlly connect from dataGridView,则此代码将在表单设计器中以表单_Load()自动创建,并使用工具箱添加新计时器。在属性中,将“Enabled”设置为“True” 将
DataGridView
设置为等于计时器中的新数据
请尝试下面的代码
this.dataGridView1.RefreshEdit();
this.donorsTableAdapter.Fill(this.sbmsDataSet.providers) 请仅输入与问题相关的有用代码不要编写所有代码在设置数据源后是否尝试调用grdPatient.DataBind()?我设置数据源的代码在哪里@Karthik Kalyanasundaramy老师给了我实用代码,插入后不会刷新datagridview@Karthik Kalyanasundaram
grdPatient.DataSource=患者代码>是设置数据源的行。在if
块中,也添加此行grdPatient.DataBind()。我把密码放在哪里了?你回家后会试试的,谢谢@user32297你可以在调用函数AddPatientRecord()后使用它。或者当你给出“插入成功”时给出它。我添加了一个新图像。我就是这样做的吗?但我犯了个错误@User322297请将语句放入{}中的if循环中,然后重试。如下所示:if(result>0){Messagebox.Show(“…”);datagridview1.update();datagridview1.refresh();}通过遵循您的代码,我已经没有错误了。我在我的帖子上编辑了上面的代码并添加了一张图片。但是在我插入一条记录之后,datagrid仍然没有刷新。我还需要重新打开表格@如果我们从DataGridView自动连接,那么User32297Yours是一种简单的方法。我使用了你的建议,因为我自动连接。