How to refresh or show immediately in datagridview after inserting?
Use LoadPatientRecords()
after a successful insertion.
Try the below code
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");
}
}
Refresh button - Refreshing data grid view after inserting, deleting, updating
The easiest way to handle this is to use a Binding Source object.
If your loading information into your DataGridView from an Access Database then your most likely storing the Data in a Dataset or DataTable.
Create a Binding Source object, and once you have populated your DataTable/Dataset, set the datasource for your Binding Source to your DataTable. Then set the Datasource from the DataGridView as the Binding Source object.
Doing this ensures that any changes in your datagridview or reflected in the DataTable and vice Versa. If you reload data into your DataTable it will reflect in the Data Grid Automatically.
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource= bs;
All changes will now happen automatically.
C# refresh DataGridView when updating or inserted on another form
// Form A
public void loaddata()
{
//do what you do in load data in order to update data in datagrid
}
then on Form B define:
// Form B
FormA obj = (FormA)Application.OpenForms["FormA"];
private void button1_Click(object sender, EventArgs e)
{
obj.loaddata();
datagridview1.Update();
datagridview1.Refresh();
}
Refresh DataGridView after INSERT into SQL
Something like that @mmking, but not exactly
I already solved it.. the problem was exactly what @Fabio said...
Inside of the button click method I fill again de DataGridView with the DataSet.
public void Insert_Button_Click(object sender, EventArgs e)
{
MyClass.InsertNewClient(fullNametxt.Text, shortNametxt.Text);
this.tblClientTableAdapter.Fill(this.DataSetClient.tblClient);
}
Just to get clear... I use the DataGridView of toolbox, select "Choose data source" and select the table that I want to use to fill DataGrid...
I use the DataSetName that gave me by default and put it right like I show it in the code
Hope it helps for future questions
refresh datagridview after insert or update or delete without selecting new sql query
You can do it in couple of ways.
- Add the newly inserted record to your datatable (you need to use a global datatable variable for this) and refresh your
Grid View
using this datatable. - You can add the newly inserted record directly to the
Grid View
You can follow these techniques also for DELETE and UPDATE
Here is the implementation of idea #1 for your existing code :
DataTable dtclubroom = new DataTable();
private void button4_Click(object sender, EventArgs e)
{
employee();
}
public void employee()
{
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, myConnection);
try
{
myConnection.Open();
dtclubroom.Clear();
command.Connection = myConnection;
command.CommandText = "Select * from employee ";
adapter.SelectCommand = command;
adapter.Fill(dtclubroom);
dataGridView2.DataSource = dtclubroom;
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
finally
{
myConnection.Close();
}
}
private void button5_Click(object sender, EventArgs e)
{
SqlCommand command2 = new SqlCommand();
try
{
myConnection.Open();
command2.CommandText = "insert into employee (name,id) values (@name,@id)";
command2.Connection = myConnection;
command2.Parameters.AddWithValue("@name","Leon");
command2.Parameters.AddWithValue("@id", "002");
command2.ExecuteNonQuery();
DataRow dr = dtclubroom.NewRow();
dr["name"] = "Leon";
dr["id"] = "002";
dtclubroom.Rows.Add(dr);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
myConnection.Close();
}
dataGridView2.DataSource = dtclubroom; //<- refresh datagridview
}
Look that the datatable declaration is moved up and you need to place it in top of your class :
DataTable dtclubroom = new DataTable();
Nothing else need to be global.
Show immediately data in datagridview After the Insertion/Updating
I played around with the code you have posted and managed to get the GridView refreshed without having to reload.
Please change the Display class as below
public static void Display_Customer3(DataGridView dgv)
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
{
using (var cmd = new SqlCommand("usp_GetCustomers", con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
using (var sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable(); // Initiate the datatable here to avoid getting duplicate data during 'sda.Fill(dt)'
sda.Fill(dt);
var bsource = new BindingSource();
bsource.DataSource = dt;
dgv.DataSource = bsource;
//sda.Update(dt);
}
con.Close();
}
}
}
Update rest of the code where Display.Display_Customer
is referenced to appropriately.
Display.Display_Customer(CustomersList);
Or
Display.Display_Customer(_view.CustomersList);
With the above changed, I managed to refresh the DataGridView
upon update.
Update to enable DataGridView Search:
Since BindingSourse
is used as the DataSource, you can utilize the BindingSourse.Filter
to enable the search. Update the Search textbox as below;
BindingSource dgBS = (BindingSource)CustomersList.DataSource;
dgBS.Filter = string.Format("FileName LIKE '%{0}%'", txtFilter_FileName.Text);
Related Topics
How to Pass Multiple Arguments in Processstartinfo
Programmatically Close Aspx Page from Code Behind
How to Add/Update Child Entities When Updating a Parent Entity in Ef
How to Loop Through All Fields in an Object in C#
How to Trim All Elements in a List
Drawing a Triangle With Rounded Corners
Generate C# Class from SQL Server Table Without Store Procedure
Generate Xml for <Govtalkmessage Xmlns="Http://Www.Govtalk.Gov.Uk/Cm/Envelope">
Ziparchive Gives Unexpected End of Data Corrupted Error
Reportviewer: Show Reports in Print Layout With Page Width Zoommode
Find Item in Observablecollection Without Using a Loop
File and Line Numbers for Errors Are Not Displaying in the Error List Unless the File Is Open
The Find Element Returns Empty String..Using Xpath Contains,Text()
Storing Enums as Strings in Mongodb
User.Identity.Getuserid() Returns Null After Successful Login
Using Selenium2, How to Check If Certain Text Exists on the Page