gpt4 book ai didi

c# - 筛选 gridview 数据源

转载 作者:行者123 更新时间:2023-11-30 20:47:01 27 4
gpt4 key购买 nike

我不希望能够使用代码隐藏来搜索我的 gridview 结果。

我有一个按钮和一个 GridView :

<asp:TextBox ID="txtPaperId" Width="146" runat="server"  />
<asp:Button runat="server" ID="btnSearch" Text="Search" OnClick="FilterResult"/>

<asp:GridView ID="gvwResavePositions" runat="server" EmptyDataText="No Positions found!"
AllowPaging="True" AllowSorting="True" PageSize="50" AutoGenerateColumns="False"
SkinID="gridviewGridlinesSkin" HeaderStyle-HorizontalAlign="Left"
OnPageIndexChanging="gvwResavePositions_PageIndexChanged"
HorizontalAlign="Left" Width="100%">
<HeaderStyle BackColor="DarkGray" Font-Bold="True" HorizontalAlign="left" />
<RowStyle HorizontalAlign="Left" />
<Columns>
<asp:BoundField ItemStyle-Width="40px" DataField="strPaperId" HeaderText="K+Id / PaperId" ReadOnly="True"/>
<asp:TemplateField HeaderText="Resave">
<ItemTemplate>
<asp:CheckBox ID="bResave" runat="server" Width="50"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>

我的代码背后:

protected void FilterResult(object sender, EventArgs e)
{
try
{
(gvwResavePositions.DataSource as DataTable).DefaultView.RowFilter = string.Format("strPaperId = '{0}'",
txtPaperId.Text);
}
catch (Exception ex)
{
var t = ex.Message;
}
}

当我按下“搜索”按钮时,我得到“对象引用未设置到对象的实例。”

objectdatasource 在按下搜索按钮之前设置(它是另一个按钮......),所以当 FilterResult 运行时,gridview 充满了结果..

你有什么技巧可以做到这一点吗?

编辑

解决了。这是因为我的数据源是空的,所以我不得不重新绑定(bind)它。

最佳答案

试试这个:

// save your datatable in session while binding gridview
// Session["Dt_GridView"]=Your_datatable;
protected void FilterResult(object sender, EventArgs e)
{
try
{
// DataTable dt = (DataTable)gvwResavePositions.DataSource; this reutrn null
// hence
//gvwResavePositions.DataSource as DataTable this will return null



DataTable dt = (DataTable)Session["Dt_GridView"];

dt.DefaultView.RowFilter = string.Format("strPaperId = '{0}'",
txtPaperId.Text);
gvwResavePositions.DataSource = dt;
gvwResavePositions.DataBind();
}
catch (Exception ex)
{
var t = ex.Message;
}
}

关于c# - 筛选 gridview 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26180447/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com