1

我在表单上有 datagridview 和文本框。当用户单击任何一个单选按钮时,我会从不同的表中加载记录。网格中的数据显示完美。我想在这些文本框中显示行的值。

一种解决方案可以是:从数据集中绑定数据。第二个可以是:将行的每个单元格的值传输到相应的文本框。

请帮我。并且,请告诉我哪个更好,或者有没有比这两个更好的方法。

提前致谢。

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 的数据源属性,文本框仍将绑定到第一列和第二列的行值

希望这有帮助。

于 2012-07-10T15:42:02.943 回答
1

我尝试了许多选项来在同一表单的文本框中显示值,但它不起作用,因为 datagridview 可以显示两个不同表的记录。

相反,我使用了 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 回答
1

先生,我用 2 个单选按钮、2 个文本框和 datagridview 制作了一个简单的 WindowsForm,这里是 sln 文件http://www13.zippyshare.com/v/98590888/file.html,这必须对您有所帮助。

于 2012-07-12T13:39:13.320 回答