I have two datatables: Transactions and Accounts.
Transaction has a foreign key IDAccount from Accounts dataTable.
I assign datasource property of Datasource property of Datagridview to Transactions dataTable.
I want to Accounts.description insead of IDAccount in Datagridview.
What must I do?
Make the column in your DataGridView that's bound to the IDAccount field a DataGridViewComboBoxColumn and set its properties as follows:
DataSource: your Accounts DataTable
DisplayMember: Description
ValueMember: IDAccount
The DataGridViewComboBoxColumn's ValueMember and DisplayMember properties allows you to store and manipulate your IDAccount value in your DGV (and data source) while displaying to the user the Description value. The MSDN article I linked explains it well.
Related
I have used two entity classes for binding values into DataGridView. One is Estimates and Companies.
Estimates has columns such as "Id, Estimate Number, Estimate Amount, CompanyId".
Companies has columns such as "Id, Company Name, Address"
I have created two BindingSource such as EstimateBindingSource and CompanyBindingSource.
CompanyBindingSource has DataSource as EstimateBindingSource and DataMember as Estimates
EstimateBindingSource has DataSource as Estimates entity Class and no DataMember defined.
I have bound the EstimateBindingSource into the DataGridView using grid DataSource.
Here, I need to show Estimate number, Estimate Amount and Company Name in DataGridView.. I have't able to achieve this.
Note: I do not do any code behind logic to do this.. Need to achieve this only using design.
Options to show Second Level Properties in DataGridView
To show a sub property of your navigation property you can use either of these options:
Use a DataGridViewComboBox column and bind it to CompanyId and set it's DataSource to list of companies, and DisplayMember property to Name property of company and ValueMember to Id property of company.
Override ToString() method of Company class and return Name of company. Then show Company navigation property in grid.
Create a CompanyName property for your Estimate which returns its Company.Name value and show CompanyName in grid.
Using CellFormatting event of DataGridView and set e.Value to desired value (company name) you want to display in cell.
Shape your Estimates list using a Linq query or use a ViewModel and pass the result to data grid view.
Create a TypeDescriptor for your Estimate type to resolve second level properties.
.
To show a property of company instead of company id, you can use a DataGridViewComboBoxColumn.
Using ComboBox Column
Since you requested for a mechanism which uses designer without writing code I describe this option more. Here is settings you should perform:
EstimatesBindingSource should bind to a list of Estimates
The DataGridView should bind to EstimatesBindingSource
CompanyBindingSource is only used as data source of the combo box column and should be filled using a list of Companies
To show CompanyName in Estimates list, it's enough to use a DataGridViewComboBoxColumn and set it's DataSource to list of companies and set the DisplayMember to CompanyName and it's value member to Id. And bind it to CompanyId field of Estimate.
Also if your requirement is to don't show it as ComboBox, simply set DisplayStyle property of DataGridViewComboBoxColumn to Nothing. It removes dropdown style.
You also may find this post helpful:
Show Properties of a Navigation Property in DataGridView (Second Level Properties)
There DataGridView, the source - the table in the database, implemented in DataGridView column type ComboboxColumn related to another table in the database to display correctly connected data.
Question: when sorting staff to DataGridview means that clicking on the column header, sorting is conducted by ValueMember ComboboxColumn. How to change the sort order that it was made based on DisplayMember?
I have a database table of orders which is displayed in a DataGridView (DataTable filled using a DataAdapter). One of the columns will show the supplier and in the database this is a foreign key to the Supplier table (int ID and varchar Name).
In the DataGridView this obviously only displays as a textbox with the ID. How do I get this as a dropdown list of the Supplier names?
just use the DataGridViewColumn and set the DataSource from the properties of DataGridViewColumn you've created.
In my database I have a table Cars which contains (among other things) an idCategory column which is a foreign key. That foreign key refers idCategory in the Category table which contains the description of the category.
Now in my C# application I got a form that is used to add new cars. There is a combobox to choose the category.
comboboxCat.DataSource = datatable1;
comboboxCat.DisplayMember = "categoryDescription";
comboboxCat.ValueMember = "idCategory";
So I set the DataSource of the ComboBox to the DataTable that contains the data of Category table, and I set the id as the valueMember and the Description as DisplayMember.
I also have a DataSet containing the table Cars.
What I want to do is to automatically update the Car data in the DataSet when the value of the Combobox is changed. I don't know how to bind it. In a datagridview it's easy because I just have to set the DataPropertyName of the DataGridViewComboBoxColumn. But there's not such properties in a normal ComboBox. Thanks in advance
I belive you have to bind the IdCategory field from the Cars DataTable to the SelectedValue of the ComboBox.
Finally found what I was looking for : http://blogs.msdn.com/b/bethmassi/archive/2007/04/25/tips-on-related-data-binding-and-comboboxes.aspx
My answer is to use DataBindings to make the link
I am working on a project for my college where I need to bind data from database into the combobox. I need to store the roll no / enrollment no in the "value" field of combobox and name of the student in the "text" property of the combobox. How can I do that?>???
Please reply ASAP....
You will need to set the DataSource of the combobox to your datasource. Then the ValueMember for the Roll No and the DisplayMember for the name of the student.
e.g
cboStudents.DataSource = dataSet1.Tables["Students"];
cboStudents.ValueMember = "RollNumber";
cboStudents.DisplayMember = "StudentName";
The two complex-bound controls you've most likely encountered are the ComboBox and the Listbox. To complex-bind one of these controls, you need to set the DataSource (where values originate), the DisplayMember (the name of the column of data that supplies the visible list items), and the ValueMember (the name of the column of data that supplies the possible control values).
combobox.DataSource = dataTable
combobox.ValueMember = "id"
combobox.DisplayMember = "name"