C# listview select row(a row and multiple rows)
The listview control can be used to display a menu or a table. When used to display a table, you can select a row or multiple rows; it provides the corresponding properties, you only need to set the corresponding properties to true; you can set it in the control properties or use a sentence code.
Regardless of whether it is selecting a row or multiple rows in the listview table, it is generally necessary to fetch data for corresponding operations, which requires writing a program to achieve.
I. C# listview select row
Method one:
1. Select the listview control, click Properties, as shown in Figure 1:
Figure 1
2. Find FullRowSelect, click the drop-down box on its right, and select True.
Method Two:
Set with the following code:
listview1.FullRowSelect = true;
II. C# listview select multiple rows
1. Method 1: Find MultiSelect and set its value to True in Figure 1.
2. Method 2: Use the following code to set:
listview1.MultiSelect = true;
III. Get the value of listview selected rows
1. First, add the event SelectedIndexChanged that is triggered when the listview row is selected, as shown in Figure 2:
Figure 2
The event lv_SelectedIndexChanged is added in Figure 2, the following method will be automatically generated in the code:
private void lv_SelectedIndexChanged(object sender, EventArgs e)
{
}
2. Write code in this method to get the value of the selected rows of listview, the code is as follows:
private void lv_SelectedIndexChanged(object sender, EventArgs e)
{
if (listview1.SelectedItems.Count > 0)
{
string colunmName = listview1.Columns[0].Text;//Get the title name of first column
//Get the value of the first column of the selected row
string colunmVal1 = listview1.SelectedItems[0].SubItems[0].Text;
//Get the value of the second column of the selected row
string colunmVal2 = listview1.SelectedItems[0].SubItems[2].Text;
//Get the value when multiple rows are selected
foreach (ListViewItem lvi in listView1.SelectedItems)
{
string colunmVal1 = lvi.SubItems[0].Text);
string colunmVal2 = lvi.SubItems[1].Text);
}
}
}
-
Related Reading
- C# Read and write to text file Newline, with one lin
- C# merge binary array(byte array)
- C# Winform button transparent background with proper
- C# float, double range, precision, definition and ty
- C# split string with \n pay attention to the problem
- C# efficiency comparison of multiple strings concate
- C# date format and examples, with binding and ToStri
- Bubble sort algorithm example in C# and C (Ascending
- C# Hashtable create, add, update, remove and removed
- C# judge whether cookies are disabled on the client
- C# remove first or last character from string
- C# get url, domain, absolute, relative and physical