我需要在我的GridView中添加搜索。我有一个可折叠的gridview。因此,为了搜索目的,有没有办法在折叠的gridview中搜索某些内容并返回折叠的结果,然后展开项目以查看搜索的内容。
我制作了一个包含数千条记录的折叠教科书gridview。设置了分页,我有大约55页折叠形式的记录。
所以,如果有搜索书名和#34;大学会计"它将显示折叠的表单,然后查看搜索的内容,用户可以只展开返回的每个项目"大学会计"。
这可能吗?
答案 0 :(得分:1)
这是一个让你入门的小代码。但我不知道循环所有网格列和单元格是否是搜索数据的有效方式...
在此示例中,代码将停止在网格中第一个找到的searchterm上查找更多结果。如果你想要最后一个,你应该从循环中删除if (searchTermFound == false)
。
如果要匹配多个结果,则应将找到的列和单元格存储在List或Array中。
使用找到的rowIndexParent
和rowIndexChild
值,您可以在所需的行上展开网格。
private void searchGridView(string searchTerm)
{
int rowIndexParent = -1;
int cellIndexParent = -1;
int rowIndexChild = -1;
int cellIndexChild = -1;
bool searchTermFound = false;
//loop all rows in parent grid
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//remove this if you want the last match displayed as found, not the first
if (searchTermFound == false)
{
//loop all cells in parent grid
for (int j = 0; j < GridView1.Columns.Count; j++)
{
string cellContent = GridView1.Rows[i].Cells[j].Text;
if (cellContent.ToLower().Contains(searchTerm.ToLower()))
{
rowIndexParent = i;
cellIndexParent = j;
searchTermFound = true;
break;
}
}
//find the nested grid and cast it
GridView gv = GridView1.Rows[i].FindControl("GridView2") as GridView;
//loop all rows in child grid
for (int ii = 0; ii < gv.Rows.Count; ii++)
{
//loop all cells in child grid
for (int jj = 0; jj < gv.Columns.Count; jj++)
{
string cellContent = gv.Rows[ii].Cells[jj].Text;
if (cellContent.ToLower().Contains(searchTerm.ToLower()))
{
rowIndexParent = i;
rowIndexChild = ii;
cellIndexChild = jj;
searchTermFound = true;
break;
}
}
}
}
}
//cellIndexParent > -1 means searchTerm is found in parent grid, not child
if (searchTermFound == true && cellIndexParent > -1)
{
Response.Write("Searchterm \"" + searchTerm + "\" found in parent grid: row " + rowIndexParent + ", column " + cellIndexParent + ".");
}
else if (searchTermFound == true)
{
Response.Write("Searchterm \"" + searchTerm + "\" found in child grid: row " + rowIndexChild + ", column " + cellIndexChild + ", parent row " + rowIndexParent + ".");
}
else
{
Response.Write("Searchterm \"" + searchTerm + "\" not found.");
}
}
请注意,这仅适用于BoundField列,而不适用于TemplateField和AutoGenerated Columns。见下文。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<!-- search terms in these columns can be found -->
<asp:BoundField DataField="field01" HeaderText="Column A" />
<asp:BoundField DataField="field02" HeaderText="Column B" />
<asp:TemplateField>
<ItemTemplate>
<!-- search terms in this column cannot be found -->
<%# DataBinder.Eval(Container.DataItem, "field05").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<!-- search terms in these columns can be found -->
<asp:BoundField DataField="field03" HeaderText="Column C" />
<asp:BoundField DataField="field04" HeaderText="Column D" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>