0

これは非常に簡単な質問かもしれません。

私のアプリケーションには、FormdatagridviewdgvBillと Buttonがある がありbtnClickます。エクステンダーをクリックして を選択して、列を宣言しましたadd columns。フォームを実行した後、 の各列にテキストを書き込みましたdgvBill。クリックするbtnClickと、xml ファイルが作成されますが、データが含まれていません。(なぜデータが追加されていないのですか?)

作成された XML ファイルは次のようになります。

<?xml version="1.0" standalone="yes"?>
<NewDataSet />

以下のコードを試しました:

btnClick イベント

DataSet ds = new DataSet();
ds.WriteXml(@"..\..\Customer_Info\" + lblCustId.Text + ".xml");
dgvBill.DataSource = ds;  /* also tried ds.Tables[0] */

lblCustIdのラベルです。Form

オプション: datagridview の各セルを ComboBox にすることはできますか?

前もって感謝します。

4

5 に答える 5

0

データセットをdgvBillに割り当てる前にXMLを作成していると思います。後を置いてみてください。

于 2012-10-05T06:53:15.810 に答える
0

まず、バインディングソースを作成し、バインディングソースからデータテーブルにデータをロードして、新しいデータセットを作成し、データセットにデータテーブルを追加してから、データセットからXMLファイルに書き込みます。

BindingSource bs = (BindingSource )MyGridView.DataSource;
DataTable dt= (DataTable ) bs.DataSource;
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables[0].WriteXml("E:\\test2.xml"); 
于 2012-10-05T06:53:45.503 に答える
0

列に datapropertyname を設定したことを確認してください。これが私のコードです。これは正常に動作します form3.cs

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;
using System.IO;
 namespace WindowsFormsApplication1
 {
  public partial class Form32 : Form
    {
    public Form32()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("row");
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("UserName", typeof(string));
        dt.Rows.Add(1, "Tamer");
        dt.Rows.Add(2, "Foo");
        ds.Tables.Add(dt);
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "row";
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        string fileName = @"C:\users\tamer\desktop\data.xml";
        if (File.Exists(fileName))
        {
            DataSet ds = new DataSet();
            ds.ReadXml(fileName);
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "row";
        }
    }

    private void Form3_FormClosed(object sender, FormClosedEventArgs e)
    {
        string fileName = @"C:\users\tamer\desktop\data.xml";
        DataSet dataSet = (DataSet)dataGridView1.DataSource;
        dataSet.WriteXml(fileName);

    }
}

}

form3.designer.cs

namespace WindowsFormsApplication1 { partial class Form32 { /// /// 必要なデザイナー変数。/// プライベート System.ComponentModel.IContainer コンポーネント = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.button1 = new System.Windows.Forms.Button();
        this.Id = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.UserName = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Id,
        this.UserName});
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(582, 337);
        this.dataGridView1.TabIndex = 0;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(246, 377);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // Id
        // 
        this.Id.DataPropertyName = "Id";
        this.Id.HeaderText = "Id";
        this.Id.Name = "Id";
        // 
        // UserName
        // 
        this.UserName.DataPropertyName = "UserName";
        this.UserName.HeaderText = "UserName";
        this.UserName.Name = "UserName";
        // 
        // Form32
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(632, 431);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form32";
        this.Text = "Form3";
        this.Load += new System.EventHandler(this.Form3_Load);
        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form3_FormClosed);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.DataGridViewTextBoxColumn Id;
    private System.Windows.Forms.DataGridViewTextBoxColumn UserName;
}

}

于 2013-05-21T04:53:11.780 に答える
0

新しい空のxmlファイル(列名のみを作成したもの)を作成し、データセットオブジェクトでxmlファイルを(フォームロードイベントで)読み取ると、私の問題は解決しました。同じデータセット オブジェクトを使用して、別の xml ファイルに書き込みました (ボタン クリック イベントで)。これは正しい方法ではないと確信していますが、うまくいきました。他に良い提案があればここに投稿してください。

于 2012-10-05T10:07:34.757 に答える
0

を使用dataGridView1.EndEdit();して作成するXML

于 2012-10-05T06:45:14.783 に答える