如何将单个静态值绑定到gridview? [英] how to bind the single static value to gridview?

查看:95
本文介绍了如何将单个静态值绑定到gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态值的下拉列表(来自数据表)。

我已经为它添加了一个静态值(请选择)。

ddl绑定到gridview for dynamic values。

那么如何将单个静态值绑定到gridview?

解决方案

从你共享的代码中,我是什么理解是 - 你有一个下拉列表和一个用于过滤gridview的文本框。



每次用户更改选择一个项目时,都不需要在下拉列表中使用新的列表项目下拉列表。最好在绑定下拉列表时插入此静态列表。



您共享的代码 -

  protected   void  ddlCountries_SelectedIndexChanged( object  sender, EventArgs e)
{
DataTable dtCountries =(DataTable)Session [ GrdSearch];
DataView dv = new DataView(dtCountries);
dv.RowFilter = Country =' + ddlCountries.SelectedItem + 'AND fullname like'% + Convert.ToString(txtfullname.Text)+ %';
grdSearch.DataSource = dv;
grdSearch.DataBind();
ddlCountries.Items.Insert( 0 new System.Web.UI.WebControls.ListItem ( 请选择 < span class =code-string>));
}





建议更改 -

  protected   void  ddlCountries_SelectedIndexChanged( object  sender,EventArgs e)
{
DataTable dtCountries =(DataTable)Session [ GrdSearch];
DataView dv = new DataView(dtCountries);
string filterQuery = fullname like'% + Convert.ToString(txtfullname.Text.Trim())+ %';
if (ddlCountries.SelectedIndex> 0)
{
filterQuery + = AND Country =' + ddlCountries.SelectedValue + ';
}
dv.RowFilter = filetrQuery;
grdSearch.DataSource = dv;
grdSearch.DataBind();
}





还要确保将dropdownlist的 AutoPostBack 属性设置为是的这样就会触发SelectedIndexChanged事件。



希望,这有帮助。如果这不能解决您的问题,请告诉我:)


避免dv.rowfilter为ddlCountries选择值。



添加以下代码,其中包含ddlCoutries的绑定数据。删除你添加的地方。



ddlCountries.Items.Insert(0,new System.Web.UI.WebControls.ListItem(Please Select,)) ;

I have a dropdownlist of dynamic values (from datatable).
I had added a static value to it("Please select").
ddl is binded to gridview for dynamic values.
So how to bind the single static value to gridview?

解决方案

From the code you have shared, what I understood is - you have a dropdownlist and a textbox placed for filtering gridview.

There is no need to a new listitem to the dropdownlist each time user changes selects an item from the dropdownlist. It's better to insert this static listitme at time of binding the dropdownlist.

Code you have shared-

protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dtCountries = (DataTable)Session["GrdSearch"];
    DataView dv = new DataView(dtCountries);
    dv.RowFilter = "Country='" + ddlCountries.SelectedItem + "' AND fullname like '%" + Convert.ToString(txtfullname.Text) + "%'";
    grdSearch.DataSource = dv;
    grdSearch.DataBind();
    ddlCountries.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Please Select", ""));
}



Suggested changes-

protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dtCountries = (DataTable)Session["GrdSearch"];
    DataView dv = new DataView(dtCountries);
    string filterQuery="fullname like '%" + Convert.ToString(txtfullname.Text.Trim()) + "%'";
    if(ddlCountries.SelectedIndex>0)
    {
        filterQuery += " AND Country='"+ddlCountries.SelectedValue+"'";
    }
    dv.RowFilter = filetrQuery;
    grdSearch.DataSource = dv;
    grdSearch.DataBind();
}



Also make sure that you have AutoPostBack property of dropdownlist set to true so that the SelectedIndexChanged event will get fired.

Hope, it helps. In case this doesn't resolve your problem, please let me know :)


avoid dv.rowfilter for ddlCountries selected value of "".

add following code where you have bind data for ddlCoutries. remove where you have added.

ddlCountries.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Please Select", ""));


这篇关于如何将单个静态值绑定到gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆