hadox

Hi!

Database structure:

book table: PublisherID

publisher table: PublisherID, publisherName

Variables:

BindingSource bookBindingSource

BindingSource publisherBindingSource

DataSet customTableDataSet

 

In my program is a BindingNavigator customTableBindingNavigator: to navigate trough a custom dataset (joins 2 different tables, the book table and another one) Then I have some textbox fields (a PublisherID textbook and some others) bound to customTableBindingSource and a combobox.

What I'm trying to do is to fill the combobox with ALL values of publisherName column, but with the right publisherName selected.

Filling the box is no problem, but i'm having a problem selecting the right one.

I hope it's a bit clear.

Thanks,

hadox

(Moderator: Thread moved to this forum for better responses)


Re: Windows Forms Data Controls and Databinding Combobox: displaying different data from a column from 2 linked tables

Ken Tucker

Try something like this



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ComboBindingSource
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet ds;
BindingSource bsPublishers;
BindingSource bsTitles;
private void Form1_Load(object sender, EventArgs e)
{
String strConn = "Server = .\\sqlexpress;Database = pubs;Integrated Security = SSPI;";
SqlDataAdapter daPubs;
SqlDataAdapter daTitles;
SqlConnection conn;
conn = new SqlConnection(strConn);
daPubs = new SqlDataAdapter("Select * From Publishers", conn);
daTitles = new SqlDataAdapter("Select * From Titles", conn);
ds = new DataSet();
daPubs.Fill(ds, "Publishers");
daTitles.Fill(ds, "Titles");
bsPublishers = new BindingSource(ds, "Publishers");
ds.Relations.Add(new DataRelation("TitlePub", ds.Tables["Publishers"].Columns["pub_id"],ds.Tables["Titles"].Columns["pub_id"]));
bsTitles = new BindingSource();
bsTitles.DataSource = bsPublishers;
bsTitles.DataMember = "TitlePub";
cboPublisher.DataSource = bsPublishers;
cboPublisher.DisplayMember = "pub_name";
cboTitles.DataSource = bsTitles;
cboTitles.DisplayMember = "Title";
}
}
}






Re: Windows Forms Data Controls and Databinding Combobox: displaying different data from a column from 2 linked tables

Zhi-Xin Ye - MSFT

Hi, hadox

You want to selected the combobox according to what I'm afraid you don't describe clear.

Regards.

Ye






Re: Windows Forms Data Controls and Databinding Combobox: displaying different data from a column from 2 linked tables

hadox

i was mixing some things up when setting the right bindingsources

got it all right now

 

thanks Ken Tucker for the example