c#数据视图单向绑定

本文关键字:绑定 视图 数据 | 更新日期: 2023-09-27 18:10:44

我有一个datagridview DGV1,我想从XML文件中读取并将DGV1绑定到它但是一旦在DGV1中进行了更改,我希望将其存储在列表中比如file.

XML --> DGV1 --> list

我现在尝试的是下面提到的

    public DataTable PopulateDGV1()
    {
        DataSet dataSet = new DataSet();
        dataSet.ReadXml(GlobalParam.SettingsXMLPath);
        return dataSet.Tables[0];
    }
    private void LoadDGV1_Click(object sender, EventArgs e)
    {
        DGV1.DataSource = _control.PopulateDGV1();
        if (DGV1.Columns[0] != null) DGV1.Columns[0].ReadOnly = true;//make the most left column read only 
    }

我想从save

    List<string> data = new List<String>();
    foreach (DataGridViewRow item in DGV1.Rows)
    {
        if (item.Cells[1].Value!=null)
        data.Add(item.Cells[1].Value.ToString());
    }

我在这里错过了什么?

c#数据视图单向绑定

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private List<Test> DataSource;
    public void PopulateDGV1()
    {
        DataSource=new List<Test>() { new Test() { Age = 2, Name = "Test" } };
        //TODO:read your XML file
        DGV1.DataSource = DataSource;
    }
    private void LoadDGV1_Click(object sender, EventArgs e)
    {
        PopulateDGV1();
    }
}
public class Test
{
    public string Name { get; set; }
    public int Age { get; set; }
}