다음은 시작하기위한 간단한 코드입니다. 그러나 모든 gridcolumns와 셀을 반복하는 것이 데이터를 검색하는 효율적인 방법인지는 모르겠다. ...
이 예제에서 코드는 그리드에서 처음 발견 된 검색 엔진에서 더 많은 결과를 찾는 것을 멈출 것이다. 마지막으로 루프를 원하면 if (searchTermFound == false)
을 루프에서 제거해야합니다. 결과가 여러 개 일치하도록하려면 발견 된 열과 셀을 목록 또는 배열에 저장해야합니다. rowIndexParent
및 rowIndexChild
값이 있으면 필요한 행에서 눈금을 확장 할 수 있습니다. 이것은 단지 BoundField 열,하지 TemplateField 및 자동 생성됨 열이 작동
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.");
}
}
참고. 아래를 참조하십시오.
<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>
그래서 난 그냥 용어를 검색 할 동일한 검색어에 대해 사용하고 텍스트 상자를 설정합니다? txtSearch.Text = searchTerm; 그런 다음 btnSearch_click에서이 메서드를 호출합니까? – Norque
그게 효과가있다. 검색어의 길이를 3 자 이상으로 확인하십시오. 그렇지 않으면 오탐 (false positive)이 많이 발생합니다. – VDWWD
검색 결과에서 찾은 결과 만 쉽게 표시 할 수 있습니까? 아니면 약간의 일입니까? 이 것이 효과가 있기 때문에! 이제 발견 된 정보 만 표시하려면 그리드 뷰를 새로 고쳐야합니다. – Norque