Help on simple databinding ???

G

Guest

Dear all,

I have 2 tables from which I create a simple data relation:
Table1 has a field name ID which is retrive from a database
Table2 has fields ID and Name which is retrived from database

I have build a datarelation from Table 1 to Table 2 on field ID, so far so
good
Base on that my dataset contains following structure :

TABLE1: ID
TABLE2: ID, NAME

From that I have 2 list box name lstTable1 and lstTable 2
What I need to do is :
- bind the lstTable1 listbox on ID field of table 1 from dataset
- bind the lstTable2 listbox on field NAME and gets childs content field
issue from content of lstBox1

I am able to bind list box but the content of lstBox' contains all records
and NOT only records based on parent of lstBox1

How can I do that ?

thnaks for your help
regards
Serge
 
C

carl

Serge,

One approach is to use a row filter on the second list. Here's some
sample code:

/* Filter sample */

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
bindData("1");
}

protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
bindData(DropDownList1.SelectedValue);
}

private void bindData(string selectedId)
{
DataSet myds = GetDataSet();

// Initialize Drop Down List 1
DropDownList1.DataSource = myds.Tables["TableA"];
DropDownList1.DataTextField = "ID";
DropDownList1.DataValueField = "ID";

// Bind Drop Down List 1
DropDownList1.DataBind();
DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue(selectedId).Selected = true;

// Initialize Drop Down List 2
DropDownList2.DataSource = myds.Tables["TableB"];
DropDownList2.DataTextField = "NAME";
DropDownList2.DataValueField = "ID";

// Apply Filter to Drop Down List 2
myds.Tables[1].DefaultView.RowFilter =
string.Format("ID = {0}", selectedId);

// Bind Drop Down List 2
DropDownList2.DataBind();
}

/* End sample */

I simplified it a bit, but you can see that when the page loads or the
first DropDownList's index changes the second list is filtered.

-Carl
 
G

Guest

hi carl, thanks

the idea was to use the dtarelation object.
I manage to get it working

serge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top