c#列表视图焦点问题

时间:2011-08-09 09:16:15

标签: c# listview

我有一个列表视图,显示数据库中的内容。我的表单中也有一个刷新按钮。点击刷新按钮后,列表视图再次更新。问题是当点击刷新按钮时列表视图中的所选项目将从焦点中删除。这是我的代码

       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项目的关注,即使点击了刷新按钮

2 个答案:

答案 0 :(得分:1)

您正在从GetData()顶部的列表视图中删除原始(某些选定的)项目。 在从数据库中读取新数据之前,您需要存储所选内容。然后,在listview中显示新数据后,您必须根据数据刷新前选择的内容选择项目。

//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上执行此操作之外,您只能在数据刷新之前执行此操作,除非您出于其他原因需要它。

答案 1 :(得分: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;


    }