Connecting Tech Pros Worldwide Help | Site Map

Trying to figure out how to link two controls

Henry
Guest
 
Posts: n/a
#1: Nov 17 '05
I hope someone can help me on this. I have two controls on a form.
the first is a comboBox the second is a datagrid. Both controls are bound
to tables in
a common dataset. What I am trying to get to happen is to have the value
from the combobox control what populates the datagrid.

I have two data adapters, the second of which uses a parameterized query to
populate its referenced table in the dataset. I have two issues. first how
to fill the second control when form is first shown and second how to
refresh the second control.



this.cmbPeriod.DataSource = this.dtPeriod;// this is the table object

this.cmbPeriod.DisplayMember = "period_name"; // this is the column from the
period table that displays

this.cmbPeriod.Name = "cmbPeriod";

// this is the column from the period table that I want to be returned as
the selectedIndex

// the period_id is what I want to pass as the parameter value to fill the
other control

this.cmbPeriod.ValueMember = "period_id";

this.cmbPeriod.SelectedValueChanged += new
System.EventHandler(this.cmbPeriod_SelectedValueCh anged);

//
// daOrg This is the second data adapter association with table
named organization
//
this.daOrg.SelectCommand = this.cmdSelectOrg;
//
// cmdSelectOrg This is the select command associated with data
adapter daOrg
//
this.cmdSelectOrg.CommandText = "SELECT id AS org_id, hier_path AS
org_hier_path, name AS org_name, order_index AS" +
" org_index, period_id, original_id AS org_original_id FROM
dbo.organization WHER" +
"E (period_id = @period_id) ORDER BY hier_path";
this.cmdSelectOrg.Connection = this.cnSoComply;
this.cmdSelectOrg.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@period_id", System.Data.SqlDbType.Int,
4, "period_id"));
//
// dgOrg this is the data grid I am trying to fill
//
this.dgOrg.DataMember = "";
this.dgOrg.DataSource = this.dtOrganization;


// This is the Selected Value changed event handler for the
comboBox cmbPeriod

private void cmbPeriod_SelectedValueChanged(object sender,
System.EventArgs e)
{
//I need to pass the value of the the select period row period_id field
here
// I thought by assigned the ValueMember to period_id that selectedIndex
would return
// that number but that is not the case.
daOrg.SelectCommand.Parameters["@period_id"].Value =
cmbPeriod.SelectedIndex;
daOrg.Fill(dsSoComply,"Organization");

}

I am still confused as to how to populate the grid the first time it is
shown because I don't think the SelectedValueChanged event fires at that
point.


John Richardson
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Trying to figure out how to link two controls


if the datagrid doesn't populate off the form loading, then can't you run it
manually in your form constructor?
After you set your combo box datasource, you should check if their are rows,
set it to a default position (ie: 0), and call your event handler manually
if it isn't firing. Your hanlder won't fire since you haven't defined your
event handler before you assigned your datasource... and imo this is good,
because it's easier to control what happens by calling config stuff manually
at the time you set your datasource.

As for getting your value from the combo box, SelectedIndex is the index of
the row in it's collection (the combolist). You are looking for
SelectedValue. You will have to cast the return to the correct type. As an
alternative you can also get it by retrieving the entire data row (as a data
row view object).

DataRowView r = cmbPeriod.Items[cmbPeriod.SelectedIndex] as DataRowView;



"Henry" <hstock@onproject.com> wrote in message
news:O$o4tqB6FHA.884@TK2MSFTNGP14.phx.gbl...[color=blue]
>I hope someone can help me on this. I have two controls on a form.
> the first is a comboBox the second is a datagrid. Both controls are bound
> to tables in
> a common dataset. What I am trying to get to happen is to have the value
> from the combobox control what populates the datagrid.
>
> I have two data adapters, the second of which uses a parameterized query
> to populate its referenced table in the dataset. I have two issues. first
> how to fill the second control when form is first shown and second how to
> refresh the second control.
>
>
>
> this.cmbPeriod.DataSource = this.dtPeriod;// this is the table object
>
> this.cmbPeriod.DisplayMember = "period_name"; // this is the column from
> the period table that displays
>
> this.cmbPeriod.Name = "cmbPeriod";
>
> // this is the column from the period table that I want to be returned as
> the selectedIndex
>
> // the period_id is what I want to pass as the parameter value to fill the
> other control
>
> this.cmbPeriod.ValueMember = "period_id";
>
> this.cmbPeriod.SelectedValueChanged += new
> System.EventHandler(this.cmbPeriod_SelectedValueCh anged);
>
> //
> // daOrg This is the second data adapter association with table
> named organization
> //
> this.daOrg.SelectCommand = this.cmdSelectOrg;
> //
> // cmdSelectOrg This is the select command associated with data
> adapter daOrg
> //
> this.cmdSelectOrg.CommandText = "SELECT id AS org_id, hier_path AS
> org_hier_path, name AS org_name, order_index AS" +
> " org_index, period_id, original_id AS org_original_id FROM
> dbo.organization WHER" +
> "E (period_id = @period_id) ORDER BY hier_path";
> this.cmdSelectOrg.Connection = this.cnSoComply;
> this.cmdSelectOrg.Parameters.Add(new
> System.Data.SqlClient.SqlParameter("@period_id",
> System.Data.SqlDbType.Int, 4, "period_id"));
> //
> // dgOrg this is the data grid I am trying to fill
> //
> this.dgOrg.DataMember = "";
> this.dgOrg.DataSource = this.dtOrganization;
>
>
> // This is the Selected Value changed event handler for the
> comboBox cmbPeriod
>
> private void cmbPeriod_SelectedValueChanged(object sender,
> System.EventArgs e)
> {
> //I need to pass the value of the the select period row period_id field
> here
> // I thought by assigned the ValueMember to period_id that selectedIndex
> would return
> // that number but that is not the case.
> daOrg.SelectCommand.Parameters["@period_id"].Value =
> cmbPeriod.SelectedIndex;
> daOrg.Fill(dsSoComply,"Organization");
>
> }
>
> I am still confused as to how to populate the grid the first time it is
> shown because I don't think the SelectedValueChanged event fires at that
> point.
>
>[/color]


Closed Thread


Similar C# / C Sharp bytes