Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
3.86/5 (5 votes)
See more:
Hi to every body.
I am programming a database(Library database) program whit C# and SQL Server.
I have a Tabel suppose that tblBook that show the information about my library books.
I have a MainForm that have a Datagridview that linked to my tblBook and show my Books information that inserted in tblBook.
in Main Form I have a Button that open another Form(Name is frmNewBook) for inserting information about a new Book in my tblBook.
when I open frmNewBook by that Button and insert a new record at this time I want the Main Form automaticaly Update and the Datagridview show also new record information automaticaly by closing the frmNewBook Form or by Pushing the button(Save New Record.)
that's my question.
thank a lot
Saeed.

I use Windows Application not Web Application.
C#
//The Function for showimg the information of tblBook in DGVBook in MainForm.
public void ShowAllBook()
{
  SqlDataAdapter addapter = new SqlDataAdapter();
  DataTable dt = new DataTable();
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = "SELECT * FROM tblBook";
  cmd.Connection = sqlcnn;
  addapter.SelectCommand = cmd;
  addapter.Fill(dt);
  DGVBook.DataSource = dt;
}

//****************************************************
private void MainForm_Load(object sender, EventArgs e)
{
  ShowAllBook();
}

//*******************************************************
// Inserting a new record by frmNewBook.
private void btnInsert_Click(object sender, EventArgs e)
{
  MainForm mainform = new MainForm();
  mainform .ShowAllBook();
}
Posted
Updated 8-Apr-11 1:00am
v4

Dear saeeddoe,

While, you edit record in DataGridview it updates DataTable (without any effort). You just have to call Update methode of adapter by passing DataTable where you want to udpate it. But make sure you are using single SqlTable.


MSIL
public partial class MainForm : Form
{
SqlDataAdapter addapter = new SqlDataAdapter();
DataTable dt = new DataTable();

public void ShowAllBook()

{  
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = "SELECT * FROM tblBook";
  cmd.Connection = sqlcnn;
  addapter.SelectCommand = cmd;
  addapter.Fill(dt);
  DGVBook.DataSource = dt;
}

//****************************************************
private void MainForm_Load(object sender, EventArgs e)
{
  ShowAllBook();
}

//*******************************************************
// Inserting a new record by frmNewBook. 
//Aman's Comment: - Here I am assuming that I have Inserted record in MainForm's DataGridView. Best Partice is do these thing on same form. Otherwise you can use adapter and dt as global variable.
private void btnInsert_Click(object sender, EventArgs e)
{
  //Update Sql Table
   addapter.Update(dt);
  //Get All Records of tbBooks 
  ShowAllBook();
}
}


This update method will update your table. No matter you have inserted, deleted or modified any records.



Regards!
Aman
 
Share this answer
 
v6
Comments
saeeddoe 8-Apr-11 10:08am    
ok.
I want update DGV in MainForm automaticaly (not load againmy MainForm) when I insert a new record in frmNewBook.
please insert your commnd in my Code.
thanks.
Aman4.net 8-Apr-11 12:27pm    
Sorry for late response. Answer is updated. Plz mention your comment if It doesn't work. If it works the mark it as answer.
saeeddoe 8-Apr-11 15:07pm    
Hi.
Excuse me. I am beginner in Programing and somthing for me not clear.

From frmNewBook Form I have the following event if it is possible to call adapter hear or not and how? I want to insert a new record only from this frmNewBook form and then MainForm automaticaly updated and DGVBook show any new record.
for this reason in the End of this code I call ShowAllBook() again by (MainForm mainform = new MainForm();
mainform .ShowAllBook();) but it dose not update my MainForm or DGVBook. that is my ununderstanding! please insert your codes in frmNewBook form for updating the DGVBook.
thanks.

private void btnInsert_Click(object sender, EventArgs e)
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO tblBook(BookID,Title) VALUES ('" + txtBookID.Text.Trim() + "','" + txtTitle.Text.Trim()+"')";
cmd.Connection = sqlcnn;
sqlcnn.Open();
cmd.ExecuteNonQuery();
sqlcnn.Close();
MessageBox.Show("Information Successfully Inserted.");


MainForm mainform = new MainForm();
mainform .ShowAllBook();
Aman4.net 9-Apr-11 0:43am    
Ok, The way (to use any variable globally) is declare it into class file and pass value once you have done this form work and pass to variable then access that variable on other form. But in your way, You can close the frmNewBook once you have done insertion and again call ShowAllBook on MainForm. Dear, you need to optimize the things to be good.
saeeddoe 9-Apr-11 10:33am    
HI my friend can you solve my problem and finish my Requisition please.
if it is possible to you please write the correct code and my project work properly.
thanks.
As already said, if you modify the contents of the same datatable in another window the datagridview binded to that datatable will be updated.

If you want to do programmatic operations, one way that you add a new event in the frmNewBook, for example:
C#
public event System.Action NotifyAnotherForm;

Now when you create the form you wire that event:
C#
frmNewBook a = new frmNewBook();
a.NotifyAnotherForm += new System.Action(a_NotifyAnotherForm);
a.Show();

In the main form you have the method that handles the event, something like:
C#
void a_NotifyAnotherForm() {
    custom code goes here
}

And when you do modifications in the frmNewBook, you raise the event in proper place:
MIDL
if (NotifyAnotherForm != null) {
   NotifyAnotherForm();
}
 
Share this answer
 
Comments
saeeddoe 8-Apr-11 15:17pm    
HiExcuse me it is not clear for me. please explain more and if it is possible to you write me a complet Example regard to my request.
thanks.
saeeddoe 10-Apr-11 14:41pm    
hi my friend it works very well thanks for your help and only you solved my problem.
best regard
saeed.
Wendelius 10-Apr-11 14:45pm    
You're welcome, glad it helped :)

If you like, you can mark this solution as accepted.
canerik06 9-Aug-12 20:49pm    
Hi Mika,
I do not know how to thank to you. I was trying to find out this solution for days. Thank you and best regards. Have good luck at your works.
Wendelius 10-Aug-12 1:16am    
Thanks :)
After inserting Record You need to bind your gridview Again
so that updated Database table records will be diplayed in Datagrid.

after Inserting The Data

Response.Redirect("MainPage.aspx")

//In Page Load of Main Page

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fn_BindGrid();
        }
    }

 public void fn_BindGrid()
{
 //  you code to fetch the Data From the database
   Gridview1.DataSource = Your DataSource;
    GridView1.DataBind();

}
 
Share this answer
 
v2
Comments
saeeddoe 8-Apr-11 6:31am    
yes but I can not call the function (that show the tblBook information in datagridview in Main Form )in the frmNewBook Form. can you write an Example for it. thanks.
saeeddoe 8-Apr-11 6:52am    
thank.
I use Windows Application not Web Application.
and I Inserted my Code please tell me my mistake and correct it.
Thanks my friend.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900