Master-detail view using textBoxes and ADO
The problem I'm fighting with is binding data from MySql to the textBoxes in C#. Using dataGridView it looks like that:
DataAdapter = new SqlDataAdapter(commandString , connection);
DataAdapter.Fill(dataSet1,"show");
DataAdapterDetail.Fill(dataSet1, "detail");
DataRelation Relation = new DataRelation("FK_Zadanie_Pracownik",
dataSet1.Tables["show"].Columns["ID"],
dataSet1.Tables["detail"].Columns["ID"]);
dataSet1.Relations.Add(Relation);
bindingSource1.DataSource = dataSet1;
bindingSource1.DataMember = "show";
bindingSource2.DataSource = bindingSource1;
bindingSource2.DataMember = "FK_Zadanie_Pracownik";
dataGridView1.DataSource = bindingSource1;
And my question is how to do the same but using combobox with the first bindingSource, and the textbox with the second.
E开发者_Go百科DIT:
I was trying to do something like that, using DataRelations:
DataRelation ZameldowaniaMieszkancy = new DataRelation("ZamMiesz", dsZameldowania.Tables["Zameldowania"].Columns["id_mieszkanca"], dsZameldowania.Tables["Mieszkancy"].Columns["id"], false);
bsOsoba = new BindingSource();
bsOsoba.DataSource = dsZameldowania;
bsOsoba.DataMember = "Zameldowania";
design.comboBoxFiltrNazwisko.DataSource = bsOsoba;
design.comboBoxFiltrNazwisko.ValueMember = "ZamMiesz.nazwiskoImie";
design.comboBoxFiltrNazwisko.DisplayMember = "ZamMiesz.nazwiskoImie";
But it's showing me only the first result from the DB (only one row-to-row relation) and not all of them.
You can simply bind your dataset with ComboBox like this
mycombo.DataSource = dataSet1.Tables["show"];
After that you have to set DisplayMember and ValueMember of the ComboBox
mycombo.DisplayMember = "ColumnNameToDisplay";
mycombo.ValueMember = "ColumnNameForID";
For TextBox value you have to define specified column and row of the datatable Like this:
TextBox1.Text = dataSet1.Tables["show"].Rows[RowIndex]["Column Name"].ToString();
If dropdown and textbox is in Grid than you have to perform the same thing in RowDataBoundEvent of Grid.
maybe you want to take a look at this little tutorial over here
精彩评论