Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
305
xamCombo Editor - going crazy? - value empty and selectedItem System.Data.DataRowView
posted

I have a number of xamCombos in use. When I bind my data from DB and for a particular field there is a selectedItem in the combo matching all is fine, it gets selected - when I go to save my data back to DB all in fine if I use the cboSomething.Value property except for...(see problem)

When binding data from DB - I do the following:
this.cboConsultant_e.SelectedItem = (int)rdr["Consultant"];
this.cboConsultant_e.Value = (int)rdr["Consultant"];

Problem: If a DB field has a null value for "Consultant" there is no selectedItem initially when loading data, the combo has all its items but nothing selected, desirable behavior. Its when I then select an item from the combo and try to save. The Value is non existant when I try to access it? And im having trouble with selectedItem when trying to access it instead. The error I get is "No Mapping exists from object type System.Data.DataRowView to a know managed provider native type" I understand how the item can be treated as a datarow, but cant figure out best way to access the selectedItem? Doesnt happen if initially on the first load I have a selectedItem for the combo. Please help! Ive stuffed round for a while on this...

if (this.cboConsultant_e.Text != null &&
this.cboConsultant_e.Text.ToString().Length > 0
)
{
command.Parameters.Add(
new SqlParameter("@Consultant", this
.cboConsultant_e.Value));
}
else
{
command.Parameters.Add(
new SqlParameter("@Consultant", DBNull
.Value));
}

This is the binding to the control in my code behind.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

private

 

 

void BindConsultant()
{

try

{

 

 

 

//Ward - using spGetWardLookUp

 

 

 

SqlConnection connection;

 

 

 

SqlCommand command;

 

 

DataSet dtSet = new DataSet();

 

 

 

using (connection = new SqlConnection(connectionString))

{

 

 

 

string sql = "spGetConsultantLookUp";

command =

 

new SqlCommand(sql, connection);

 

 

 

SqlDataAdapter adapter = new SqlDataAdapter();

connection.Open();

adapter.SelectCommand = command;

adapter.Fill(dtSet,

 

"Cons");

 

 

 

 

 

 

 

 

 

this.cboConsultant.ItemsProvider.ItemsSource = dtSet.Tables["Cons"].DefaultView;;

this.itp_Consultant.DisplayMemberPath = "DESC";

 

 

 

this.itp_Consultant.ValuePath = "ID";

connection.Close();

}

}

 

 

 

catch (Exception

ex)

{

 

 

 

MessageBox

.Show(ex.Message);


}

}

XAML:

 

 

 

 

 

 

 

 

 

<igEditors:XamComboEditor Grid.Row="1" Grid.Column="1" Name="cboConsultant" Margin="0,2.5,4,2.5" Height="20" d:LayoutOverrides="GridBox"

>

 

 

 

 

<igEditors:XamComboEditor.ItemsProvider

>

 

 

 

 

<igEditors:ComboBoxItemsProvider x:Name

="itp_Consultant" />

 

 

 

 

</igEditors:XamComboEditor.ItemsProvider

>

 

 

 

 

</igEditors:XamComboEditor

>

 

 

 

 

Parents
No Data
Reply
  • 305
    Verified Answer
    posted

     

     

     

     

    string value = ((System.Data.DataRowView)mycombo.SelectedItem).Row.ItemArray[0

    ].ToString();

     

    This will work

Children
No Data