Raja aar
Raja aar

Reputation: 69

how to display value in listbox from dataview

i have a textbox and listbox in a winform app. when i type a value(i.e,string) in the textbox i want the listbox to show the values from datatable and when i am selecting a particular value from listbox it will display in textbox .

code:

private void txtIName_TextChanged(object sender, EventArgs e)
        {
            string constring = "Data Source=.;Initial Catalog=Test;User Id=sa;Password=admin@123";
            using (SqlConnection con = new SqlConnection(constring))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT distinct * FROM Item_Details", con))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        using (dt = new DataTable())
                        {
                            sda.Fill(dt);                            
                            DataView dv = new DataView(dt);
                            dv.RowFilter = string.Format("IName Like '%{0}%'", txtIName.Text);
                            listBox1.Visible = true;
                            listBox1.DataSource = dv;

                        }
                    }
                }
            }
        }

but i got the output in listbox like "system.data.datarow". By debugging i can seen that rows which i want, in dataview 'dv'. what i am missing here? thanks.

Upvotes: 2

Views: 1938

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125217

You don't need to load data each time the TextChanged event fires. It's enough to load data in Load event of your form and then in TextChanged just filter the data. Then using an event like DoubleClick of ListBox set the text of TextBox:

private DataTable dataTable = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
    string constring = @"Data Source=.;Initial Catalog=Test;User Id=sa;Password=admin@123";
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("SELECT distinct * FROM Item_Details", con))
        {
            sda.Fill(dataTable);
        }
    }
    this.listBox1.DataSource = new DataView(dataTable);
    this.listBox1.DisplayMember = "IName";
    this.listBox1.Visible = false;
}
private void txtIName_TextChanged(object sender, EventArgs e)
{
    var dv = (DataView)this.listBox1.DataSource;
    dv.RowFilter = string.Format("IName Like '%{0}%'", txtIName.Text);
    listBox1.Visible = true;
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
    var item = (DataRowView)listBox1.SelectedItem;
    if (item != null)
        this.txtIName.Text = item["IName"].ToString();
    else
        this.txtIName.Text = "";

    this.listBox1.Visible = false;
}

Don't forget to attach Form1_Load, txtIName_TextChanged and listBox1_DoubleClick handlers to events.

Upvotes: 1

user5447154
user5447154

Reputation:

You must specify your DisplayMember, it must be some Field in your DataView

listBox1.DisplayMember = "Your Field" 

then you can suscribe to the event SelectedValueChanged :

 listBox1.SelectedValueChanged += new EventHandler(listBox1_SelectedValueChanged);

visual studio create for you an event handler

  void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            // you get your selected item here
        }

hope it helps

Upvotes: 0

Related Questions