0

I have two ListBoxes and one ComboBox in my forms application. I want to add the current selected item from all 3 of them (two ListBoxes and one ComboBox)to a new TextBox. However, I'm getting the column name as:

SYSTEM.DATA.DATAROWVIEW

Here's the code I'm using:

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    string cmdstr = @"select * from information_schema.columns where table_name = '" + comboBox1.SelectedItem + "'";
    string conStr = @"Data Source=INPDDBA027\NGEP;Initial Catalog=Dev_Server;Integrated Security=True";
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(cmdstr, conStr);
    sda.Fill(dt);
    //listBox2.DataSource = dt;
    //listBox2.DisplayMember = "Column_Name";
    textBox2.CharacterCasing = CharacterCasing.Upper;
    textBox2.Text = (listBox1.SelectedItem.ToString() + " " + listBox2.SelectedItem.ToString() + " FROM " + comboBox1.SelectedItem.ToString());
}

Please help.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
ankastic
  • 171
  • 2
  • 14
  • 1
    Have a look at this question: http://stackoverflow.com/questions/34588605/copy-items-from-listbox-to-checkedlistbox – Salah Akbari Jul 15 '16 at 05:42
  • 1
    Or this: http://stackoverflow.com/questions/34422684/how-can-i-get-the-string-value-of-a-checked-item-from-a-checkedlistbox/ – Salah Akbari Jul 15 '16 at 05:43

4 Answers4

1

You should set the both:

listBox1.DataValueField="fieldValue" and listBox1.DataTextField="textValue"

listBox2.DataValueField="fieldValue" and listBox2.DataTextField="textValue"

Hamed Javaheri
  • 538
  • 3
  • 13
1

Use the GetItemText method:

listBox1.GetItemText(listBox1.SelectedItem);

For combobox use

this.ComboBox.GetItemText(this.ComboBox.SelectedItem);
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
0

While you can manipulate

listBox1.SelectedItem.ToString()

using a cast like

((DataRowView)listBox1.SelectedItem)["Enter column name here"].ToString();

to retrieve the data out of the SYSTEM.DATA.DATAROWVIEW you've added to the listbox, a better option might be to look into how you're populating your listbox in the first place so it just contains the strings you need and not a collection of SYSTEM.DATA.DATAROWVIEWs.

Ash
  • 5,786
  • 5
  • 22
  • 42
0

You can get the value of the selected item as

string variable =  listBox1.SelectedValue;

and the Text of the selected item as

string variable = listBox1.SelectedItem.Text;

Hope That helps !!!

prat14k
  • 117
  • 2
  • 10