【问题标题】:How to bind data column to textbox?如何将数据列绑定到文本框?
【发布时间】:2012-07-09 05:07:06
【问题描述】:

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

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

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

提前致谢。

【问题讨论】:

    标签: asp.net data-binding binding


    【解决方案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 的 datasource 属性,文本框仍将绑定到第一列和第二列的行值

    希望对您有所帮助。

    【讨论】:

    • 我试过这段代码,我的观察是: 1. 当我点击另一行时,我得到文本框中第一行的值。在此之后,即使选择了另一行,文本框中的值也不会改变。 2.当我点击radiobutton2并尝试设置绑定源的数据源属性时抛出错误。
    • 使用 DataGridView1_SelectionChanged 事件并在内部使用 bindingSource Position 属性获取当前行。之后将数据行单元格值分配给每个文本框。数据行博士 = ((DataTable)b​​indingSource.DataSource).Rows[bindingSource.Position]; textbox1.Text = dr[0].ToString(); textBox2.Text = dr[1].ToString();
    • 先生,将单元格值分配给文本框是我在不使用绑定源的情况下完成的。请看下面我的回答。我想知道为什么在我按照您的指导使用 bindingsource 时第一次单击 datagridview 后不显示值。
    【解决方案2】:

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

    【讨论】:

    • 我得到了真正的帮助。谢谢先生。
    【解决方案3】:

    我尝试了许多选项来在同一个表单的文本框中显示值,但它不起作用,因为 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] 的值,即使用户单击任何单元格。

    我还是希望有更好的选择,所以我在等待cmets。

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 2022-08-02
      相关资源
      最近更新 更多