title: c# List view focus problem
body: <p>I have a listview which display the content from the database.I also have a refresh button in my form.Once the refresh button is clicked the listview is get updated once again.The problem is when the refresh button is clicked the already selected item in the listview is get removed from the focus.This is my code</p>
  
  <pre><code>       private void btnRefresh_Click(object sender, EventArgs e)
      {
          //to refresh manually
          this.Refresh();
          listView1.Items.Clear();
          /*btnEdit_Question.Enabled = true;
          btnRepeat_Question.Enabled = true;
          btnDelete_Question.Enabled = true;*/
          GetData();
  
  
      } 
  
       public void GetData()
      {
  
          try
          {
              now = DateTime.Now;
              String time_date = now.ToString();
              myConnection = new SqlConnection(connectString);
             listView1.Items.Clear();
              myConnection.Open();
              String MyString1 = string.Format("SELECT " + data_variables.RES_TXT_STRING_COLUMN1 + "," + data_variables.RES_TXT_STRING_COLUMN2 + "," + data_variables.RES_TXT_STRING_COLUMN3 + "," + data_variables.RES_TXT_STRING_COLUMN4 + "," + data_variables.RES_TXT_STRING_COLUMN6 + " FROM " + data_variables.RES_TXT_STRING_QUESTION_TABLE);
              com = myConnection.CreateCommand();
              com.CommandText = MyString1;
              dr = com.ExecuteReader();
              ListViewItem itmX;
              //Adding the Items To The Each Column
              while (dr.Read())
              {
                  itmX = new ListViewItem();
                  itmX.Text = dr.GetValue(0).ToString();
                  ListViewItem.ListViewSubItem aSubFooItem1 = new ListViewItem.ListViewSubItem(itmX, dr.GetValue(1).ToString()); //Creating subitems for the parent item
                  itmX.SubItems.Add(aSubFooItem1);
                  //Associating these subitems to the parent item
                  ListViewItem.ListViewSubItem aSubFooItem2 = new ListViewItem.ListViewSubItem(itmX, dr.GetValue(2).ToString()); //Creating subitems for the parent item
  
                  ListViewItem.ListViewSubItem aSubFooItem3 = new ListViewItem.ListViewSubItem(itmX, dr.GetValue(3).ToString()); //Creating subitems for the parent item
  
                  if (dr.GetValue(4).ToString() == "0")
                  {
                      aSubFooItem5 = new ListViewItem.ListViewSubItem(itmX, "No");
                  }
                  else
                  {
                      aSubFooItem5 = new ListViewItem.ListViewSubItem(itmX, "Yes");
                  }
  
                  if (dr.GetDateTime(2) &lt; now &amp;&amp; dr.GetDateTime(3) &gt; now)
                  {
                      itmX.SubItems.Add(aSubFooItem2);
                      itmX.SubItems.Add(aSubFooItem3);
                      ListViewItem.ListViewSubItem aSubFooItem4 = new ListViewItem.ListViewSubItem(itmX, "In Progress");
                      itmX.SubItems.Add(aSubFooItem4);
                      itmX.SubItems.Add(aSubFooItem5);
                    }
                  else if (dr.GetDateTime(2) &lt;= now)
                  {
  
                      itmX.SubItems.Add(aSubFooItem2);
                      itmX.SubItems.Add(aSubFooItem3);
                      ListViewItem.ListViewSubItem aSubFooItem4 = new ListViewItem.ListViewSubItem(itmX, "Expired");
                      itmX.SubItems.Add(aSubFooItem4);
                      itmX.SubItems.Add(aSubFooItem5);
                   }
                  else if (dr.GetDateTime(2) &gt; now)
                  {
  
                      itmX.SubItems.Add(aSubFooItem2);
                      itmX.SubItems.Add(aSubFooItem3);
                      ListViewItem.ListViewSubItem aSubFooItem4 = new ListViewItem.ListViewSubItem(itmX, "Not Expired");
                      itmX.SubItems.Add(aSubFooItem4);
                      itmX.SubItems.Add(aSubFooItem5);
                  }
                  //add all the items ti listview
                  listView1.Items.Add(itmX);
                  //Adding colors
                  itmX.UseItemStyleForSubItems = false;
                  foreach (ListViewItem lvi in listView1.Items)
                  {
                      if (lvi.SubItems[4].Text=="Expired")
                      {
  
                         lvi.SubItems[4].BackColor = Color.Red;
                      }
                      else if (lvi.SubItems[4].Text == "Not Expired")
                      {
                          itmX.SubItems[4].BackColor = Color.Yellow;
                      }
                      else
                      {
                          itmX.SubItems[4].BackColor = Color.Green;
                      }
  
                  }
  
              }
              EventLog log = new EventLog(data_variables.RES_TXT_STRING_LOG_EVENT);
              try
              {
  
                  log.Source = data_variables.RES_TXT_STRING_LOG_SOURCE;
  
                  log.WriteEntry(data_variables.REX_TXT_STRING_MESSAGE_SUCCESSFUL, EventLogEntryType.Information);
              }
           if (listView_Selected_Index &gt; -1)
          {
  
                  //Keep the focus in the listview
                 this.listView1.Items[listView_Selected_Index].Focused = true;
  
  
          }
  
            private void listView1_SelectedIndexChanged(object sender, EventArgs e)
      {
  
          now = DateTime.Now;
          for (int i = 0; i &lt; listView1.SelectedItems.Count; i++)
          {
  
  
           //Selecting the each values of the selected item from listview
  
  
           listView_Selected_Index = listView1.SelectedIndices[i];
  
          }
       }
  </code></pre>
  
  <p>Can anyone help me how to remain the focus on the listview item even the refresh button is get clicked</p>