第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 2 個表單之間發(fā)送數(shù)據(jù)

在 2 個表單之間發(fā)送數(shù)據(jù)

C#
翻閱古今 2022-06-12 11:27:54
我有帶有 datagridview 和一個按鈕的 form1。當(dāng)我單擊一個按鈕時,會打開一個新表單,其中有一個文本框和一個按鈕。在這個文本框中,我可以編寫查詢,并單擊一個按鈕,查詢結(jié)果顯示在 form1 datagridview 中。問題是它打開了 form1 的另一個實例,但我希望 form1 始終保持打開狀態(tài),并且根據(jù) form2 中的查詢輸入,只有 datagridview 中的記錄在發(fā)生變化。form1 和 form2 都需要在調(diào)用時打開并處于活動狀態(tài)。這是我的代碼://FORM 1public Form1(){    InitializeComponent();}private void button1_Click(object sender, EventArgs e){    var queryForm = new Form2();    queryForm.Show(this);}//FORM 2public Form2(){    InitializeComponent();}private SqlConnection Conn;private void Form1_Load(object sender, EventArgs e){    Conn = new SqlConnection(@"Data Source=srvr;Initial Catalog =db; User ID =user; Password =pass");}private void btnExecute_Click(object sender, EventArgs e){    Form1 frm1 = new Form1();    frm1.Show(this);    frm1.Activate();    SqlCommand cmd = new SqlCommand();    cmd.Connection = Conn;    cmd.CommandText = txtQuery.Text;    try    {        Conn.Open();        SqlDataReader reader = cmd.ExecuteReader();        frm1.dataGridView1.Columns.Clear();        frm1.dataGridView1.Rows.Clear();        if (reader.HasRows)        {            DataTable schema = reader.GetSchemaTable();            int field_num = 0;            foreach (DataRow schema_row in schema.Rows)            {                int col_num = frm1.dataGridView1.Columns.Add(                    "col" + field_num.ToString(),                    schema_row.Field<string>("ColumnName"));                field_num++;                frm1.dataGridView1.Columns[col_num].AutoSizeMode =                     DataGridViewAutoSizeColumnMode.AllCells;            }            object[] values = new object[reader.FieldCount];            while (reader.Read())            {                reader.GetValues(values);                frm1.dataGridView1.Rows.Add(values);            }        }    }    catch (Exception ex)    {        MessageBox.Show("Error executing command.\n" + ex.Message);    }    finally    {        Conn.Close();    }}
查看完整描述

2 回答

?
桃花長相依

TA貢獻(xiàn)1860條經(jīng)驗 獲得超8個贊

好吧,既然你在打電話Form1 frm1 = new Form1();- 你還期待opens up another instance of form1什么?- 為什么不應(yīng)該new Form1()產(chǎn)生另一個實例?


您將需要獲取已創(chuàng)建的Form1.


例如,請參見 在 c# windows 應(yīng)用程序中查找打開的表單


當(dāng)你找到它時,你可以激活它,例如:


var frm1 = Application.OpenForms[0];

//frm1.Show(this); <- don't need to call Show since its already open

frm1.Activate();

你也應(yīng)該改變你btnExecute_Click的。


private void btnExecute_Click(object sender, EventArgs e)

{

    var frm1 = Application.OpenForms[0] as Form1; //find `Form1` like you want, I only take [0]


    //always create a new instance of SqlConnection here and dispose it with the using Keyword

    //don't use a private field to try to keep the Connection, let the internal Connection pool handle that case

    using (var con = new SqlConnection(@"Data Source=srvr;Initial Catalog =db; User ID =user; Password =pass"))

    {

        try

        {

            con.Open();


            //clean up, Command/Reader with using keyword

            using (var cmd = con.CreateCommand())

            {

                cmd.CommandText = txtQuery.Text;

                using (SqlDataReader reader = cmd.ExecuteReader())

                {

                    //read data

                }

            }

        }

        catch (Exception ex)

        {

            MessageBox.Show("Error executing command.\n" + ex.Message);

        }

    }


    //should activate the `Form1` AFTER the job is done, you can consider if you only want to activate it if the previous Code didn't fail

    frm1.Activate();

}

不要真正了解您在“read_data”例程中所做的事情。


此代碼塊:


frm1.dataGridView1.Columns.Clear();

frm1.dataGridView1.Rows.Clear();

if (reader.HasRows)

{

    DataTable schema = reader.GetSchemaTable();

    int field_num = 0;

    foreach (DataRow schema_row in schema.Rows)

    {

        int col_num = frm1.dataGridView1.Columns.Add(

            "col" + field_num.ToString(),

            schema_row.Field<string>("ColumnName"));

        field_num++;


        frm1.dataGridView1.Columns[col_num].AutoSizeMode = 

            DataGridViewAutoSizeColumnMode.AllCells;

    }


    object[] values = new object[reader.FieldCount];


    while (reader.Read())

    {

        reader.GetValues(values);

        frm1.dataGridView1.Rows.Add(values);

    }

}

嘗試以下是否足夠,將上面代碼中的我的注釋“//讀取數(shù)據(jù)”替換為:


frm1.dataGridView1.AutoGenerateColumns = true; //say to automatically create columns, based on the result inside the datatable

frm1.dataGridView1.Columns.Clear();

var dataTable = new DataTable();

dataTable.Load(dataReader); //load the SqlDataReader into the DataTable

frm1.dataGridView1.DataSource = dataTable; //set the dataGridView's DataSource to the dataTable



查看完整回答
反對 回復(fù) 2022-06-12
?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗 獲得超8個贊

在 form1 中單擊按鈕時,您可以簡單地打開 form2 的一個新實例并在那里完成您的工作,并在關(guān)閉時在 form1 中接收該值?;蛘吣梢酝ㄟ^構(gòu)造函數(shù)將 form1 的實例傳遞到 form2 并從 form2 更新 form1。例如:


var isFormClosed = false;

using(form1 frm = new form1())

{

   // do something here

   frm.ShowDialog();

   isFormClosed = true;

}

或者,如果您希望將 form1 的引用傳遞給 form2,


var isFormClosed = false;

using(form1 frm = new form1(this))

{

   // do something here

   frm.ShowDialog();

   isFormClosed = true;

}

在這里,在 form2 中,您可以簡單地使用 form1 的傳遞引用來更新屬性或網(wǎng)格。


查看完整回答
反對 回復(fù) 2022-06-12
  • 2 回答
  • 0 關(guān)注
  • 176 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號