日期:2014-05-18  浏览次数:20949 次

WinForm中的數據操作
如果僅僅對一張表操作,並且直接在DataGridView編輯,我是這樣做的:
 DataSet DS = null;
 SqlDataAdapter adapter = null;

  //自定義函數,獲取所有貨幣,Load時調用
  private void BindCurrency()
  {
  try
  {
  adapter = new SqlDataAdapter("select * from TB_Currency", ConLocal );

  SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

  DS = new DataSet();

  ConLocal.Open();

  adapter.Fill(DS, "TB_Currency");

  BSCur.DataSource = DS; //BindingSource
  BSCur.DataMember = "TB_Currency";  

  dgvCur.DataSource = BSCur;

  }
  catch (Exception ex)
  {
  throw ex;
  }
  finally
  {
  ConLocal.Close();
  }
  }
  //窗體初始化
  private void FrmBCurrency_Load(object sender, EventArgs e)
  {
  try
  {
  BindCurrency();  
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }

  //保存
  private void btnSave_Click(object sender, EventArgs e)
  {
  BSCur.EndEdit();  

  adapter.Update(DS, "TB_Currency");  
  }


如果是主從表,主表在文本框編輯,我現在是下面的方法,每個字段的賦值:
  DataRow tRow = DSTotal.Tables["TA_CostMaster"].NewRow();

  tRow["ManageNo"] = txtManageNo.Text;
  tRow["t_mitm"] = txtt_mitm.Text;
  tRow["t_dsca"] = txtt_dsca.Text;
 從表在DataGridView,新增時我會遍歷表格,然後同樣每個字段賦值。

麻煩各位告訴我更簡便的方法!謝謝


------解决方案--------------------
参考DataRelation

主从表绑定后 主表添加完数据 从表是可以自动获取相关数据的.
------解决方案--------------------
帮顶..
------解决方案--------------------
CurrencyManager 主表的数据Table 与 控件绑定