C# clear listview columns cells

对着背影说爱祢 提交于 2019-12-12 02:45:47

问题


I have some columns in listview which are "Column1", "Column2",.... I want to clear (set null character at that cells) and remove some cells from related columns which can be (1.column) "Column1".Could you help me?

Specific question: After selected rows deleted, I want to update my ID column with updated numbers with a button. How can I do that?

for (int i = 1; i <= newID; i++)
{
    int newID = listView1.Items.Count;
    listView1.Items[0].Remove(listView1.Columns[0].ListView.Items.Count); // something like that ?
    listView1.Items.Add(i.ToString());
}

回答1:


After some modicifation with others code u can get what you want with button.

 if (listView1.Items.Count == 0) return;
            var col = listView1.Columns.Cast<ColumnHeader>()
                                .Select((x, i) => new { x, i })
                                .FirstOrDefault(a => a.x.Text == "ID");
            if (col == null) return;
            foreach (ListViewItem item in listView1.Items)
            {
                item.SubItems[col.i].Text = "";
            }
            int newID = listView1.Items.Count;
            for (int i = 1; i <= newID; i++)
            {
                listView1.Items[i-1].SubItems[0].Text = i.ToString();
            }


来源:https://stackoverflow.com/questions/24122750/c-sharp-clear-listview-columns-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!