在GRID的CellMouseClick事件點選GRID時順便帶值給TEXTBOX

private void dgvData_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
   //取得目前滑鼠點選的CELL值
   //textBox2.Text = dgvData.CurrentCell.Value.ToString();
   //取得目前滑鼠點選的ROW值
   textBox2.Text = dgvData.CurrentRow.Cells[1].Value.ToString();
   textBox3.Text = dgvData.CurrentRow.Cells[2].Value.ToString();
   textBox4.Text = dgvData.CurrentRow.Cells[3].Value.ToString();

}

將DataTable 繫結到 TEXT,在FORM1_LOAD執行一次即可繫結,在其他事件繫結重複執行會發生

這會造成集合中的兩個繫結與相同的屬性產生繫結。參數名稱: binding】的錯誤

private void Form1_Load(object sender, EventArgs e)
{
  using (SqlConnection dataConnection = new SqlConnection(conn.DB63_PTERP))
  {
    dataConnection.Open();

    SqlCommand cmd = new SqlCommand("select ACPT_VOU_NO,ACPT_VOU_TYPE,CASH_AMT

                                                               from AC_ACPT_M  

                                                               WHERE 1=0",dataConnection);

    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    dgvData.DataSource = dt;
    BindingSource bs = new BindingSource();
    bs.DataSource = dt;

    textBox2.DataBindings.Add("text", bs, "ACPT_VOU_NO", true);
    textBox3.DataBindings.Add("text", bs, "ACPT_VOU_TYPE", true);
    textBox4.DataBindings.Add("text", bs, "CASH_AMT", true);

  }

}

arrow
arrow

    YCH 發表在 痞客邦 留言(1) 人氣()