私のユーザーコントロールをDGVにバインドするデータに問題があります。コントロールの1つのテキストボックスにのみバインドすると機能します。しかし、私は1つ以上のテキストボックスをバインドするとき、最初の作品のみ。助けてください。私はC#の初心者です。以下はコードです。ユーザーコントロールの2つ以上のテキストボックスをDGVにバインドする。最初のテキストボックスのみが機能します
- 私はプロジェクトTest100にユーザーコントロール "MyBox" を加え(Test100という名前の)ウィンドウフォームアプリ
- を作成し、IはMyBoxでそこに2つのテキストボックス(textBox1テキストボックスとTextBox2を)
を加えました。 csが、私は次のようしている:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace Test100 public partial class MyBox : UserControl { public string Box1 { get { return textBox1.Text; } set { textBox1.Text = value; } } public string Box2 { get { return textBox2.Text; } set { textBox2.Text = value; } } public MyBox() { InitializeComponent(); } }
構築した後、私は自分のフォーム1を開き、:私をForm1にMyBoxコントロールを追加し、フォーム1にDGVを追加し、フォームをリフレッシュするためのボタンを追加しました。データリストはForm1.csで直接生成されます。 Form1.csのは、以下に記載されている:開始デバッグ後
public partial class Form1 : Form { private List<Track> trackList = new List<Track>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { trackList.Add(new Track { TrackID = 1234, Name = "Name1111" }); trackList.Add(new Track { TrackID = 5678, Name = "Name2222" }); trackList.Add(new Track { TrackID = 9012, Name = "Name333" }); BindingSource bindingSource1 = new BindingSource(); bindingSource1.DataSource = trackList; dataGridView1.DataSource = bindingSource1; myBox1.DataBindings.Add("Box1", dataGridView1.DataSource, "TrackID"); myBox1.DataBindings.Add("Box2", dataGridView1.DataSource, "Name"); } private void button1_Click(object sender, EventArgs e) { this.Refresh(); } } public class Track { public int TrackID { get; set; } public string Name { get; set; } }
を、IはDGVにTrackIDは私が変更された値で変更、BOX1の値を変更し、Button1を押し込みます。しかし、Box2の値を変更してbutton1を押すと、DGVのName列は変更されませんでしたが、Box2は元の値に戻りました。
- Box2のみをバインドすると、Box2が機能します。
助けてください。ありがとう。
問題のコードが私の問題を解決しました!ありがとう – Arash