从其他形式的按钮更新数据网格视图C#

时间:2016-06-11 02:47:41

标签: c# winforms entity-framework datagridview

我必须使用form1,form1和form2。

在form1中,我有一个datagridview,它从我的数据库加载数据,两个按钮,一个用于过滤器,另一个用于向datagridview添加一行。

在form2中,我有一些组合框,文本框,单选按钮和四个按钮,一个用于添加数据,一个用于删除选定的行(当我在datagridview中双行clic时,form2加载了该行中的信息),一个取消(关闭表单),最后一个是用修改后的信息更新选定的行。

但我的问题是我被困在那里,我不知道如何更新同一行。我可以添加新的或删除一个。我正在使用实体框架处理C#。

有人可以帮我解决如何从其他表单更新所选行的问题吗?

我尝试修改我在使用datagridview加载数据时使用的代码以加载form2中的数据:

    import pandas as pd
from pandas import ExcelWriter
import numpy as np

xl = pd.ExcelFile('C:\\Users\\Steph_000\\Desktop\\students2.xlsx')
xl

wr = pd.ExcelWriter('C:\\Users\\Steph_000\\Desktop\\students2.xlsx')

xl.sheet_names

['Sheet1']

df = xl.parse('Sheet1') #This reads the sheet into a dataframe
df

cp = pd.read_excel('C:\\Users\\Steph_000\\Desktop\\students2.xlsx')

IDNum = cp[:1] #This pulls the first row as a variable

print(IDNum) #This is where it prints to the search bar to look the student up

chart = cp[1:] #This pulls everything but the first row as a variable

print(chart)

df.to_excel(wr, 'Sheet1')
wr.save() #This is supposed to overwrite it, but just saves it as blank

2 个答案:

答案 0 :(得分:0)

转到form2时,使用一些静态变量来存储行信息。在返回时,使用这些静态变量来更新同一行。 例如:

public static int rowId;
public static bool changed = false;
private void rowClicked(object sender, EventArgs e)
{
//get row id. say you got row id = 1
Form1.rowId = 1;
Form2 frm = new Form2();
frm.ShowDialog();
if(Form1.changed)
updateRowinGridView(rowId);
}

在Form2按钮中,使用以下代码示例:

private void updateButton(object sender,EventArgs e)
{
//Update Record//
Form1.changed = true;
Close();
}

希望这会对你有所帮助。

答案 1 :(得分:0)

您可以在项目周围实现一个DataSet,当您更新它时,所有绑定组件都会刷新。