Datagridview在c#中自动更新. [英] Datagridview automatically update in c#.

查看:120
本文介绍了Datagridview在c#中自动更新.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我正在使用C#和SQL Server编写数据库(库数据库)程序.
我有一个Tabel,假设tblBook显示有关我的图书馆书籍的信息.
我有一个MainForm,其中有一个Datagridview链接到我的tblBook,并显示了插入到tblBook中的我的Books信息.
在主窗体中,我有一个可以打开另一个窗体(名称为frmNewBook)的按钮,用于在我的tblBook中插入有关新书的信息.
当我通过该按钮打开frmNewBook并在此时插入新记录时,我希望通过关闭frmNewBook表单或按按钮(保存新记录)来自动更新主窗体,并且Datagridview也自动显示新记录信息. > 那是我的问题.
非常感谢
赛义德.

我使用Windows应用程序而不是Web应用程序.

 // 在MainForm的DGVBook中显示tblBook信息的功能.
公共 无效 ShowAllBook()
{
  SqlDataAdapter addapter =  SqlDataAdapter();
  DataTable dt =  DataTable();
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = " ;
  cmd.Connection = sqlcnn;
  addapter.SelectCommand = cmd;
  addapter.Fill(dt);
  DGVBook.DataSource = dt;
}

//  ************************ ******************************** 
私有 无效 MainForm_Load(对象发​​件人,EventArgs e)
{
  ShowAllBook();
}

//  ************************ ************************************** 
// 通过frmNewBook插入新记录.
私有 无效 btnInsert_Click(对象发​​件人,EventArgs e)
{
  MainForm mainform =  MainForm();
  mainform .ShowAllBook();
} 

解决方案

尊敬的saeeddoe,

同时,您在DataGridview中编辑记录,它会更新DataTable(无需任何努力).您只需通过将 DataTable 传递到要更新的位置来调用适配器的Update方法.但是请确保您使用的是单个SqlTable.


公共局部 class  MainForm:表单
{
SqlDataAdapter addapter =新的SqlDataAdapter();   
DataTable dt = new DataTable();   

公共无效 ShowAllBook()

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

//  ************************ ******************************** 
私人 void  MainForm_Load(对象发​​送者,EventArgs e)
{
  ShowAllBook();   
}

//  ************************ ************************************** 
// 通过frmNewBook插入新记录. 
//  Aman的评论:-在这里,我假设我已经在MainForm的DataGridView中插入了记录.最好的Partice是以相同的形式执行这些操作.否则,您可以将adapter和dt用作全局变量.
私人 void  btnInsert_Click(对象发​​件人,EventArgs e)
{
  // 更新Sql表
   addapter.Update(dt);   
  // 获取tbBooks的所有记录
  ShowAllBook();   
}
} 



此更新方法将更新您的表.无论您是否插入,删除或修改了任何记录.



问候!
如前所述,如果您在另一个窗口中修改相同数据表的内容,则绑定到该数据表的datagridview将被更新.

如果要进行编程操作,可以在frmNewBook中添加新事件的一种方式,例如:

 公共 事件 System.Action NotifyAnotherForm; 


现在,当您创建表单时,您可以关联该事件:

 frmNewBook a =  frmNewBook();
a.NotifyAnotherForm + =  System.Action(a_NotifyAnotherForm);
a.Show(); 


在主窗体中,您具有处理事件的方法,例如:

  void  a_NotifyAnotherForm(){
    自定义代码在这里
} 


当您在frmNewBook中进行修改时,可以在适当的位置引发事件:

如果(NotifyAnotherForm!= null){
   NotifyAnotherForm();
} 


插入Record之后,您需要再次绑定gridview
因此更新的数据库表记录将在Datagrid中显示.

插入数据后

Response.Redirect("MainPage.aspx")

//在首页的页面加载中

 受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
    {
        如果(!IsPostBack)
        {
            fn_BindGrid();
        }
    }

 公共 无效 fn_BindGrid()
{
 // 您编写了代码以从数据库中获取数据
   Gridview1.DataSource =您的数据源;
    GridView1.DataBind();

} 


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();
}

解决方案

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


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();
}


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();

}


这篇关于Datagridview在c#中自动更新.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆