简体   繁体   中英

Clear ListView items and update from second form

I have ListView (SessionList) in my main form ( Form1 ). And I'm listing items by getting value form mysql database by calling the function CreateSessionList() in Form1_Load() .

Now I want to filter. So, I have another form (FilterForm) invoked by using the menu from main form (Form1).

FilterForm contains a ComboBox for all column header of particular table. When I click the Filter button in second Form(Filter Form), I'm sending the selected data to first form(Form1), there I clear all previous items in List view and by having this new data for SELECT query to display new data in ListView .

What happened here is, previous list is not get cleared. It shows the count as zero. but View has the old data. No change at all.

Form1.cs Coding

private void CreateSessionList()
        {

            SessionList.View = View.Details;          
            SessionList.Items.Clear();         
            MySqlDataAdapter ada = new MySqlDataAdapter("SELECT * FROM sessiontbl", Properties.Settings.Default.DBPerfScoreConnectionString);            
            DataTable dt = new DataTable();
            ada.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem listitem = new ListViewItem((i+1).ToString());               
                listitem.SubItems.Add(dr["SessionID"].ToString());
                listitem.SubItems.Add(dr["InstID"].ToString());
                listitem.SubItems.Add(dr["Trainee"].ToString());
                listitem.SubItems.Add(dr["SessDt"].ToString());
                listitem.SubItems.Add(dr["ModelID"].ToString());
                listitem.SubItems.Add(dr["ModelName"].ToString());
                listitem.SubItems.Add("Alarm"); 
                SessionList.Items.Add(listitem);                
            } 
        }

public void GetWhereQuery(String Sesssel, String Inssel, String Trsel, String Modsel, String Dtsel)
        {
            SessionList.Items.Clear(); //After this,item count shows 0
            SessionList.Refresh();

            String sQuery = "SELECT * FROM sessiontbl WHERE (SessionID ='"+ Sesssel+"')";
            MySqlDataAdapter ada = new MySqlDataAdapter(sQuery, Properties.Settings.Default.DBPerfScoreConnectionString);
            DataTable dt = new DataTable();
            ada.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem listitem = new ListViewItem((i + 1).ToString());
                listitem.SubItems.Add(dr["SessionID"].ToString());
                listitem.SubItems.Add(dr["InstID"].ToString());
                listitem.SubItems.Add(dr["Trainee"].ToString());
                listitem.SubItems.Add(dr["SessDt"].ToString());
                listitem.SubItems.Add(dr["ModelID"].ToString());
                listitem.SubItems.Add(dr["ModelName"].ToString());
                listitem.SubItems.Add("Alarm");
                SessionList.Items.Add(listitem);
            } 

        }

FilterForm.cs Coding

private void Filterbtn_Click(object sender, EventArgs e)
        {            

            if(sessioncombo.SelectedIndex>-1)
                 Sesssel = sessioncombo.SelectedItem.ToString();
            if(InstructorCombo.SelectedIndex>-1)
                 Inssel = InstructorCombo.SelectedItem.ToString();
            if(TraineeCombo.SelectedIndex>-1)
                 Trsel = TraineeCombo.SelectedItem.ToString();
            if(ModelCombo.SelectedIndex>-1)
                 Modsel = ModelCombo.SelectedItem.ToString();
            if(Datecombo.SelectedIndex>-1)
                 Dtsel = Datecombo.SelectedItem.ToString();
            frm = new Form1();
            frm.GetWhereQuery(Sesssel, Inssel, Trsel, Modsel, Dtsel); //Calling function of form1 to filter.
            this.Close();


        }

Just send instance of Form1 to FilterForm like

FilterForm form = new FilterForm();
form.Form1 = this;
form.Show();

In your FilterForm create property of type Form1

public Form1 Form { get; set;}

and on Filterbtn_Click call `Form1.GetWhereQuery(...);

Your changes is not applied therefore u create new instance of Form1.

I followed this link.I got the solution. Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM