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

Add Dropdown list on grid headers

6 Answers 253 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vvamc
Top achievements
Rank 1
Vvamc asked on 23 May 2014, 06:34 PM
Hi ,

I need to display a grid with two headers where the top header gives the text and the bottom header displays a dropdownlist when auto generate Columns set to true.I have a checkbox column  too.I need to display dropdowns on grid headers. Please help me out.

Thanks in advance

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 May 2014, 06:19 AM
Hi,

You can try the below code snippet to add DropDownList in header:

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridHeaderItem)
    {
        foreach (GridColumn col in RadGrid1.Columns)
        {
            GridHeaderItem headerItem = (GridHeaderItem)e.Item;
            DropDownList ddl = new DropDownList();
            ddl.ID = "ddl" + col.UniqueName;
            ddl.DataSourceID = "SqlDataSource1";
            ddl.DataTextField = col.UniqueName;
            ddl.DataValueField = col.UniqueName;
            headerItem[col.UniqueName].Controls.Add(ddl);
        }
    }
}

Thanks,
Shinu
0
Vvamc
Top achievements
Rank 1
answered on 27 May 2014, 01:07 PM
Hi Shinu ,


I  want it this way.  One is a checkbox column the other with Autogeneratecolumns = true
0
Vvamc
Top achievements
Rank 1
answered on 27 May 2014, 01:13 PM
On Each Checkbox Click and select the dropdowns and click on apply button the row will be updated. I need to set the Dropdown for the headers dynamically and there should not be dropdown for checkbox column and others should have a dropdown.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 28 May 2014, 05:36 AM
Hi,

I guess you want the DropDown in the FilterTemplate. You can try the following code snippet. On your button click, access these dropdown and do the necessary functions.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowFilteringByColumn="true" OnPreRender="RadGrid1_PreRender" OnItemCreated="RadGrid1_ItemCreated">
</telerik:RadGrid>

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
    {
        RadGrid1.MasterTableView.GetColumn(col.UniqueName).ShowFilterIcon = false;
        RadGrid1.MasterTableView.GetColumn(col.UniqueName).AutoPostBackOnFilter = true;
    }
    RadGrid1.Rebind();
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    DropDownList ddl;
    if (e.Item is GridFilteringItem)
    {
        GridFilteringItem filterItem = (GridFilteringItem)e.Item;
        foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
        {
            if (col.ColumnType != "GridCheckBoxColumn")
            { 
                if (col.ColumnType == "GridBoundColumn")
                {
                    TextBox txtFilter = (TextBox)filterItem[col.UniqueName].Controls[0];
                    txtFilter.Visible = false;
                    ddl = new DropDownList();                 
                    ddl.ID = "ddl" + col.UniqueName;
                    ddl.DataSourceID = "SqlDataSource1";
                    ddl.DataTextField = col.UniqueName;
                    ddl.DataValueField = col.UniqueName;                  
                    filterItem[col.UniqueName].Controls.Add(ddl);
                }
                else if (col.ColumnType == "GridNumericColumn")
                {
                    RadNumericTextBox txtFilter = (RadNumericTextBox)filterItem[col.UniqueName].Controls[0];
                    txtFilter.Visible = false;
                    ddl = new DropDownList();                  
                    ddl.ID = "ddl" + col.UniqueName;
                    ddl.DataSourceID = "SqlDataSource1";
                    ddl.DataTextField = col.UniqueName;
                    ddl.DataValueField = col.UniqueName;                  
                    filterItem[col.UniqueName].Controls.Add(ddl);
                }
            }
        }
    }  
}

Thanks,
Shinu
0
Vvamc
Top achievements
Rank 1
answered on 28 May 2014, 02:58 PM
Thanks a lot shinu. If i want to update the values below so how can i find the control dropdownlist?
0
Shinu
Top achievements
Rank 2
answered on 29 May 2014, 04:39 AM
Hi,

I'm not sure where you want to access the DropDownList. below is a code snippet where I have accessed DropDownList in a buttons OnClick event.

C#:
protected void btnApply_Click(object sender, EventArgs e)
{
  foreach (GridFilteringItem filterItem in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
  {
    foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
    {
      //Access dropdowlist using its ID
      DropDownList ddl = (DropDownList)filterItem.FindControl("ddl" + col.UniqueName);
      //Gets the selected text
      string value = ddl.SelectedItem.Text;
    }
  }
}

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