c#winform将DataGridView中的数据写入XML

时间:2017-11-30 21:34:11

标签: c# xml winforms datagridview

我想我遇到了问题:/ 我想将datagridview中的数据写入XML。但我无能为力。在此之前,我可以使用按钮添加行到datagridview,我可以写任何自由字段,但我无法从可用空间读取数据。我需要做什么?(我的英语不太清楚我知道:()

这些是我用来将空闲空间插入DataGridView的代码。

dataGridView1.ColumnCount = 10;
dataGridView1.Columns[0].Name = "Hesap Kodu*";
DataGridViewColumn column0 = dataGridView1.Columns[0];
column0.Width = 80;
dataGridView1.Columns[1].Name = "Hesap Adı*";
DataGridViewColumn column1 = dataGridView1.Columns[1];
column1.Width = 80;
dataGridView1.Columns[2].Name = "Evrak No";
DataGridViewColumn column2 = dataGridView1.Columns[2];
column2.Width = 80;
dataGridView1.Columns[3].Name = "Evrak Tarihi";
DataGridViewColumn column3 = dataGridView1.Columns[3];
column3.Width = 80;
dataGridView1.Columns[4].Name = "BT";
DataGridViewColumn column4 = dataGridView1.Columns[4];
column4.Width = 30;
dataGridView1.Columns[5].Name = "Açıklama";
DataGridViewColumn column5 = dataGridView1.Columns[5];
column5.Width = 80;
dataGridView1.Columns[6].Name = "Borç(TL)";
DataGridViewColumn column6 = dataGridView1.Columns[6];
column6.Width = 80;
dataGridView1.Columns[7].Name = "Alacak(TL)";
DataGridViewColumn column7 = dataGridView1.Columns[7];
column7.Width = 80;
dataGridView1.Columns[8].Name = "Evrak No";
DataGridViewColumn column8 = dataGridView1.Columns[8];
column8.Width = 80;
dataGridView1.Columns[9].Name = "Miktar";
DataGridViewColumn column9 = dataGridView1.Columns[9];
column9.Width = 80;
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Add(chk);
chk.HeaderText = "BABS";
chk.Name = "chk";
DataGridViewColumn column10 = dataGridView1.Columns[10];
column10.Width = 50;
dataGridView1.Rows.Add("", "", "", "", "", "", "", "","");

1 个答案:

答案 0 :(得分:0)

以下是使用DataTable的解决方案

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("Hesap Kodu*", typeof(string));
            dt.Columns.Add("Hesap Adı*", typeof(string));
            dt.Columns.Add("Evrak No", typeof(int));
            dt.Columns.Add("Evrak Tarihi", typeof(string));
            dt.Columns.Add("BT", typeof(string));
            dt.Columns.Add("Açıklama", typeof(string));
            dt.Columns.Add("Borç(TL)", typeof(string));
            dt.Columns.Add("Alacak(TL)", typeof(string));
            dt.Columns.Add("Miktar", typeof(string));

            dt.Rows.Add(new object[] { "A", "B", 1, "C", "D", "E", "F", "G", "H" });

            dataGridView1.DataSource = dt;

            dt.WriteXml("Filename");

        }
    }
}