i want create search in form similar EXCEL , find and put (row) in listview this my form :
我希望以类似EXCEL的形式创建搜索,在listview中找到并放入(行)这个表单:
and my code :
和我的代码:
private int searchIndex = -1;
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Find Next";
try
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
searchIndex = (searchIndex + 1) % dataGridView1.Rows.Count;
DataGridViewRow row = dataGridView1.Rows[searchIndex];
if (row.Cells["Product"].Value == null)
{
continue;
}
if (row.Cells["Product"].Value.ToString().Trim().StartsWith(textBox1.Text) || row.Cells["Product"].Value.ToString().Trim().Contains(textBox1.Text))
{
ListViewItem lvi = new ListViewItem(row.Cells["Product"].Value.ToString());
lvi.SubItems.Add(row.Cells["Product"].Value.ToString());
listView1.Items.Add(lvi);
dataGridView1.CurrentCell = row.Cells["Product"];
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[row.Index].Index;
row = dataGridView1.Rows[i];
return;
}
}
}
catch { }
}
and in textbox1_textchanged :
并在textbox1_textchanged中:
searchIndex = -1;
button1.Text = "Find";
listView1.Clear();
I want when search end, send message ... thanks
我想在搜索结束时发送消息...谢谢
1 个解决方案
#1
Mimicking the Find All
functionality, populating your ListView
will be very similar to the Find Next
functionality. Here's an example if it were a separate button from Find Next
:
模仿“查找全部”功能,填充ListView将与“查找下一个”功能非常相似。以下是一个示例,如果它是查找下一个单独的按钮:
public Form1()
{
InitializeComponent();
listView1.View = View.Details;
listView1.Columns.Add("Column");
listView1.Columns.Add("Row");
listView1.Columns.Add("Value");
}
private void button2_Click(object sender, EventArgs e)
{
button2.Text = "Find All";
int tempIndex = -1;
listView1.Items.Clear();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
tempIndex = (tempIndex + 1) % dataGridView1.Rows.Count;
DataGridViewRow row = dataGridView1.Rows[tempIndex];
if (row.Cells["Foo"].Value == null)
{
continue;
}
if (row.Cells["Foo"].Value.ToString().Trim().StartsWith(textBox1.Text) || row.Cells["Foo"].Value.ToString().Trim().Contains(textBox1.Text))
{
DataGridViewCell cell = row.Cells["Foo"];
ListViewItem lvRow = new ListViewItem(new string[] { cell.ColumnIndex.ToString(), cell.RowIndex.ToString(), cell.Value.ToString() });
listView1.Items.Add(lvRow);
}
}
MessageBox.Show("Search ended."); // Or whatever message you intend to send.
}
#1
Mimicking the Find All
functionality, populating your ListView
will be very similar to the Find Next
functionality. Here's an example if it were a separate button from Find Next
:
模仿“查找全部”功能,填充ListView将与“查找下一个”功能非常相似。以下是一个示例,如果它是查找下一个单独的按钮:
public Form1()
{
InitializeComponent();
listView1.View = View.Details;
listView1.Columns.Add("Column");
listView1.Columns.Add("Row");
listView1.Columns.Add("Value");
}
private void button2_Click(object sender, EventArgs e)
{
button2.Text = "Find All";
int tempIndex = -1;
listView1.Items.Clear();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
tempIndex = (tempIndex + 1) % dataGridView1.Rows.Count;
DataGridViewRow row = dataGridView1.Rows[tempIndex];
if (row.Cells["Foo"].Value == null)
{
continue;
}
if (row.Cells["Foo"].Value.ToString().Trim().StartsWith(textBox1.Text) || row.Cells["Foo"].Value.ToString().Trim().Contains(textBox1.Text))
{
DataGridViewCell cell = row.Cells["Foo"];
ListViewItem lvRow = new ListViewItem(new string[] { cell.ColumnIndex.ToString(), cell.RowIndex.ToString(), cell.Value.ToString() });
listView1.Items.Add(lvRow);
}
}
MessageBox.Show("Search ended."); // Or whatever message you intend to send.
}