質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

DataGrid

GUIの一種であり、データを表の形式でみることが可能です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

2回答

1997閲覧

C#のDataGridViewによるXMLファイルの読み書き (書き込み編)

Reindeer_Csharp

総合スコア13

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

DataGrid

GUIの一種であり、データを表の形式でみることが可能です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

1グッド

0クリップ

投稿2022/04/28 07:09

編集2022/04/28 07:23

前提

 C#でDataGridViewのXMLファイル読み込みと書き込みのコーディング途中

実現したいこと

ここに実現したいことを箇条書きで書いてください。

  • XMLをDataGridViewに読み込みをしたい(場所は自由に参照)
  • XMLにDataGridViewのsource書き込みをしたい(場所は自由に参照)

本文

C#歴1週間です。
今回はアルコール量を検査して検査したデータを入力し、XML(CSVは諦めました)に書き込みんだり読み込みしてデータグリッドビューに反映させたいのですが、いくら探してもコンソールでの出力しかなく、データグリッドビューからの読み書きは検索しても出てきませんでした。
私の知識不足だと思いますがもしよければアドバイスしてくれると嬉しいです。
以下に試してみたソースコード一覧を貼り付けます。

該当のソースコード

C#

1 private void SAVEB_Click(object sender, EventArgs e) 2 { 3 4 // FileStream fs = new FileStream("rog.xml", FileMode.Open); 5 System.IO.StreamWriter sr = new System.IO.StreamWriter("rog.xml", false, System.Text.Encoding.GetEncoding("utf-8")); 6 dataSet.WriteXml(sr); 7 sr.Close(); 8 9 MessageBox.Show("正常に入力しました", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 10 Clear(); 11 }

全文ソースコード

把握用 

このコードは経験ある人が見たらもっと短縮できそうだと思います。

C#

1using System; //アセンブリ参照 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Xml; 6using System.Drawing; 7using System.Linq; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11using System.IO; 12using Microsoft.VisualBasic; 13using System.Data.OleDb; 14using System.Xml.Linq; 15using Microsoft.VisualBasic.FileIO; 16namespace sample  17{ 18 public partial class Form1 : Form 19 { 20 public Form1() //接続処理 21 { 22 InitializeComponent(); 23 try              //接続処理 24 { // 接続を試みます 25 serialPort.PortName = ("COM5"); 26 serialPort.BaudRate = 9600; 27 serialPort.Open(); 28 } 29 catch 30 { 31 ; 32 } 33 34 } 35 private void Form1_Load_1(object sender, EventArgs e) //起動時XML初期読み込み まだ組み込まない 36 { 37 38 } 39 private void SeRe(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) //書き込み処理 40 { 41 try //書き込みを行います テキストボックス式 42 { 43 SetText(serialPort.ReadExisting()); 44 } 45 catch 46 { 47 ; 48 } 49 } 50 delegate void SetTextCallback(string text); 51 private void SetText(string text) //文章入力処理 52 { 53 if (TbxSeData.InvokeRequired) 54 { 55 SetTextCallback d = new SetTextCallback(SetText); 56 Invoke(d, new object[] { text }); 57 } 58 else 59 { 60 TbxSeData.AppendText(text); 61 62 } 63 } 64 private void BtSc_Click(object sender, EventArgs e) //スキャンボタン 65 { 66 if (string.IsNullOrEmpty(TbxSeData.Text))  //テキストボックスに行った文字列を解析し、処理したあとに変数にします。 67 { 68 MessageBox.Show("未スキャンです", "", MessageBoxButtons.OK, MessageBoxIcon.Error); //例外が起きたら弾きます 69 goto SCER; 70 } //文章解析処理 71 string txt = TbxSeData.Text; 72 string seta = txt.Remove(txt.IndexOf("mg/L")); 73 string setb = seta.Substring(seta.LastIndexOf(":")); 74 string ALs = setb.Remove(0, 1); 75 double ALi = double.Parse(ALs); 76 LaAlcohol.Text = ALs + ("mg/L"); 77 if (ALi >= 0.15) 78 { 79 this.TbClear.BackColor = Color.Red; 80 this.Scanbox.BackColor = Color.Red; 81 this.Scanbox.Text = "X"; 82 this.Scanbox.ForeColor = Color.White; 83 TbxResult.Text = "お酒を検出しました"; 84 } //0.15以上の処理 85 else 86 { 87 this.TbClear.BackColor = Color.Green; 88 this.Scanbox.BackColor = Color.Green; 89 this.Scanbox.Text = "O"; 90 this.Scanbox.ForeColor = Color.White; 91 TbxResult.Text = "お酒を検出しませんでした"; 92 } //0.15未満の処理 93 MessageBox.Show("正常にスキャンしました", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 94 SCER: 95 ; 96 } 97 private void BtFIN_Click(object sender, EventArgs e) //終了ボタン 98 { 99 FIN(); 100 } 101 private void BtRe_Click(object sender, EventArgs e) //リセットボタン 102 { 103 Clear(); 104 MessageBox.Show("リセットしました", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 105 } 106 private void BtSe_Click(object sender, EventArgs e) //入力用ボタン 107 { 108 Save(); 109 } 110 private void PictureBox2_Click(object sender, EventArgs e) //使い方ボタン 111 { 112 Form2 form2 = new Form2(); 113 form2.Show(); 114 } 115 private void PictureBox3_Click(object sender, EventArgs e) //管理用ボタン 116 { 117 Form3 form3 = new Form3(); 118 form3.Show(); 119 } 120 private void PictureBox4_Click(object sender, EventArgs e) //使い道ないボタン 無駄なので見なくていいです。 121 { 122 MessageBox.Show("おさけ", "おさーけ", MessageBoxButtons.OK, MessageBoxIcon.Information); 123 MessageBox.Show("アルコール量が0.15mg/Lを超えての運転は違法です", "飲酒運転の基準", MessageBoxButtons.OK, MessageBoxIcon.Error); 124 } 125 private void Form1_KeyPress(object sender, KeyPressEventArgs e) //キー入力系イベント 126 { 127 if (e.KeyChar == (char)Keys.Escape) //終了イベント ESCキー 128 { 129 FIN(); 130 } 131 } 132 private void Clear() //記録削除系イベント 133 { 134 LaAlcohol.Text = "アルコール量"; 135 TbxResult.Text = ""; 136 TbClear.BackColor = Color.White; 137 Scanbox.BackColor = Color.RoyalBlue; 138 Scanbox.ForeColor = Color.Snow; 139 Scanbox.Text = "?"; 140 TbxSeData.Text = ""; 141 } 142 private void Save() //保存系イベント 143 { 144 DateTime now = DateTime.Now; 145 if (NameCombo.Text == ("-名前を選択-")) 146 { 147 MessageBox.Show("名前が未入力です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 148 goto SeER; 149 } 150 if (LaAlcohol.Text == ("アルコール量")) 151 { 152 MessageBox.Show("未スキャンです", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 153 goto SeER; 154 } 155 Xmlgrid.Rows.Add(now, NameCombo.Text, LaAlcohol.Text); 156 Clear(); 157 MessageBox.Show("正常に入力しました", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 158 SeER: 159 ; 160 } 161 private void FIN() //終了イベント 162 { 163 164 DialogResult result = MessageBox.Show("終了しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 165 if (result == System.Windows.Forms.DialogResult.Yes) 166 { 167 Application.Exit(); 168 } 169 } 170// ここから該当のソースコード 171 private void SAVEB_Click(object sender, EventArgs e) //XML書き込み エラー 172 { 173 // FileStream fs = new FileStream("rog.xml", FileMode.Open); 174 System.IO.StreamWriter sr = new System.IO.StreamWriter("rog.xml", false, System.Text.Encoding.GetEncoding("utf-8")); 175 dataSet.WriteXml(sr); 176 sr.Close(); 177 178 MessageBox.Show("正常に入力しました", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 179 Clear(); 180//該当ここまで 181 } 182 private void FINEX() //強制終了 未使用 183 { 184 Application.Exit(); 185 } 186 private void Label2_Click(object sender, EventArgs e) //デバッグ用 直接入力 187 { 188 TbxSeData.Text = ":0.00 mg/L" 189 } 190 191 private void Btload_Click(object sender, EventArgs e)  //XML読み込み 192 { 193 194 } 195 } 196}

デザイン画像
イメージ説明
各割り当て
飾りのオブジェクトなどは対象外です

オブジェクト名称プロパティの名称イベント名イベント名称
FormForm1Form1_Load_1Load
DataGridViewXmlgrid----
Button1BtScanBtSc_ClickClick
Button2BtFINBtFIN_ClickClick
Button3BtResetBtRe_ClickClick
Button4BtSendBtSe_ClickClick
Button5BtloadBtload_ClickClick
Button6BtSaveSAVEB_ClickClick
Label1Scanbox----
Label2LaAlcohol----
Label3Lbstring_debugLabel2_clickClick
Textbox1TbxResut----
Textbox2TbxSeData----
Textbox3TbClear----
Textbox4Passtext----
ComboBoxNameCombo----
PictureBox1boxOsakePixtureBox4_ClickClick
PictureBox2boxAdminPictureBox3_ClickClick
PictureBox3boxQAPictureBox2_ClickClick
SerialPortSerialPortSeReDataReceived
DataSetdataSet----

試したこと

いろいろなウェブサイトを調べ、LINQやVBの組み込みなどしてみました。が、どれも合わず、知り合いのひとからコードを教えてもらい、このコードを組みました。

補足情報(FW/ツールのバージョンなど)

2022/04/28 現在
FW:.NET FrameWork 4.7.2
開発環境 MicrosoftVisualStudio Community  ver.17.0.1
開発言語 C#

初めての質問投稿なのでこういう質問の書き方がいいなどのアドバイスもあると今後の書き込みの助けになります、もしよろしければお願いします。

読み込みはまた別の機会にコーディングしようと思います。

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Reindeer_Csharp

2022/04/28 07:18

追記:本来はデバッグ用のグループボックスは見えないようになっています。
退会済みユーザー

退会済みユーザー

2022/04/30 02:01

その後がどうなりましたか? このスレッドの課題「DataGridViewによるXMLファイルの読み書き」が解決したらクローズしてください。解決してなくてまだ質問があるならそれを書いてください。
guest

回答2

0

いくら探してもコンソールでの出力しかなく、データグリッドビューからの読み書きは検索しても出てきませんでした。

そうですか?だとすると検索ワードが悪いんでしょうか??
c# datagridview xml - Google 検索

XMLをDataGridViewに読み込みをしたい(場所は自由に参照)
XMLにDataGridViewのsource書き込みをしたい(場所は自由に参照)

SerialPortなんかは動いているようですし、文字数もギリギリなんで最低限に絞ります(行追加・xml読み書きのみ)

cs:Form1.cs

1using System; 2using System.Windows.Forms; 3 4namespace Qdb5e5dist8ast6 5{ 6 public partial class Form1 : Form 7 { 8 public Form1() => InitializeComponent(); 9 10 private void AddButton_Click(object sender, EventArgs e) 11 => dataTable1.Rows.Add(DateTime.Now, textBox1.Text, numericUpDown1.Value); 12 13 private void LoadButton_Click(object sender, EventArgs e) 14 { 15 if (openFileDialog1.ShowDialog() == DialogResult.OK) 16 { 17 dataSet1.Clear(); 18 dataSet1.ReadXml(openFileDialog1.FileName); 19 } 20 } 21 22 private void SaveButton_Click(object sender, EventArgs e) 23 { 24 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 25 dataSet1.WriteXml(saveFileDialog1.FileName); 26 } 27 } 28}

コード的にはこれだけです。あとはすべてデザイナで、できてしまうということです。

  1. DataSetDataTableを作る
  2. DataTableDataColumnを作る
  3. DataGridViewDataSourceDataSetを、DataMemberDataTableを設定

設定法は↓が画像付きで分かりやすかったです(C++ですがデザイナは同じなので)
[C++/CLI] DataGridViewとXML読み込み、保存 - Qiita

文字数ギリギリなので、不格好なところもありますがこんな感じです(スペースをタブに置換しました)

cs:Form1.Designer.cs

1namespace Qdb5e5dist8ast6 2{ 3 partial class Form1 4 { 5 /// <summary> 6 /// 必要なデザイナー変数です。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 使用中のリソースをすべてクリーンアップします。 12 /// </summary> 13 /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows フォーム デザイナーで生成されたコード 24 25 /// <summary> 26 /// デザイナー サポートに必要なメソッドです。このメソッドの内容を 27 /// コード エディターで変更しないでください。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.addButton = new System.Windows.Forms.Button(); 32 this.loadButton = new System.Windows.Forms.Button(); 33 this.saveButton = new System.Windows.Forms.Button(); 34 this.textBox1 = new System.Windows.Forms.TextBox(); 35 this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); 36 this.dataSet1 = new System.Data.DataSet(); 37 this.dataTable1 = new System.Data.DataTable(); 38 this.dataColumn1 = new System.Data.DataColumn(); 39 this.dataColumn2 = new System.Data.DataColumn(); 40 this.dataColumn3 = new System.Data.DataColumn(); 41 this.dataGridView1 = new System.Windows.Forms.DataGridView(); 42 this.dateDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 43 this.nameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 44 this.brACDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 45 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 46 this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); 47 ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); 48 ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); 49 ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit(); 50 ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 51 this.SuspendLayout(); 52 // 53 // addButton 54 // 55 this.addButton.Location = new System.Drawing.Point(437, 8); 56 this.addButton.Name = "addButton"; 57 this.addButton.Size = new System.Drawing.Size(75, 23); 58 this.addButton.TabIndex = 2; 59 this.addButton.Text = "Add"; 60 this.addButton.UseVisualStyleBackColor = true; 61 this.addButton.Click += new System.EventHandler(this.AddButton_Click); 62 // 63 // loadButton 64 // 65 this.loadButton.Location = new System.Drawing.Point(356, 422); 66 this.loadButton.Name = "loadButton"; 67 this.loadButton.Size = new System.Drawing.Size(75, 23); 68 this.loadButton.TabIndex = 4; 69 this.loadButton.Text = "Load"; 70 this.loadButton.UseVisualStyleBackColor = true; 71 this.loadButton.Click += new System.EventHandler(this.LoadButton_Click); 72 // 73 // saveButton 74 // 75 this.saveButton.Location = new System.Drawing.Point(437, 422); 76 this.saveButton.Name = "saveButton"; 77 this.saveButton.Size = new System.Drawing.Size(75, 23); 78 this.saveButton.TabIndex = 5; 79 this.saveButton.Text = "Save"; 80 this.saveButton.UseVisualStyleBackColor = true; 81 this.saveButton.Click += new System.EventHandler(this.SaveButton_Click); 82 // 83 // textBox1 84 // 85 this.textBox1.Location = new System.Drawing.Point(205, 10); 86 this.textBox1.Name = "textBox1"; 87 this.textBox1.Size = new System.Drawing.Size(100, 19); 88 this.textBox1.TabIndex = 0; 89 // 90 // numericUpDown1 91 // 92 this.numericUpDown1.DecimalPlaces = 2; 93 this.numericUpDown1.Increment = new decimal(new int[] { 94 1, 95 0, 96 0, 97 131072}); 98 this.numericUpDown1.Location = new System.Drawing.Point(311, 11); 99 this.numericUpDown1.Name = "numericUpDown1"; 100 this.numericUpDown1.Size = new System.Drawing.Size(120, 19); 101 this.numericUpDown1.TabIndex = 1; 102 // 103 // dataSet1 104 // 105 this.dataSet1.DataSetName = "NewDataSet"; 106 this.dataSet1.Tables.AddRange(new System.Data.DataTable[] { 107 this.dataTable1}); 108 // 109 // dataTable1 110 // 111 this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] { 112 this.dataColumn1, 113 this.dataColumn2, 114 this.dataColumn3}); 115 this.dataTable1.TableName = "Table1"; 116 // 117 // dataColumn1 118 // 119 this.dataColumn1.ColumnName = "Date"; 120 this.dataColumn1.DataType = typeof(System.DateTime); 121 // 122 // dataColumn2 123 // 124 this.dataColumn2.ColumnName = "Name"; 125 // 126 // dataColumn3 127 // 128 this.dataColumn3.ColumnName = "BrAC"; 129 this.dataColumn3.DataType = typeof(double); 130 // 131 // dataGridView1 132 // 133 this.dataGridView1.AllowUserToAddRows = false; 134 this.dataGridView1.AutoGenerateColumns = false; 135 this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 136 this.dateDataGridViewTextBoxColumn, 137 this.nameDataGridViewTextBoxColumn, 138 this.brACDataGridViewTextBoxColumn}); 139 this.dataGridView1.DataMember = "Table1"; 140 this.dataGridView1.DataSource = this.dataSet1; 141 this.dataGridView1.Location = new System.Drawing.Point(12, 41); 142 this.dataGridView1.Name = "dataGridView1"; 143 this.dataGridView1.ReadOnly = true; 144 this.dataGridView1.Size = new System.Drawing.Size(500, 375); 145 this.dataGridView1.TabIndex = 3; 146 // 147 // dateDataGridViewTextBoxColumn 148 // 149 this.dateDataGridViewTextBoxColumn.DataPropertyName = "Date"; 150 this.dateDataGridViewTextBoxColumn.HeaderText = "日時"; 151 this.dateDataGridViewTextBoxColumn.Name = "dateDataGridViewTextBoxColumn"; 152 // 153 // nameDataGridViewTextBoxColumn 154 // 155 this.nameDataGridViewTextBoxColumn.DataPropertyName = "Name"; 156 this.nameDataGridViewTextBoxColumn.HeaderText = "名前"; 157 this.nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn"; 158 // 159 // brACDataGridViewTextBoxColumn 160 // 161 this.brACDataGridViewTextBoxColumn.DataPropertyName = "BrAC"; 162 this.brACDataGridViewTextBoxColumn.HeaderText = "アルコール量(mg/L)"; 163 this.brACDataGridViewTextBoxColumn.Name = "brACDataGridViewTextBoxColumn"; 164 // 165 // openFileDialog1 166 // 167 this.openFileDialog1.Filter = "XMLファイル (*.xml)|*.xml"; 168 // 169 // saveFileDialog1 170 // 171 this.saveFileDialog1.Filter = "XMLファイル (*.xml)|*.xml"; 172 // 173 // Form1 174 // 175 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 176 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 177 this.ClientSize = new System.Drawing.Size(524, 457); 178 this.Controls.Add(this.numericUpDown1); 179 this.Controls.Add(this.dataGridView1); 180 this.Controls.Add(this.textBox1); 181 this.Controls.Add(this.saveButton); 182 this.Controls.Add(this.loadButton); 183 this.Controls.Add(this.addButton); 184 this.Name = "Form1"; 185 this.Text = "Form1"; 186 ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); 187 ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); 188 ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit(); 189 ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 190 this.ResumeLayout(false); 191 this.PerformLayout(); 192 193 } 194 195 #endregion 196 197 private System.Windows.Forms.Button addButton; 198 private System.Windows.Forms.Button loadButton; 199 private System.Windows.Forms.Button saveButton; 200 private System.Windows.Forms.TextBox textBox1; 201 private System.Windows.Forms.NumericUpDown numericUpDown1; 202 private System.Data.DataSet dataSet1; 203 private System.Windows.Forms.DataGridView dataGridView1; 204 private System.Data.DataTable dataTable1; 205 private System.Data.DataColumn dataColumn1; 206 private System.Data.DataColumn dataColumn2; 207 private System.Data.DataColumn dataColumn3; 208 private System.Windows.Forms.DataGridViewTextBoxColumn dateDataGridViewTextBoxColumn; 209 private System.Windows.Forms.DataGridViewTextBoxColumn nameDataGridViewTextBoxColumn; 210 private System.Windows.Forms.DataGridViewTextBoxColumn brACDataGridViewTextBoxColumn; 211 private System.Windows.Forms.OpenFileDialog openFileDialog1; 212 private System.Windows.Forms.SaveFileDialog saveFileDialog1; 213 } 214}

初めての質問投稿なのでこういう質問の書き方がいいなどのアドバイスもあると今後の書き込みの助けになります、もしよろしければお願いします。

とてもよく書けていると思います。初質問でここまで体裁の整ったものは珍しいです。
かなり時間がかかったのではないですか?お疲れさまでした(わたしも回答文を書くのに数時間かかってしまうこともあります^^;

回答者目線であえて指摘させていただくと、

  • 質問者のスキルがわからない
    初質問で提示コードも他人のものとなると、体裁や文体でしか想像できません。
    C#歴1週間(ほかの言語ならプロorプロ級)なのか、プログラミング歴1週間なのかで全く回答も変わってきます(わたしは「プログラミング歴1週間ではなさそうだな」という印象を持ちました)
  • 各割り当て
    こういうのがあるとわかりやすいですが、そもそも書く必要のないくらい絞っていただきたいです。
    できれば新規に(質問・検証用の)プロジェクトを作っていただくのが望ましいです。
  • 命名
    これはReindeer_Csharpさんの責任ではないでしょうが、変数名やメソッド名が分かりにくいです。
    「ボタンはBtから始める」等のルールはいいのですが、一貫性に欠け読みにくいです。
    イベントハンドラも「ボタン名_Click」でいいのに、不必要に省略されています。

投稿2022/04/29 01:59

編集2023/07/30 07:41
TN8001

総合スコア9326

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2022/04/29 02:03

収まりそうもないのでここで補足 出力例 <?xml version="1.0" standalone="yes"?> <NewDataSet> <Table1> <Date>2022-04-29T11:00:00.1054801+09:00</Date> <Name>寺尾 太郎</Name> <BrAC>0</BrAC> </Table1> </NewDataSet> NewDataSet・Table1の部分は、DataSetName・TableName 名前を考えるのが面倒だったので既定値です^^;
Reindeer_Csharp

2022/05/01 23:34

回答ありがとうございます。とても参考になりました!
guest

0

ベストアンサー

以下の記事が参考になりませんか? XML ファイルからデータを取得して Windows Forms アプリの DataGridView に表示し、それをユーザーが編集して結果を XML ファイルに書き戻すサンプルです。

XML ファイルを DataGridView に表示
http://surferonwww.info/BlogEngine/post/2019/04/26/show-content-of-xml-file-on-datagridview.aspx

CSV の例もあります。上の記事の XML ファイルを CSV ファイルに替えたサンプルです。

CSV ファイルを DataGridView に表示
http://surferonwww.info/BlogEngine/post/2020/09/11/show-date-in-csv-file-on-datagridview.aspx

投稿2022/04/28 07:24

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Reindeer_Csharp

2022/04/28 07:27

一度上記URLを参考にしてコーディングしてみようと思います。 多分また壁にぶつかると思うのでまだ未解決の状態にしておきます。
退会済みユーザー

退会済みユーザー

2022/04/28 07:48

いろいろなことを一度にやりすぎだと思います。回答者の方もそのいろいろを追いかけてコメントするのは無理があります。 特定の部分、今回は表題の「DataGridViewによるXMLファイルの読み書き」の部分に絞って、コードのその部分だけのサンプルを書いて、それを質問に載せて、どこで躓いていて、何が分かれば解決するかを説明することをお勧めします。
Reindeer_Csharp

2022/04/28 07:56

わかりました。次の質問からそうしようと思います。
退会済みユーザー

退会済みユーザー

2022/04/30 02:01

その後がどうなりましたか? このスレッドの課題「DataGridViewによるXMLファイルの読み書き」が解決したらクローズしてください。解決してなくてまだ質問があるならそれを書いてください。
Reindeer_Csharp

2022/05/01 23:19

返信が遅れてしまいました。 色々探してみて無事コードが書けました。 教えていただきありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問