1

フォームにデータグリッドビューとテキストボックスがあります。ユーザーがいずれかのラジオボタンをクリックすると、さまざまなテーブルからレコードが読み込まれます。グリッド内のデータは完全に表示されます。これらのテキストボックスに行の値を表示したい。

解決策の 1 つは、データセットからデータをバインドすることです。2 つ目は、行の各セルの値をそれぞれのテキスト ボックスに転送することです。

私を助けてください。また、どちらが優れているか、またはこの 2 つよりも優れた方法があれば教えてください。

前もって感謝します。

4

3 に答える 3

2
Try using a BindingSource

BindingSource bindingSource = new BindingSource();
DataSet dataSet = new DataSet();
DataAdapter da1 = new DataAdapter("Select * from Customers", conn1);
DataAdapter da2 = new DataAdapter("Select * form Orders", conn1);

da1.Fill(dataSet,"Customers");
da2.Fill(dataSet,"Orders");

//Let we set Customers table as bindiningSource datasource
bindingSource.DataSource = dataSet.Tables["Customers"];

private void RadioButtonCustomers_CheckedChanged(object sender, EventArgs e)
{
     if(radioButtonCustomers.Checked==true)
        bindingSource.DataSource =dataSet.Tables["Customers"];
}
private void RadioButtonOrders_CheckedChanged(object sender, EventArgs e)
{
     if(radioButtonOrders.Checked==true)
        bindingSource.DataSource = dataSet.Tables["Orders"];
}
//First param of Binding is to which prop of TextBox to bind the value
//Second param is the data source
//Third param is the data member or the column name of the table as datasource, so
//we have to get that table from casting the bindingSource datasource prop and casting it
//to DataTable obj and after that to take the ColumnName prop of the desired column

textBox1.DataBindings.Add(new Binding("Text",bindingSource,((DataTable)bindingSource.DataSource).Columns[0].ColumnName));
textBox2.DataBindings.Add(new Binding("Text",bindingSource,((DataTable)bindingSource.DataSource).Columns[1].ColumnName));
etc...

bindingSource の datasource prop を変更しても、テキストボックスは 1 列目と 2 列目の rowvalue にバインドされたままになります

この助けを願っています。

于 2012-07-10T15:42:02.943 に答える
1

サー、2 つのラジオ ボタン、2 つのテキスト ボックス、および datagridview を備えた単純な WindowsForm を作成しました

于 2012-07-12T13:39:13.320 に答える
1

同じフォームのテキストボックスに値を表示するために多くのオプションを試しましたが、datagridview が 2 つの異なるテーブルのレコードを表示できるため、うまくいきませんでした。

代わりに、datagridview の次のイベントを使用しました。

    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        this.dataGridView1.SelectionChanged += new System.EventHandler(this.DisplayValueinTextBox);
    }

DisplayValueinTextBox では、各テーブルに表示される列の数に基づいて次のコードを記述しました。

   private void DisplayValueinTextBox(object sender, EventArgs e)
    {
        try
        {
            textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
            textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();

            if (tblGrid == "Employee") //name of table which has more columns in grid
            {
                textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
                textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
                textBox6.Text = dataGridView1.SelectedCells[5].Value.ToString();
            }
            this.dataGridView1.SelectionChanged -= new System.EventHandler(this.DisplayValueinTextBox); //removed it as I was getting error.
        }
        catch (Exception exdisp)
        {
            MessageBox.Show(exdisp.Message);
        }
    }

また、dataGridView の SelectionMode プロパティを FullRowSelect に変更しました。これにより、ユーザーが任意のセルをクリックしても、textbox1 に SelectedCells[0] の値が表示されます。

もっと良い選択肢があることを願っていますので、これについてのコメントをお待ちしています。

于 2012-07-10T12:13:09.640 に答える