2

ここに私がフォローしてきたことの背景があります。

http://www.homeandlearn.co.uk/csharp/csharp_s12p9.html

これは、データベースの最後または最初のレコードに移動します。ID番号をテキストボックスに入力して、ユーザーが必要とするアクセスデータベースのレコードにスキップしたいのですが、正しい行がテキストボックスに入れられます。

上記のウェブサイトからこのコードを使用できると思います。上記のWebサイトから他のすべてを実装しました。

グローバル変数

int inc = 0;

後で [スキップ] ボタンで呼び出すナビゲート レコード

private void NavigateRecords()
{
      DataRow dRow = ds1.Tables["Laptops"].Rows[inc];

      txtMaker.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(1).ToString();
      txtModel.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(2).ToString();
      txtPrice.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(3).ToString();
      txtBids.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(4).ToString();
      txtScreen.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(5).ToString();
      txtCPU.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(6).ToString();
      txtMemory.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(7).ToString();
      txtHD.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(8).ToString();
      picLaptops.Image = Image.FromFile(ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(9).ToString());
}

これまでのスキップボタン...

private void btnSkip_Click(object sender, EventArgs e)
{
    NavigateRecords();           
}

これを行うのは私には難しいです。やりたいことはわかっているが、それを行うための技術的スキルが不足している。とてもイライラします。どうすればいいのかわかりません。

誰かがそれを解決してコードを見せてくれれば、それを理解して他の場所で使用できます。

これが役立つ場合に次のレコードに移動するための次のボタンの例を次に示します。

private void btnNext_Click(object sender, EventArgs e)
{
      if (inc != MaxRows - 1)
      {
            inc++;
            NavigateRecords();
      }
      else
      {
          MessageBox.Show("You have reached the end of available items", "End of Available Items", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }
}
4

2 に答える 2

0

あなたの説明に基づいて、これはあなたが望むものだと思います

void btnNext_Click(object sender, EventArgs e) {
    int id = Int32.Parse(yourIdTextBox.Text);

    DataRow row = ds1.Tables["Laptops"].Rows.OfType<DataRow>()
                     .SingleOrDefault(r => (int)r.ItemArray[your id index] == id);

    if (row == null)
    {
         //user typed in invalid row id
    }  else
          processRow(row);
}

void processRow(DataRow row){
    txtMaker.Text = row.ItemArray.GetValue(1).ToString();
    txtModel.Text = row.ItemArray.GetValue(2).ToString();
    txtPrice.Text = row.ItemArray.GetValue(3).ToString();
    txtBids.Text = row.ItemArray.GetValue(4).ToString();
    txtScreen.Text = row.ItemArray.GetValue(5).ToString();
    txtCPU.Text = row.ItemArray.GetValue(6).ToString();
    txtMemory.Text = row.ItemArray.GetValue(7).ToString();
    txtHD.Text = row.ItemArray.GetValue(8).ToString();
    picLaptops.Image = Image.FromFile(row.ItemArray.GetValue(9).ToString());
}

次に、 NavigateRecors() メソッドを単純化します

private void NavigateRecords()
{
    DataRow dRow = ds1.Tables["Laptops"].Rows[inc];

    processRow(dRow);
}
于 2011-12-03T23:59:22.873 に答える
0

コントロールに手動で値を割り当てる代わりに、データ バインディングを使用します。

  1. モデルクラスを作成する
public class MyClass  
{  
    public string Maker { get; set; }  
    public double price { get; set; }  
    // and so on, for all your fields  
}
  1. Visual Studio の [データ ソース] エクスプローラーで MyClass のオブジェクト データ ソースを追加します。

  2. フィールドをデータ ソースからフォームにドラッグします。Visual Studio は、BindingSource と BindingNavigator をフォームに自動的に追加します。

  3. 次の形式でデータを BindingSource に割り当てます。

this.bindingSource1.DataSource = myData;

myData は MyClass オブジェクトの列挙です。

これは、データベース ソースに対しても実行できます。しかし、個人的には、データをクラスに配置することを好みます。DataSet やフォーム フィールドを直接操作するよりも、扱いが簡単です。

于 2011-12-04T00:07:46.567 に答える