I have a button "Show data". When click it, data will display in list view. But I want to clear it and then click "Show data" again to display new data. Because I don't want when I click "Show data" again It duplicated data. So I want to clear old data to show new data (no duplicated data). But its header no clear (still keep header). What is the source code?
One more question. I want to show data from table in access to Datagridview in c#开发者_StackOverflow (eg. table in access contain: Name, Position, salary and Datagridview in c# has 3 column also: Name, Position and Salary by click on button "Show". What is the source code?
In WinForms:
listView1.Clear();
In WPF:
listView1.Items.Clear();
for listView on clicking first clear the list and then add items: like below
private void button9_Click(object sender, EventArgs e)
{
listView1.Clear();
listView1.Items.Add("Item1");
listView1.Items.Add("Item2");
listView1.Items.Add("Item3");
listView1.Items.Add("Item4");
listView1.Items.Add("Item5");
}
for your second question try below link:
How to show data from Access on C#?
What about the following?
ListView lv = new ListView();
while (lv.Items.Count > 1) {
//leave the header
lv.Items.RemoveAt(1);
}
While making windows store app write:
listview.Items.Clear();
Just listview.Clear(); won't work in windows store app
listView1.Items.Clear();
Clears in 2008 c# column headers with the data, if you have multiple ListView on the same form the best way is writing a method such as (suggested by Lasse as above)
private void ClearLvItems(ListView li)
{
while(li.Items.Count>1)
li.Items.RemoveAt(1);
}
Or if it does not work as expected as it does not work with me (one row still rests in Listview)
listView1.Items.Clear();
SetHeaders(li); // If you have more then one ListView in the same form. Otherwise don't use the parameters.
private void SetHeader(ListView li)
{
string[] header_names = new string[] {"Id","Name","SurName","Birth Date"};
int i = 0;
foreach (ColumnHeader ch in li.Columns)
{
ch.Text = header_names[i];
++i;
}
}
Another discussion is Here
ListView.Clear Method removes all items and columns from the control.
The following are some articles that may help you working with ListView and DataGridView controls:
ListView @ C# Online.Net
Working with Data—Using the DataGridView
foreach (ListView item in listview.Items)
{
ListView.Items.Remove(item);
}
or
ListView.Clear();
put this at the top of your "Show Data" button's click event.
private void showData_Click(object sender, EventArgs e) // done
{
listViewData.Items.Clear();
// your code
}
精彩评论