如何从第二种形式重新绑定Datagridview中的数据 [英] How to Re-Bind Data in Datagridview from Second Form
问题描述
我在主窗体上有一个datagridview.
我想从第二种形式重新绑定datagridview中的数据.当我单击第二个窗体上的后退按钮时,它必须重新绑定主窗体上的数据.
//主要形式
主窗体的名称是:frmMain
Datagridview名称是:dtgAllIngdetTerminals(修饰符:公共)
我编写了以下代码:
I have a datagridview on my Main Form.
I want to re-bind the data in the datagridview from the second form. When I am clicking on the back button on the second form, it must re-bind the data on the Main Form.
//Main Form
Name of the Main Form is:frmMain
Datagridview name is: dtgAllIngdetTerminals (Modifiers: Public)
I have written the following code:
//Second Form
//Methid to bind the data
public void ShowAllTerminalDetails()
{
frmMain formMain = new frmMain();
DataTable dt = new DataTable();
dt = clsIngadmin.listallingdetails(); //reading a short procedure from a class
formMain.dtgAllIngdetTerminals.DataSource = dt;
}
//我的第二个表单上的返回"按钮将返回到主表单
//Back button on my second form that is going back to the main Form
private void btnBack_Click(object sender, EventArgs e)
{
ShowAllTerminalDetails();
this.Close();
}
如果我重新绑定了Main表单中的数据,它将起作用.所有数据都显示在datagridview中.如果我尝试重新绑定第二个表单中的数据,则datagridview中将不显示任何内容.我在做什么错?
If I re-bind the data from the Main form, it works. All the data are displayed in the datagridview. If I am trying to re-bind the data from the second form, nothing is displayed in the datagridview. What am I doing wrong?
推荐答案
如果您想在首次加载页面时查看数据,并在页面加载时调用您的页面加载方法还刷新按钮
call your binding method on page load if u want to see the data when page is load first time and also write the same method on refresh button also
我为使其正常工作所做的事情:
我在工具栏上做了一个刷新按钮.我将数据表绑定到onclick事件的刷新"按钮中.
What I did to make it work:
I made a Refresh button on a toolstrip. I bind the datatable in the Refresh button on the onclick event.
private void tlStripRefresh_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = clsIngadmin.listallingdetails();
dtgAllIngdetTerminals.DataSource = dt;
}
然后我在要使用它的地方调用了刷新按钮的刷新事件(tlStripRefresh).
Then I called the refresh event(tlStripRefresh) of the refresh button where I want to use it.
tlStripRefresh_Click(tlStripRefresh, EventArgs.Empty);
这篇关于如何从第二种形式重新绑定Datagridview中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!