2011-08-09 57 views
0

我有一個listview顯示數據庫中的內容。我的表單中也有一個刷新按鈕。一旦刷新按鈕被點擊,listview被再次更新。問題是刷新點擊按鈕在ListView的就是從focus.This刪除已選擇的項目是我的代碼c#List view focus problem

 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) < now && dr.GetDateTime(3) > 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) <= 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) > 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 > -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 < listView1.SelectedItems.Count; i++) 
     { 


     //Selecting the each values of the selected item from listview 


     listView_Selected_Index = listView1.SelectedIndices[i]; 

     } 
    } 

誰能幫助我如何保持專注於ListView項甚至刷新按鈕獲得點擊

回答

1

您正在從GetData()的頂部的列表視圖中刪除原始(某些選定的)項目。 在從數據庫中讀取新數據之前,您需要在某處存儲選定內容。然後,在列表視圖中顯示新數據後,您必須根據數據刷新前選擇的內容來選擇項目。

//store selected items (id or sth else that identifies item) 
GetData(); 
//restore selection (some of previously items may no longer exist) 

在你的代碼中,你只保存最後選擇的項目不是全部。

for (int i = 0; i < listView1.SelectedItems.Count; i++) 
{ 
    //Selecting the each values of the selected item from listview 

    // here You are only saving last selected item 
    // instead of this do sth like 
    // selectedItems.Add(sth that identifies this item, not index) 
    listView_Selected_Index = listView1.SelectedIndices[i]; 
} 

同樣的,而不是做它在每一個的SelectedIndexChanged你可以做到這一點的數據只刷新之前,除非你想在其他一些原因。

0

你可以做的是,保存的ListView1當前選定的指數在一些臨時的野外,並呼籲GetData()方法通過分配臨時的野外價值重置的ListView1選定索引屬性ListView1.SelectedIndex財產

這樣的事情後:

private int _selectedIndex = -1; 

private void btnRefresh_Click(object sender, EventArgs e) 
    { 
     _selectedIndex = listView1.SelectedIndex; 
     //to refresh manually 
     this.Refresh(); 
     listView1.Items.Clear(); 
     /*btnEdit_Question.Enabled = true; 
     btnRepeat_Question.Enabled = true; 
     btnDelete_Question.Enabled = true;*/ 
     GetData(); 
     listView1.SelectexIndex = _selectedIndex; 


    } 
+0

爲什麼會員變量? ListView可以包含許多項目。 –

+0

一個臨時變量保存最後選擇的索引,最後我是指在調用ListView1.Items.Clear之前選定的索引 – Waqas

+0

它應該是方法局部變量。並且可以選擇很多項目。 –