Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView TextBox column : Bound data source table column contains numbers, want to display it as text from a "lookup" table.
 

DataGridView TextBox column : Bound data source table column contains numbers, want to display it as text from a "lookup" table.

Pardon me if this has been asked before; I'm quite new to ADO.NET terminology so I'm not really sure what keywords to search for to find an answer to this.

I've got a strongly typed dataset with two tables named 'Books' and 'Authors'.

The Books table has two columns:
Title (string)
AuthorID (int)

The Authors table has two columns
ID (int)
Name (string)

A relation has been setup between the two tables:
Authors table / ID column is the Parent table / Key column.
Books table / AuthorID column is the Child table / Foreign Key column.


The application has a form with a DataGridView control. The gridview is supposed to display all the data from the Books table BUT instead of displaying the numbers in the AuthorID column, I want the gridview to display the Name s from the Authors table that corresponds to the Books table's AuthorID s. I've got no problem getting this to work if I change the AuthorID column type to ComboBox and setup the proper bindings in the gridview's column editor but I can't figure out how to do this sort of binding for a textbox column.

(I've got the same problem with Textboxes vs. ComboBoxes/ListBoxes placed directly on a form).


Any help would be much appreciated. VB or C# codesnippets are ok.

/BR
O
OakOMalm  Saturday, April 25, 2009 12:58 PM

Try this type...

Create a variable connected with textbox then attend for sql query.

SqlCommand command = new SqlCommand("select * from icup where ID=@id", cn); command.Parameters.Add("@id", SqlDbType.Int); command.Parameters["@id"].Value = textBox1.Text;


Please mark the replies as answers if they help and unmark if they don't.
  • Unproposed As Answer byOakOMalm Sunday, April 26, 2009 1:32 PM
  • Proposed As Answer byNitin Chaudhary Sunday, April 26, 2009 4:13 AM
  •  
Nitin Chaudhary  Sunday, April 26, 2009 4:12 AM
Continue to use DataGridViewComboBoxColumn. Setup the proper bindings as you said you did. Change the column's property DisplayStyle to Nothing. The column will now look like a regular text field. This will make the column look like a text field, but if your column is not ReadOnly, when the user clicks the field it will still show the dropdown. This makes sense though, because the nature of a lookup field means you can't enter arbitrary text into it.
paul hoenecke  Tuesday, August 04, 2009 2:40 PM

You can use google to search for other answers

Custom Search

More Threads

• using SUM in DataTable.Select()
• When to dispose TableAdapter?
• Adding a record (where tableadapter is created in designer)
• Multiple Lines for DataGridView Header Text
• DataGrid Rows and Columns
• GetChanges?
• Datagridview in MDI
• Arraylist is populated using a custom class. Listbox Datasource set to the ArrayList. Error: Object must implement IConvertible
• Reading individual DataTable fields
• Undo/Redo - Windows Forms, Data Binding and Business Objects