C# 按标签查找 DataGridView 项(行)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1821736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find DataGridView item (row) by Tag
提问by Captain Comic
Hi I have DataGridView control that I manyally fill with items (no DataSource)
嗨,我有 DataGridView 控件,我经常用项目填充(没有数据源)
like that
像那样
int row = dgvClients.Rows.Add();
dgvClients.Rows[row].Cells["ClientObjectID"].Value = somevalue1;
dgvClients.Rows[row].Cells["ClientCode"].Value = somevalue2;
dgvClients.Rows[row].Tag = SomeObject1;
Pls note that each row in gridview represents some object and its Tagis set to specific object. Only one row can have Tag reference to one SomeObject. No duplicates.
请注意 gridview 中的每一行都代表某个对象,并且它的Tag设置为特定对象。只有一行可以具有对一个 SomeObject 的 Tag 引用。没有重复。
Now I need to find datagridview ROW having reference to SomeObject. What is the best way?
现在我需要找到引用 SomeObject 的 datagridview ROW。什么是最好的方法?
采纳答案by Jon Comtois
Here is something I put together that might do what you are describing. It is quick and dirty, but it might get you going:
这是我放在一起的可能会做你所描述的事情。它又快又脏,但它可能会让你前进:
I have a blank DataGridView, a combobox, and a textbox on a form. TestObject is a class that is an object with 3 string properties for testing purposes of this example.
我在表单上有一个空白的 DataGridView、一个组合框和一个文本框。TestObject 是一个类,它是一个具有 3 个字符串属性的对象,用于此示例的测试目的。
For ease, I initialize a generic list with a few instances of TestObject. I then manually add 3 columns to the datagridview which correspond to the 3 properties of TestObject. I then iterate through the list and manually add them to the datagridview, as well as actually storing the object in the Row's tag property as well.
为方便起见,我用几个 TestObject 实例初始化了一个通用列表。然后我手动将 3 列添加到 datagridview,它们对应于 TestObject 的 3 个属性。然后我遍历列表并手动将它们添加到 datagridview,以及实际将对象存储在 Row 的 tag 属性中。
I then fill the combobox with references to the columns in the datagridview. The user will select which column she/he wants to search for, then type the text to match in the textbox.
然后我用对 datagridview 中列的引用填充组合框。用户将选择她/他要搜索的列,然后在文本框中键入要匹配的文本。
I then handle the textbox textchanged event to search the datagridview based on the column that is selected in the combobox and the text in the textbox. For a bigger dataset, you wouldn't want to handle the textchanged event because it would be too slow to search after every letter change.
然后我处理文本框 textchanged 事件以根据组合框中选择的列和文本框中的文本搜索 datagridview。对于更大的数据集,您不希望处理 textchanged 事件,因为在每次更改字母后搜索都太慢了。
Without using a datatable or datasource, I can't think of any easy way to search the rows without iterating. I don't know your requirements, but I would use a bindingsource with a list or set up a datatable at a minimum. With a bindingsource you could also apply a filter and dynamically show only those results that match your search.
如果不使用数据表或数据源,我想不出任何简单的方法来搜索行而不进行迭代。我不知道您的要求,但我会使用带有列表的 bindingsource 或至少设置一个数据表。使用 bindingsource,您还可以应用过滤器并仅动态显示与您的搜索匹配的结果。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<TestObject> objects = new List<TestObject>
{
new TestObject("1", "object1", "first"),
new TestObject("2", "object2", "nada"),
new TestObject("3", "object3", "Hello World!"),
new TestObject("4", "object4", "last")
};
dataGridView1.Columns.Add("ColID", "ID");
dataGridView1.Columns.Add("ColName", "Name");
dataGridView1.Columns.Add("ColInfo", "Info");
foreach (TestObject testObject in objects)
{
int row = dataGridView1.Rows.Add();
dataGridView1.Rows[row].Cells["ColID"].Value = testObject.ID;
dataGridView1.Rows[row].Cells["ColName"].Value = testObject.Name;
dataGridView1.Rows[row].Cells["ColInfo"].Value = testObject.Info;
dataGridView1.Rows[row].Tag = testObject;
}
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
comboBox1.Items.Add(col);
}
comboBox1.ValueMember = "HeaderText";
comboBox1.SelectedIndex = 0;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[((DataGridViewColumn)comboBox1.SelectedItem).Name].Value == null)
{
continue;
}
if (row.Cells[((DataGridViewColumn)comboBox1.SelectedItem).Name].Value.ToString().Equals(
textBox1.Text,StringComparison.InvariantCultureIgnoreCase))
{
row.Selected = true;
return;
}
}
}
}
}
public class TestObject
{
public TestObject(string id, string name, string info)
{
ID = id;
Name = name;
Info = info;
}
public string ID { get; set; }
public string Info { get; set; }
public string Name { get; set; }
}
回答by Wade73
You could use LINQ and set the results as the datagridview datasource. This way all you would need to do is run the LINQ, bind it to the datagridview, and then refresh it.
您可以使用 LINQ 并将结果设置为 datagridview 数据源。这样,您需要做的就是运行 LINQ,将其绑定到 datagridview,然后刷新它。
In VB I would code it as so:
在VB中,我会这样编码:
dim results = from obj in objects _
where obj.<property> = <value> _
select obj
datagridview1.datasource = results.tolist()
datagridview1.refresh()
If no one else comes up with a better answer I will create a test project this evening to give you practical C# code.
如果没有其他人提出更好的答案,我将在今晚创建一个测试项目,为您提供实用的 C# 代码。
Wade
韦德