16

I have 2 forms which are form A and form B,

form A allowes user to insert and update student information.

form b is only a DataGridView and button there.

When I insert student on form A, then I go to form B, the new student did not show on the DataGridView , and if I rerun the program, the new student will appear in form B.

I tried using this on button on form b

datagridview1.refresh();
datagridview1.update();

but it's still not working.


Edited:

My code for inserting a worker

cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,[Position],Photo) values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Value + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + dateTimePicker2.Value + "',@Position,@Photo)", con);


        cmd.Parameters.AddWithValue("@Position", comboBox1.SelectedText.ToString());
        conv_photo();

        con.Open();
        int n = cmd.ExecuteNonQuery();
        //cmd.ExecuteNonQuery();
        con.Close();
        if (n > 0)
        {
            MessageBox.Show("Inserted");
            loaddata();

            rno++;
        }
        else
            MessageBox.Show("No Insert");



    }

my Datagridview1(Form2) doesn't automatically update when I inserted a new worker. But if I rerun the application, the new worker appears.

4

8 に答える 8

23
// Form A
public void loaddata()
{
    //do what you do in load data in order to update data in datagrid
}

次に、フォーム B で次のように定義します。

// Form B
FormA obj = (FormA)Application.OpenForms["FormA"];

private void button1_Click(object sender, EventArgs e)
{
    obj.loaddata();
    datagridview1.Update();
    datagridview1.Refresh();
}
于 2013-03-20T14:22:03.737 に答える
5

C# の datagridview の場合、このコードを使用します

con.Open();
MySqlDataAdapter MyDA = new MySqlDataAdapter();
string sqlSelectAll = "SELECT * from dailyprice";
MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, con);

DataTable table = new DataTable();
MyDA.Fill(table);

BindingSource bSource = new BindingSource();
bSource.DataSource = table;


dataGridView1.DataSource = bSource;
con.Close();

これは、datagridview に新しいレコードを表示するために機能します。

于 2016-11-10T18:55:17.620 に答える
5

DataGridView.Refreshおよび AndDataGridView.Updateは、Control から継承されたメソッドです。コントロールを再描画する必要があるため、新しい行が表示されません。

私の推測では、データの取得は Form_Load で行われます。フォーム B のボタンでデータベースから最新のデータを取得する場合は、Form_Load が行っていることをすべて実行する必要があります。

これを行う良い方法は、データ取得の呼び出しを別の関数に分けて、それを From Load イベントと Button Click イベントの両方から呼び出すことです。

于 2013-03-20T14:38:18.183 に答える
2

簡単な例を挙げると、十分な出発点になるはずです

フォーム A のコード

public event EventHandler<EventArgs> RowAdded;

private void btnRowAdded_Click(object sender, EventArgs e)
{
     // insert data
     // if successful raise event
     OnRowAddedEvent();
}

private void OnRowAddedEvent()
{
     var listener = RowAdded;
     if (listener != null)
         listener(this, EventArgs.Empty);
}

フォーム B のコード

private void button1_Click(object sender, EventArgs e)
{
     var frm = new Form2();
     frm.RowAdded += new EventHandler<EventArgs>(frm_RowAdded);
     frm.Show();
}

void frm_RowAdded(object sender, EventArgs e)
{
     // retrieve data again
}

EventArgs新しく追加されたデータを含む独自のクラスを作成することも検討できます。これを使用して、データを DatagridView の新しい行に直接追加できます。

于 2013-03-20T15:33:07.043 に答える
-1

小さな関数を作成してどこでも使用

public SqlConnection con = "Your connection string"; 
public void gridviewUpdate()
{
    con.Open();
    string select = "SELECT * from table_name";
    SqlDataAdapter da = new SqlDataAdapter(select, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "table_name");
    datagridview.DataSource = ds;
    datagridview.DataMember = "table_name";
    con.Close();
}
于 2020-08-20T15:37:33.797 に答える