Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#DataGridView
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.
//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 7-Apr-11 23:54pm
Edited 8-Apr-11 1:00am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
public event System.Action NotifyAnotherForm;
Now when you create the form you wire that event:
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:
void a_NotifyAnotherForm() {
    custom code goes here
}
And when you do modifications in the frmNewBook, you raise the event in proper place:
if (NotifyAnotherForm != null) {
   NotifyAnotherForm();
}
  Permalink  
Comments
saeeddoe at 8-Apr-11 15:17pm
   
Hi Excuse 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 at 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.
Mika Wendelius at 10-Apr-11 14:45pm
   
You're welcome, glad it helped :) If you like, you can mark this solution as accepted.
canerik06 at 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.
Mika Wendelius at 10-Aug-12 1:16am
   
Thanks :)
Jayanta Chatterjee at 28-Mar-13 7:50am
   
My +5....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 

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
  Permalink  
v6
Comments
saeeddoe at 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 at 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 at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
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();
 
}
 
  Permalink  
v2
Comments
saeeddoe at 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 at 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)

  Print Answers RSS
0 Dnyaneshwar@Pune 994
1 Sergey Alexandrovich Kryukov 826
2 OriginalGriff 448
3 Tadit Dash 390
4 sanket saxena 323
0 Sergey Alexandrovich Kryukov 11,800
1 OriginalGriff 7,225
2 Peter Leow 5,009
3 Abhinav S 3,893
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 8 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid