Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databases, DataGridView and Data bound items 1

Status
Not open for further replies.

abcfantasy

Programmer
Jun 3, 2006
110
DK
I guess this is a simple question.

I had used databound items in comboboxes to display a value member while storing another value (example displaying names and storing their IDs).

Is it possible to do this in one of the columns of a DataGridView?

Thanks in advance
Andrew

ABC -
 
Yes, just add a Template Column with a DropDownList in it.
 
I searched on how to create template columns, and all i found is how to create them in the Web Forms Designer and not in a Windows Application.

ABC -
 
Well you didn't specify that this was a desktop app, though I should have assumed it since it's in the regular C# forum.

Add a dataGridView to the for, a Binding Source.

And in the Properties Explorer you click on EditColumns.. then you can add columns from you BindingSource or make a new Column, where you select the type of item to put in that column.

HTH
 
I think I have not made myself clear enough.

I've got DataGridView1 whose DataSource is set to BindingSource1. Now I've got a column 'City', which stores integers. I need to use that value to find the CityName from another datasource (or BindingSource2), then display the CityName instead of the integer. 'City' is used as a foreign key to search the CityName.

This is what I want to do.
Hope I made it clearer
And thanks for your help.

ABC -
 
Thanks so much, finally got it to work.
I appreciate your help.

One last thing, I think there are different ways to get the CityName from the second source. Just wanted to ask if what I did is good practice:

Code:
private void donorsDataGridView_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e )
      {
         if ( e.ColumnIndex == cityCol )
         {
            e.FormattingApplied = true;
            e.Value = citiesTableAdapter.GetDataByCityID( (int)e.Value )[0].CityName;         
         }
      }


ABC -
 
didnt you mention you had a 2nd datasource with the city names in it already?

Assuming it's a dataset or a datatable and you used the dataadapter to fill, you can just select from a datatable.

DataRow[] dr = myDataSet.Tables["TableName"].Select("CityId='" + e.Value + "'");

if you already have the datasource populated this will save you extra trips to the database.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top