Question Not sure why this isnt working

radgator

New member
Joined
Apr 27, 2016
Messages
3
Programming Experience
5-10
foreach (DataRow dataRow in dt.Rows)
{
if (dataRow["Column1"].ToString().Contains(["ExampleString"])
{

listBox1.Items.Add(dataRow["Column1"] + " " + dataRow["Column2"] + " " + dataRow["Column3"] + " " + dataRow["Column4"]);

}
}


//the above code works perfectly and returns only the rows containing the "ExampleString"


// if i try to use a SelectedText value from a combobox as a string to replace the "ExampleString", (as shown below),the listbox populates with ALL of the rows, not just the ones with the "ExampleString".




string strFromCombobox = comboBox1.SelectedText;

foreach (DataRow dataRow in dt.Rows)
{
if (dataRow["Column1"].ToString().Contains(strFromCombobox)
{

listBox1.Items.Add(dataRow["Column1"] + " " + dataRow["Column2"] + " " + dataRow["Column3"] + " " + dataRow["Column4"]);

}
}
 
I very much doubt that SelectedText is the property you want. You should read about the SelectedText, Text, SelectedItem and SelectedValue properties of the ComboBox class and get a proper understanding of what they do. The fact that the SelectedText property of a ComboBox works in exactly the same way as the SelectedText property of a TextBox should tell you something.
 
Thank you for responding

I very much doubt that SelectedText is the property you want. You should read about the SelectedText, Text, SelectedItem and SelectedValue properties of the ComboBox class and get a proper understanding of what they do. The fact that the SelectedText property of a ComboBox works in exactly the same way as the SelectedText property of a TextBox should tell you something.

Please correct me if I am wrong but i am assuming that the datarow object is looking for a text value since it works when i put a static text value in it. I have tried selecteditem.tostring, selectedvalue.tostring, and even tried referencing the index to the text.
 
How EXACTLY did you populate the ComboBox? In this scenario, you should really only be using the Text property if you want the text displayed in the control or the SelectedValue property if you bound the control and set the ValueMember and you want the value from that member.
 
How EXACTLY did you populate the ComboBox? In this scenario, you should really only be using the Text property if you want the text displayed in the control or the SelectedValue property if you bound the control and set the ValueMember and you want the value from that member.

I did not bind anything. I merely added the text items to the combobox so I could just pull them from there.
 
Back
Top Bottom