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

Switch regular textbox for RadAutoCompleteBox

13 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 03 Oct 2013, 01:30 AM
Hello,

How would i be able to switch from using regular text boxes which uses
boundColumn = new GridBoundColumn();

When creating a radgrid dynamically to using Radautocomplete text boxes?

13 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 07 Oct 2013, 11:56 AM
Hello Brad,

Well you can create a GridAutoCompleteColumn the same way, then add it to your grid.

GridAutoCompleteColumn column1 = new GridAutoCompleteColumn();

Further more you can set the DataSourceID for column, to be able to populate the values inside the AutoCompleteBox during in insert/update/filter items.
Check this blog post for an example:
http://blogs.telerik.com/aspnet-ajax/posts/13-06-27/the-most-wanted-features-in-radgrid-for-asp.net-ajax-in-q2-2013

Regards,
Vasil
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 07 Oct 2013, 02:18 PM
What about if i have the following code:

RadGrid grid = new RadGrid();
        grid.ID = "RadGrid" + i.ToString();
        grid.Visible = bl;
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.AutoGenerateEditColumn = true;
        grid.AutoGenerateDeleteColumn = true;
        grid.AllowAutomaticInserts = false;
        grid.Width = Unit.Percentage(100);
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.AllowFilteringByColumn = false;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        grid.AllowAutomaticDeletes = false;
        grid.AllowAutomaticUpdates = false;
        grid.ItemDataBound += new GridItemEventHandler(grid_ItemDataBound);
        grid.InsertCommand += grid_InsertCommand;
        grid.DeleteCommand += grid_DeleteCommand;
        grid.UpdateCommand += grid_UpdateCommand;
        grid.MasterTableView.DataKeyNames = new string[] { "RowNumber" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "RowNumber";
        boundColumn.HeaderText = "RowNumber";
        boundColumn.ReadOnly = true;
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Size";
        boundColumn.HeaderText = "Size";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Description";
        boundColumn.HeaderText = "Description";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Quantity";
        boundColumn.HeaderText = "Quantity";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Unit";
        boundColumn.HeaderText = "Unit";
        grid.MasterTableView.Columns.Add(boundColumn);
         
        boundColumn = new GridBoundColumn();
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Duration";
        boundColumn.HeaderText = "Duration";
        grid.MasterTableView.Columns.Add(boundColumn);
 
        GridTemplateColumn objGridTemplateColumn = new GridTemplateColumn();
        objGridTemplateColumn.HeaderText = "DurationType";
        objGridTemplateColumn.DataField = "DurationType";
        objGridTemplateColumn.ItemTemplate = new MyTemplate("DurationType");
        objGridTemplateColumn.EditItemTemplate = new MyEditTemplate();
        grid.MasterTableView.Columns.Add(objGridTemplateColumn);
 
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Amount";
        boundColumn.HeaderText = "Amount";
        grid.MasterTableView.Columns.Add(boundColumn);
        grid.MasterTableView.EditMode = GridEditMode.InPlace;


If i try to add it to the following section it doesn't let me

boundColumn = new GridAutoCompleteColumn();
boundColumn.DataField = "Duration";
boundColumn.HeaderText = "Duration";
grid.MasterTableView.Columns.Add(boundColumn);
0
Vasil
Telerik team
answered on 07 Oct 2013, 02:36 PM
Hello Brad,

You are getting Compilation error because you have defined:
boundColumn as GridBoundColumn,
GridBoundColumn boundColumn = new GridBoundColumn();
And then you try to set the boundColumn variable to hold GridAutoCompleteColumn, which will not compile since they are different types.
So instead of
boundColumn = new GridAutoCompleteColumn();
Use:
GridAutoCompleteColumn column1 = new GridAutoCompleteColumn()
As I suggested earlier.

Regards,
Vasil
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 07 Oct 2013, 03:40 PM
Ok but for the datasourceID, i have a method that is called GetTable(), and i want to use that so it can get all the avaialble options for the autocomplete box, but how can i attach that to the datasourceID, it doesn't let me, and there is no option for datasource by itself.
0
Brad
Top achievements
Rank 1
answered on 07 Oct 2013, 03:48 PM
Ohh i forgot to mention the GetTable() method, grabs the data from a datatable
0
Vasil
Telerik team
answered on 08 Oct 2013, 07:58 AM
Hi Brad,

You can use the Grid's ItemCreated event, access the RadAutoCompleteBox that is rendered from the column and set it's datasource.
//  .....
    grid.ItemCreated += grid_ItemCreated; //attach the ItemCreated event
}
 
void grid_ItemCreated(object sender, GridItemEventArgs e)
{
    TableCell cell = e.Item.DataItem("IdOfYourColumn");
    if (cell.Controls.Count > 0 && cell.Controls[0] is RadAutoCompleteBox)
    {
        ((RadAutoCompleteBox)(cell.Controls[0])).DataSource = yourDataSource
    }
}
See this for more information about accessing controls inside the grid:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

Regards,
Vasil
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 08 Oct 2013, 01:44 PM
Thank you for your response, I am trying to put this in my code and get the following error on the following line:

TableCell cell = e.Item.DataItem("Duration");

Non-Invocable memeber DataItem can not be used as a method

0
Vasil
Telerik team
answered on 08 Oct 2013, 03:06 PM
Hi Brad,

Excuse me for the VB syntax in this part of the code.
The correct call should be e.Item.DataItem["Duration"]

Regards,
Vasil
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 08 Oct 2013, 03:16 PM
Hello,

I tried that and now the error says that it can't apply indexing.

But i tried this and it seems to work, but a few things.

if (e.Item is GridEditableItem && e.Item.IsInEditMode)
      {
          GridEditableItem editableItem = e.Item as GridEditableItem;
          TableCell cell = editableItem["Duration"];
          if (cell.Controls.Count > 0 && cell.Controls[0] is RadAutoCompleteBox)
          {
              ((RadAutoCompleteBox)(cell.Controls[0])).DataSource = GetTableForAuto();
          }
      }


1. How do i get the value for that field, i have the following code and it doesn't get the value, and throws me an error:

public void grid_UpdateCommand(object sender, GridCommandEventArgs e)
   {
       GridEditableItem editItem = e.Item as GridEditableItem;
       Hashtable newValues = new Hashtable();
       newValues["RowNumber"] = (editItem["RowNumber"].Controls[0] as TextBox).Text;
       newValues["Size"] = (editItem["Size"].Controls[0] as TextBox).Text;
       newValues["Description"] = (editItem["Description"].Controls[0] as TextBox).Text;
       newValues["Quantity"] = (editItem["Quantity"].Controls[0] as TextBox).Text;
       newValues["Unit"] = (editItem["Unit"].Controls[0] as TextBox).Text;
       newValues["Duration"] = (editItem["Duration"].Controls[0] as TextBox).Text; // This is the field that throws the error
       newValues["DurationType"] = (editItem.FindControl("DurationType-DDL") as DropDownList).SelectedValue;
       newValues["Amount"] = (editItem["Amount"].Controls[0] as TextBox).Text;
       DataTable dtCurrentTable = (DataTable)HttpContext.Current.Session[int.Parse(editItem.OwnerGridID.Split(new string[] { "RadGrid" }, StringSplitOptions.RemoveEmptyEntries)[0])];
 
       foreach (DictionaryEntry entry in newValues)
       {
           dtCurrentTable.Rows[e.Item.ItemIndex][entry.Key.ToString()] = entry.Value;
       }
       SaveTable(editItem.OwnerGridID);
 
   }

Error: Object reference not set to an instance of an object.

2. And how do i get it to only allow what ever is available to Autocomplete to be chosen?
3. For some reason, it lets me choose multiple items, i only want to be able to choose one.


Thank you
0
Vasil
Telerik team
answered on 09 Oct 2013, 10:21 AM
Hi Brad,

1.
newValues["Duration"] = (editItem["Duration"].Controls[0] as TextBox).Text;
Should be:
newValues["Duration"] = (editItem["Duration"].Controls[0] as RadAutoCompleteBox).Text;

3.
column.SelectionMode = RadAutoCompleteSelectionMode.Single;
Or when you access the RadAutoCompleteBox use:
((RadAutoCompleteBox)(cell.Controls[0])).TextSettings.SelectionMode = RadAutoCompleteSelectionMode.Single;

2.
Set the AllowCustomEntry = false; for your column or for the RadAutoCompleteBox control.

I hope this helps.

Regards,
Vasil
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 09 Oct 2013, 06:55 PM
Awesome, thanks for your reply.

1. This solution worked, thank yo very much.

I tried 2 and 3 but it still lets me choose multiple selections, and i want to be able to force it to be able to only choose whats available.

Below is how i modified my code.

Grid Code:
GridAutoCompleteColumn aboundColumn = new GridAutoCompleteColumn();
        aboundColumn.DataField = "Duration";
        aboundColumn.HeaderText = "Duration";
        aboundColumn.DataTextField = "Duration";
        aboundColumn.DataValueField = "Duration";
        aboundColumn.AllowCustomEntry = false;
        aboundColumn.SelectionMode = RadAutoCompleteSelectionMode.Single;
        grid.MasterTableView.Columns.Add(aboundColumn);


ItemCreated code:
private void grid_ItemCreated(object sender, GridItemEventArgs e)
    {
 
 
 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editableItem = e.Item as GridEditableItem;
            TableCell cell = editableItem["Duration"];
            if (cell.Controls.Count > 0 && cell.Controls[0] is RadAutoCompleteBox)
            {
                ((RadAutoCompleteBox)(cell.Controls[0])).TextSettings.SelectionMode = RadAutoCompleteSelectionMode.Single;
                ((RadAutoCompleteBox)(cell.Controls[0])).DataSource = GetTableForAuto();
                 
 
            }
        }
          
 
    }
0
Marin
Telerik team
answered on 14 Oct 2013, 09:21 AM
Hello,

 The settings in the code look OK. You can check whether the code in the ItemCreated event is executed and sets SelectionMode as expected. Additionally you can also set AllowCustomEntry to false in the ItemCreated event as well. With these settings the AutoCompleteBox should allow only single selection and no custom entry.

Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 14 Oct 2013, 02:20 PM
I have added the AllowCustomEntry to false, in the item created but that still did not fix the issue. I have also opened a ticket on this issue as well, Ticket 746315.
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Brad
Top achievements
Rank 1
Marin
Telerik team
Share this question
or