• Is it possible in C# to join tables in ListView ?

    Ganesh Member

    Is it possible in C# to join tables in ListView ?

    Layer Access Data

            public static List GetFilmeByRealizador()
            {
                List res = new List();
    
                using (DB db = new DB(hostnameDB, userDB, passwordDB, database))//dados de ligaçao a base de dados
                {
                    string query = string.Format("SELECT {0}.Name_Movie, {0}.Director_id, {1}.Id, {1}.Name_Director FROM {0} INNER JOIN {1} ON {0}.Director_id={1}.id AND {1}.Name_Director=@Name_Director'", table, table2);
                    //string query = string.Format("SELECT * from {0}", table);
                    Dictionary parms = new Dictionary();
                   
                    //parms.Add("Name_Director", diretor.Name_Director);
    
                    using (DbDataReader dr = db.Query(query, parms))
                    {
                        while (dr.Read())
                        {
                            Movie movie = new Movie();
                            Director director = new Director();
                            MappingDB2OA(dr, director, movie);
                            res.Add(movie);
                        }
                    }
                }
                return (res);
            }
    
            private static void MappingDB2OA(DbDataReader dr, Director director, Movie movie)
            {
                movie.Name_Movie = dr["Name_Movie "] as string;
                movie.Director_id = (int)dr["Director_id "];
                director.Id = (int)dr["Id"];
                director.Name_Director = dr["Name_Director"] as string;
            }
    

    Form

                Director dir = new Director(0, txDiretor.Text, null, null);
    
                List list = Movie.GetFilmeByRealizador();
                Director dire;
                bl = new BindingList(list);
    
                foreach (Movie m in list)
                {
                    ListViewItem lvi = new ListViewItem(m.Name_Movie);
                    ......
                    lvi.SubItems.Add(m.Director_id.ToString());
    
                    listView1.Items.Add(lvi);
    

    I want name in @Name_Director (in query) text from textbox.

  • Adan Member

    Are you using the button to populate the list after they type in the text box? You can use the text box TextChanged action to dynamically update the list depending on what is typed. I would use Linq to query your list. If I have time later, I will build a working example. If you are using the button, then just query your list using Linq or similar. Either way you need a filter in the search box to get the results.

  • Amit Member

    ListView cannot be bound using a data source (at least out of the box). You may want to consider using a DataGridView. If you are using SQL you can add a reference to Microsoft.SqlServer.Management.Controls and use a SortableBindingList. From there you can use the search button to filter the main list or query the database.

    This can be done using Linq to filter the list. The below method will find results regardless of case, where m.name is the name of the movie.

    When the form initializes you can set the DataGridView data source to movies.

    dataGridView1.DataSource = movies;
    
    //Then on the button do the following.
    
    string search = textBox1.Text;
    
    var filtered = movies.FindAll(s => m.name.IndexOf(search, System.StringComparison.OrdinalIgnoreCase) >= 0);
    
    //If you had a DataGridView you can just change the source.
    dataGridView1.DataSource = filtered;
    

    If you still want to use a ListView then see the article below.

    http://www.codeproject.com/Articles/10008/Data-binding-a-ListView

  • Abhey Member

    Here is a simple sample using a DataGridView that you can paste into a new Windows form and load. The controls are dynamic. You should be able to modify it to fit your needs. You can also query the database directly using Linq to SQL. You could present the user with the full lookup from the database and use that as the original source and then a second query using the text box text variable to pull the filtered list.

    This text box text searches name, directory, and genre at the same time. Partial matches will work and it ignores case (same as with SQL Like).

    public partial class Movies : Form
        {
            private TextBox textBoxSearch;
            private List movies;
            private List filtered;
            private DataGridView dataGridMovies;
            private Button buttonSearch;
            public Movies()
            {
                InitializeComponent();
                textBoxSearch = new TextBox();
                movies = new List();
                filtered = new List();
                dataGridMovies = new DataGridView();
                buttonSearch = new Button();
                this.AutoSize = true;
                InitializeControls();
                InitializeMovies();
    
            }
            private void InitializeControls()
            {
                textBoxSearch.Location = new Point(15, 15);
    
                buttonSearch.Location = new Point(textBoxSearch.Width + 30, 15);
                buttonSearch.Text = "Search By Movie, Director, or Genre";
                buttonSearch.AutoSize = true;
                this.buttonSearch.Click += new System.EventHandler(this.buttonSearch_Click);
    
                dataGridMovies.Bounds = new Rectangle(new Point(15, textBoxSearch.Location.Y + 30), new Size(400, 200));
                dataGridMovies.DataSource = movies;
    
                this.Controls.Add(textBoxSearch);
                this.Controls.Add(dataGridMovies);
                this.Controls.Add(buttonSearch);
            }
            private void InitializeMovies()
            {
                movies.Add(new Movie { name = "Star Wars", genre = "Sci-Fi", year = "1977", director = "George Lucas" });
                movies.Add(new Movie { name = "The Matrix", genre = "Action", year = "1999", director = "Wachowski Brothers" });
                movies.Add(new Movie { name = "The Dark Knight", genre = "Comic Book", year = "2008", director = "Christopher Nolan" });
                movies.Add(new Movie { name = "The Hangover", genre = "Comedy", year = "2009", director = "Todd Phillips" });
            }   
            private void buttonSearch_Click(Object sender, EventArgs e)
            {
                string search = textBoxSearch.Text;
                filtered = movies.FindAll(m => m.name.IndexOf(search, System.StringComparison.OrdinalIgnoreCase) >= 0
                                            || m.director.IndexOf(search, System.StringComparison.OrdinalIgnoreCase) >= 0
                                            || m.genre.IndexOf(search, System.StringComparison.OrdinalIgnoreCase) >= 0);
    
                dataGridMovies.DataSource = filtered;
            }
           
        }
        public class Movie
        {
            public string director { get; set; }
            public string name { get; set; }
            public string genre { get; set; }
            public string year { get; set; }
        }
    
Viewing 3 reply threads
  • You must be logged in to reply to this topic.