【发布时间】:2012-07-09 05:07:06
【问题描述】:
我在表单上有 datagridview 和文本框。当用户单击任何一个单选按钮时,我会从不同的表中加载记录。网格中的数据显示完美。我想在这些文本框中显示行的值。
一种解决方案可以是:从数据集中绑定数据。 第二个可以是:将行的每个单元格的值传输到相应的文本框。
请帮助我。还有,请告诉我哪一个更好,或者有没有比这两个更好的方法。
提前致谢。
【问题讨论】:
标签: asp.net data-binding binding
我在表单上有 datagridview 和文本框。当用户单击任何一个单选按钮时,我会从不同的表中加载记录。网格中的数据显示完美。我想在这些文本框中显示行的值。
一种解决方案可以是:从数据集中绑定数据。 第二个可以是:将行的每个单元格的值传输到相应的文本框。
请帮助我。还有,请告诉我哪一个更好,或者有没有比这两个更好的方法。
提前致谢。
【问题讨论】:
标签: asp.net data-binding binding
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 属性,文本框仍将绑定到第一列和第二列的行值
希望对您有所帮助。
【讨论】:
先生,我用 2 个单选按钮、2 个文本框和 datagridview 制作了一个简单的 WindowsForm 这是 sln 文件 http://www13.zippyshare.com/v/98590888/file.html 这一定对你有帮助。
【讨论】:
我尝试了许多选项来在同一个表单的文本框中显示值,但它不起作用,因为 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。
【讨论】: