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

GridDropDownColumn - is it possible to set datasource?

3 Answers 294 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 28 May 2015, 03:32 PM

GridDropDownColumn - is it possible to set datasource instead of datasourceID?

 

In the sample I see 

DataSourceID="SqlDataSource4"

But I'd like to bind the column in the code behind.

Our situation is a little unique. We have developed a framework which allows us to create dynamic grids that load based on the customer's needs and everything is generated on the pageinit and then loaded into a placeholder.

Right now we have to bind our dropdowns in the itemdatabound, which works but we could easily build it all in if we could set the datasource rather than just a datasourceid (but maybe there's a solution?)

 

Here's our method that creates the dropdown column (method is called in a foreach loop that loops through each column that needs to be generated, the code below is just one method of many that generates different column controls):

       

/// <summary>
/// Generates a dropdown field
/// </summary>
/// <param name="grid">radgrid where column is added</param>
/// <param name="column">column properties</param>
private static void CreateDropdownCol(RadGrid grid, SCSowFormColumn column)
{
    List<TableStructure> testList = new List<TableStructure>();
    GridDropDownColumn col = new GridDropDownColumn();
    col.UniqueName = column.SCSFC_Name;
    col.DataField = column.SCSFC_Name;
    col.HeaderText = column.SCSFC_ColumnHeaders;
    col.FilterControlWidth = Unit.Pixel(column.SCSFC_Width - 35);
    col.HeaderStyle.Width = Unit.Pixel(column.SCSFC_Width);
    col.AllowFiltering = column.SCSFC_AllowFilter;
    if (column.SCSFC_Required)
    {
        col.ColumnValidationSettings.EnableRequiredFieldValidation = column.SCSFC_Required;
        col.ColumnValidationSettings.RequiredFieldValidator.InitialValue = "(Select...)";//initial value is the text that is displayed
        col.ColumnValidationSettings.RequiredFieldValidator.ErrorMessage = " *Required";
        col.ColumnValidationSettings.RequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
        col.ColumnValidationSettings.RequiredFieldValidator.CssClass = "IPROD_ERROR_MESSAGE";
    }
 
    //checkto see if the column should be shown in itemview mode
    col.Display = !column.SCSFC_DoNotDisplay;
 
    //check to see if the column should be shown in edit mode
    col.ReadOnly = column.SCSFC_ReadOnly;
 

          //WOULD BE NICE IF WE COULD DO:
          //List<TestList> list = new List<TestList>();
          //col.DataSource = list;
          //col.ListTextField = "Name";
          //col.ListValueField = "ID";

    //multi column single line edit forms
    col.EditFormColumnIndex = column.SCSFC_ColumnIndex;
 
    grid.MasterTableView.Columns.Add(col);
}

 

Anyone have any ideas?

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 02 Jun 2015, 10:30 AM
Hi Phil,

You can refer to the answer in the support ticket that you have opened with the same question (ticket ID: 940306).

For other convenience, here is the answer from the support ticket:
"With GridDropDownColumn you can provide the data source directly to the generated editor, by handling the server-side OnItemDataBound event of the grid, when the item is in edit mode, getting reference to the editor and setting its DataSource property. Detailed information and example on this matter are available in the help article below:


Best Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Phil
Top achievements
Rank 1
answered on 03 Jun 2015, 01:56 PM
 
//check if we are using dropdown columns, if we are add sql datasource and automatically link dropdown to specific table
if (!string.IsNullOrEmpty(column.SCSFC_Dropdown_Table))
{
    col.DataSourceID = "sds" + column.SCSFC_Name;
    col.ListTextField = "Name";
    col.ListValueField = "ID";
    col.EnableEmptyListItem = true;
    col.EmptyListItemText = "(Select...)";
    col.EmptyListItemValue = "0";
    //col.ListDataMember
    SqlDataSource sqlDataSource = new SqlDataSource();
    sqlDataSource.ID = "sds" + column.SCSFC_Name;
    page.Controls.Add(sqlDataSource);
    sqlDataSource.ConnectionString = WebConfigurationManager.ConnectionStrings["IConnectionString"].ConnectionString;
    sqlDataSource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
  
    if (column.SCSFC_Dropdown_Table.StartsWith("MF"))
    {
        sqlDataSource.SelectCommand = "GetGenericLookupSP";
        sqlDataSource.SelectParameters.Add(new Parameter("TableName", System.TypeCode.String, column.SCSFC_Dropdown_Table));
        sqlDataSource.SelectParameters.Add(new Parameter("IDColumn", System.TypeCode.String, column.SCSFC_Drop_ID));
        sqlDataSource.SelectParameters.Add(new Parameter("SpeciesID", System.TypeCode.Int32, speciesID.ToString()));
        sqlDataSource.SelectParameters.Add(new Parameter("CultureID", System.TypeCode.String, cultureID));
    }
    else
    {
        sqlDataSource.SelectCommand = "GetStandardTableObjectLookupSP";
        sqlDataSource.SelectParameters.Add(new Parameter("TableName", System.TypeCode.String, column.SCSFC_Dropdown_Table));
        sqlDataSource.SelectParameters.Add(new Parameter("KeyName", System.TypeCode.String, "SC_ID"));
        sqlDataSource.SelectParameters.Add(new Parameter("KeyID", System.TypeCode.String, companyID.ToString()));
        sqlDataSource.SelectParameters.Add(new Parameter("TextColumn", System.TypeCode.String, column.SCSFC_Drop_Name));
        sqlDataSource.SelectParameters.Add(new Parameter("ValueColumn", System.TypeCode.String, column.SCSFC_Drop_ID));
        sqlDataSource.SelectParameters.Add(new Parameter("CodeColumn", System.TypeCode.String, column.SCSFC_Drop_Code));
        sqlDataSource.SelectParameters.Add(new Parameter("ActiveColumn", System.TypeCode.String, GetTableColumnPrefix(column.SCSFC_Drop_ID) + "_Active"));
  
    }
  
    sqlDataSource.Select(DataSourceSelectArguments.Empty);
}

 

 

For anyone who was curious, this is what I ended up getting to work. But I will be rewriting the method all together to use edititemtemplates Konstantin reconmended.

 

http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html

 

Thanks

0
Konstantin Dikov
Telerik team
answered on 08 Jun 2015, 07:27 AM
Hi Phil,

Thank you for sharing your approach with the community.

As a token of our gratitude we have updated your Telerik Points.


Best Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Phil
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Phil
Top achievements
Rank 1
Share this question
or