如何在c#中更新不同形式的GridView
本文关键字:GridView 更新 | 更新日期: 2023-09-27 18:12:33
我有一个GridView和其他4个窗体。当单击一个表单的按钮时,GridView行正在更新。在按钮上点击第二个表单另一个GridView显示。我需要在单个GridView中显示更新的值。
public void Load_Grid(int counter,string slecteditem,string playername)
{
DataTable dtable= new DataTable();
if ((dtable != null) && (dtable.Rows != null) /*&& (dtable.Rows.Count > rowindex)*/)
{
int row = dataGridView1.NewRowIndex;
dataGridView1.Rows.Add();
for (int columnindex = 0; columnindex < dtable.Columns.Count; columnindex++)
{
if (dtable.Rows[rowindex][columnindex] != null)
dataGridView1.Rows[row].Cells[columnindex].Value = dtable.Rows[rowindex][columnindex].ToString();
}
rowindex++;
dataGridView1.Rows[row].Cells[0].Value = counter;
dataGridView1.Rows[row].Cells[1].Value = slecteditem;
dataGridView1.Rows[row].Cells[2].Value = playername;
rowindex++;
dataGridView1.Refresh();
this.Show();
}
}
按钮点击出现在4种形式,试图更新另一种形式的Load_Grid函数。
private void button1_Click(object sender, EventArgs e)
{
fm1.Getselected_Controls(this);
cardobj.Load_Grid(counter, comboBox1.SelectedItem.ToString(), "Gamer3");
}
您应该使用表单激活事件。将数据保存为静态数据表,当anorder表单激活时,将新数据设置为网格。
我想说,你不必在点击按钮时更新所有表单。您应该更新所选表单并保存数据。当您关闭表单时,订单表单将被激活,并且您的表单激活事件将被触发。
定义静态类
public static class MyStaticClass
{
public static int counter { get; set; }
public static string selectedItem { get; set; }
public static string playername { get; set; }
}
添加表单激活事件到您的表单
void Form1_Activated(object sender, EventArgs e)
{
Load_Grid(MyStaticClass.counter, MyStaticClass.selectedItem,MyStaticClass.playername);
}
将按钮点击事件更改为
private void button1_Click(object sender, EventArgs e)
{
fm1.Getselected_Controls(this);
cardobj.Load_Grid(counter, comboBox1.SelectedItem.ToString(), "Gamer3");
MyStaticClass.counter = counter;
MyStaticClass.selectedItem= comboBox1.SelectedItem.ToString();
MyStaticClass.playername= "Gamer3";
}