c# - 在 Windows 窗体中选择和更新数据表

标签 c# xml winforms datagridview datatable

XML

<?xml version="1.0" encoding="utf-8"?>
<TrackCollection>
  <Tracks>
      <Id>1</Id>
      <Artist>Artist 1</Artist>
      <Album>His album</Album>
      <Filepath>C://music//song.mp3</Filepath>
      <Id>2</Id>
      <Artist>Artist 1</Artist>
      <Album>His album</Album>
      <Filepath>C://music//song2.mp3</Filepath>
      <Id>3</Id>
      <Artist>Artist 1</Artist>
      <Album>His album</Album>
      <Filepath>C://music//song2.mp3</Filepath>

  </Tracks>
</TrackCollection>

 DataSet dsStore = new DataSet();
 DataTable dt = new DataTable();
 public void loadXmlTracks()
    {
        //TrackCollection tracks = null;
        string path = "..//..//..//test.xml";

        //XmlSerializer serializer = new XmlSerializer(typeof(TrackCollection));

        //StreamReader reader = new StreamReader(path);
        //tracks = (TrackCollection)serializer.Deserialize(reader);
        //reader.Close();
        dsStore.ReadXml(path);
        dt = dsStore.Tables["Track"];

        // finally bind the data to the grid
        LoadGrid(dt);


    }

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    int i;
    int j;

    i = e.RowIndex;
    j = e.ColumnIndex;

    string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
    string value = dataGridView1.Rows[i].Cells[j].Value.ToString();

    DataRow[] row = dt.Select("Id=" + i, "Id");
    foreach (DataRow r in row)
    {
        r[j] = value;
        r.AcceptChanges();
    }

public void LoadGrid(DataTable dt)
    {
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = dt;
    }

编辑:这是我正在加载的 xml 文件,然后我将其插入到数据表中,然后我使用该数据表加载我的数据 GridView 。我想要完成的是,如果用户编辑 datagridview 中的单元格以更新我的数据表中的该行,那么当我关闭应用程序时,它会保存回 XML 文件。

我不确定且无法弄清楚的是如何将更改保存回我的数据表,如果我有需要更改的行的 ID,执行循环是否正确?

我尝试了 RowFilter 并进行了更改,但是当我重新加载我的 datagridview 时,它只显示我更改的行..

最佳答案

在你的代码中我看到了一些注释:

  1. 当您调用“AcceptChanges”时,您将行更改的行状态从已更改、已添加...更改为未更改,因此“dataadaptor”的 upadte 命令无法在您的“数据表”中找到更改记录,并且您的更改不适用于数据库。
  2. 如果您知道您的 ID 并且该 ID 是数据表中的主键,那么您可以使用“dt.Rows.Find(i)”,它会为您提供具有您想要的 ID 的数据行

但是如果您的数据 gridview 绑定(bind)到您的数据表,则当您更改一个单元格时没有必要做这样的事情,这些更改将应用​​于您的数据表,唯一的是,当您更改一个单元格时,更改将不适用于您的数据表,直到您从一行移动到另一行。

编辑: 删除函数“dataGridView1_CellEndEdit”并添加表单关闭事件处理程序,如下所示:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
               string path = "..//..//..//test.xml";
               dataGridView1.EndEdit();
               if(dsStore.GetChanges()!=null)
               {
                   dsStore.WriteXml(path);
               }


}

关于c# - 在 Windows 窗体中选择和更新数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8479875/

相关文章:

c# - 使用反射(Type.GetProperties)获取 DependencyProperties?

c# - 在 Debian 上,运行单错误 CS8027 : Error running pkg-config

android - 将 View 放置在 android 中 CoordinatorLayout 中的另一个 View 下方

xml - XSLT - 将值连接到现有值

java - 将 APK 文件复制到文件夹中?

.net - 寻找有关 Windows Forms .Net Resizing Component 的推荐

c# - 向用户控件添加滚动条

C# DateTime 从 linq 到分钟

c# - 仅在 Visual Studio 2010 中调试期间,简单的 Rx 代码在 Windows 窗体中默默失败

c# - C# windows 10 应用程序中的 ssh 连接