This is a migrated thread and some comments may be shown as answers.

Radgrid with TextBox and Buttun Filter Template

1 Answer 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RB
Top achievements
Rank 1
RB asked on 17 Jul 2014, 10:03 PM
I need to implement a radgrid as follows:
1)The grid is hidden initially. Only the Filter Template is visible.
2) The Filter Template will have labels and text box and a button. (Only 2 column names)
3)On click of button the grid is displayed accordingly.

Can you please help me with this.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Jul 2014, 10:45 AM
Hi,

Please take a look at the sample code snippet and you can modify it according to your requirement.

C#:
RadGrid _RadGrid1;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        _RadGrid1.DataSource = String.Empty;
    }
}
protected void Page_Init(object source, System.EventArgs e)
{
    _RadGrid1 = new RadGrid();
    _RadGrid1.ID = "RadGrid1";
    _RadGrid1.MasterTableView.DataKeyNames = new string[] { "OrderID" };      
    _RadGrid1.AllowPaging = true;
    _RadGrid1.AutoGenerateColumns = false;      
    _RadGrid1.AllowFilteringByColumn = true;
    _RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
    _RadGrid1.EnableLinqExpressions = false;     
 
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "OrderID";
    boundColumn.HeaderText = "OrderID";      
    _RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "ShipCountry";
    boundColumn.HeaderText = "ShipCountry";       
    boundColumn.FilterTemplate = new MyFilterTemplate("ShipCountry",_RadGrid1);
    _RadGrid1.MasterTableView.Columns.Add(boundColumn);
    this.PlaceHolder1.Controls.Add(_RadGrid1);
}  
public class MyFilterTemplate : ITemplate
{
    private string colname;
    protected RadGrid grid;
    protected Label lblShipCountry;
    protected TextBox txtShipCountry;
    protected Button btnShipCountry;
    public MyFilterTemplate(string cName, RadGrid _RadGrid)
    {
        colname = cName;
        grid = _RadGrid;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        lblShipCountry = new Label();
        lblShipCountry.ID = "lblShipCountry";
        lblShipCountry.Text = colname;
        container.Controls.Add(lblShipCountry);
 
        txtShipCountry = new TextBox();
        txtShipCountry.ID = "txtShipCountry";
        container.Controls.Add(txtShipCountry);
 
        btnShipCountry = new Button();
        btnShipCountry.ID = "btnShipCountry";
        btnShipCountry.Text = "Filter";
        btnShipCountry.Click += new EventHandler(btnShipCountry_Click);
        container.Controls.Add(btnShipCountry);
    }
    void btnShipCountry_Click(object sender, EventArgs e)
    {
        string filterText =txtShipCountry.Text;
        grid.MasterTableView.FilterExpression = "([" + colname + "] ='" + filterText + "')"; ;
        grid.Rebind();
    }      
}
void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    _RadGrid1.DataSource = GetDataTable("SELECT  * FROM Orders");
}
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    DataTable myDataTable = new DataTable();
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
    return myDataTable;
}

Thanks,
Princy
Tags
Grid
Asked by
RB
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or