合理使用DataSet和DataGridView讀取xml檔案到DataGridView

來源:互聯網
上載者:User
      呵呵!DataSet和DataGridView及xml大家都熟悉吧!這裡就不做介紹了。咱們直奔主題!這裡給出關鍵區段代碼!僅供參考

Code
   //我這裡是寫在表單的載入事件裡面
private void Form1_Load(object sender, EventArgs e)
{
   //先執行個體化一個DataSet對象用來讀取xml檔案中的資料
  DataSet set = new DataSet();
  //用set讀取xml檔案
  set.ReadXml(@"E:\XMLFile.xml");
  //設定dataGridView的資料來源
  dataGridView.DataSource = set.Tables[0];
}

     既然xml檔案能讀取到DataGridView中,哪麼能否在DataGridView中修改xml檔案的資料了?答案是肯定的!其實代碼也很簡單!

Code
1//按鈕的單擊事件中
2private void button_Click(object sender, EventArgs e)
3{
4//同樣是調用(DataSet)set的WriteXml()方法其中傳遞要修改的xml檔案地址
5set.WriteXml(@"E:\XMLFile.xml");
6//此處的set對象是上文執行個體化的set對象 因在同一個表單中所以使用了同一個對象
7}
8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.