如何将数据列绑定到文本框?

时间:2012-07-09 18:48:13

标签: asp.net data-binding binding

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

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

请帮帮我。并且,请告诉我哪一个更好或者是否有任何其他方法甚至比这两个方法更好。

提前致谢。

3 个答案:

答案 0 :(得分: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]的值,即使用户单击任何单元格也是如此。

我仍然希望有更好的选择,所以我等待对此发表评论。

答案 1 :(得分:1)

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

希望得到这个帮助。

答案 2 :(得分:1)

先生,我做了一个简单的Windows窗体,带有2个单选按钮,2个文本框和datagridview 这是sln文件http://www13.zippyshare.com/v/98590888/file.html这必须帮助你。