突出显示匹配搜索结果的网格中的单元格 [英] Highlight Cell in a grid which matches search result
本文介绍了突出显示匹配搜索结果的网格中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Dim DataGridView1 = 新 DataGridView
' 将列添加到DGV
DataGridView1.Columns.Add( 年, 年 )
DataGridView1.Columns.Add( Make, 生成)
DataGridView1.Columns.Add( 模型, 模型)
DataGridView1.Columns( Year)。AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns( Make)。AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns( Model)。AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.RowHeadersVisible = False
' 将项目添加到DGV
DataGridView1.Rows.Add( 2010, 雪佛兰, Camaro)
DataGridView1.Rows.Add ( 2006, Ford, 野马)
DataGridView1.Rows.Add( 2008, Dodge, Charger)
DataGridView1 .Rows.Add( 2007, 雪佛兰, Corvette )
在这个Datagridview中,如果我在文本框中输入ro,我需要任何具有ro的列(例如:Chevrolet,Camaro)应该突出显示。 b>请给我一个解决方案
解决方案
您可以使用cell formate事件或使用以下代码。
< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting%28v=vs.110%29.aspx> http://msdn.microsoft.com /en-us/library/system.windows.forms.datagridview.cellformattin g%28v = vs.110%29.aspx [ ^ ]
< span class =code-keyword> for ( int i = 0 ; i < dataGridView1.Rows.Count; i ++)
{
if (dataGridView1.Rows [i] .Cells [ 生成]。Value.ToString()。ToUpper()。包含( ro))
{
dataGridView1.Rows [i] .Cells [ 生成]。Style = new DataGridViewCellStyle(){Font = new Font(dataGridView1.Font,FontStyle.Bold),ForeColor = Color.DodgerBlue};
// 它会根据您的要求改变您可以使用的单元格前景
// 与forecolor相同,您可以更改背景颜色或单元格以及行。
}
// ro
// 或者您可以使用文本框值。
if (dataGridView1 .Rows [i] .Cells [ Model]。Value.ToString()。ToUpper() .Contains(txtBox1.Text))
{
dataGridView1.Rows [i] .Cells [ 模型]。Style = new DataGridViewCellStyle(){Font = new Font(dataGridView1.Font,FontStyle.Bold),ForeColor = Color.DodgerBlue};
// 您可以使用
后的完整行颜色 // dataGridView1.Rows [i] .DefaultCellStyle = new DataGridViewCellStyle(){Font = new Font(dataGridView1.Font,FontStyle .Bold),ForeColor = Color.DodgerBlue,BackColor = Color.Green};
}
}
如果有任何问题请告诉我。
- > M.U
Dim DataGridView1 = New DataGridView
'add columns to DGV
DataGridView1.Columns.Add("Year", "Year")
DataGridView1.Columns.Add("Make", "Make")
DataGridView1.Columns.Add("Model", "Model")
DataGridView1.Columns("Year").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns("Make").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns("Model").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.RowHeadersVisible = False
'add items to DGV
DataGridView1.Rows.Add("2010", "Chevrolet", "Camaro")
DataGridView1.Rows.Add("2006", "Ford", "Mustang")
DataGridView1.Rows.Add("2008", "Dodge", "Charger")
DataGridView1.Rows.Add("2007", "Chevrolet", "Corvette")
In this Datagridview if i type "ro" in a text box i need any column that having "ro" (eg: Chevrolet,Camaro ) should be highlight. Please give me a solution for this
解决方案
You can use cell formate event or use following code.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting%28v=vs.110%29.aspx[^]
for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Cells["Make"].Value.ToString().ToUpper().Contains("ro")) { dataGridView1.Rows[i].Cells["Make"].Style = new DataGridViewCellStyle() { Font = new Font(dataGridView1.Font, FontStyle.Bold), ForeColor = Color.DodgerBlue }; //it will change cell forecolor you can use according to you required action // same like forecolor you can change backcolor or cell and also row. } //ro // or you can use textbox value. if (dataGridView1.Rows[i].Cells["Model"].Value.ToString().ToUpper().Contains(txtBox1.Text)) { dataGridView1.Rows[i].Cells["Model"].Style = new DataGridViewCellStyle() { Font = new Font(dataGridView1.Font, FontStyle.Bold), ForeColor = Color.DodgerBlue }; // for full row colors you can use following // dataGridView1.Rows[i].DefaultCellStyle = new DataGridViewCellStyle() { Font = new Font(dataGridView1.Font, FontStyle.Bold), ForeColor = Color.DodgerBlue, BackColor = Color.Green }; } }
if any question then let me know.
-> M.U
这篇关于突出显示匹配搜索结果的网格中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文