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

[Solved] Select checkbox when textbox in grid clicked

1 Answer 207 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RB
Top achievements
Rank 1
RB asked on 08 Jul 2014, 10:27 PM
I have a radgrid with a checkbox column and textbox column as follows:

           this._RadGrid1.AllowFilteringByColumn = true;
            this._RadGrid1.AllowSorting = true;           
            this._RadGrid1.MasterTableView.AllowNaturalSort = true;          
            
            this._RadGrid1.MasterTableView.DataKeyNames = new string[] { this._PriceDealProductBanTable.PriceDealEntityProductItemIdColumn.ColumnName };
            this._RadGrid1.MasterTableView.ClientDataKeyNames = new string[] { this._PriceDealProductBanTable.PriceDealEntityProductItemIdColumn.ColumnName };                                            
            this._RadGrid1.EnableLinqExpressions = false;         
 
            this._RadGrid1.MasterTableView.NoMasterRecordsText = "No Products found.";
            this._RadGrid1.MasterTableView.Width = Unit.Percentage(100);
             
            this._RadGrid1.ClientSettings.Selecting.AllowRowSelect = true;
            this._RadGrid1.AllowMultiRowSelection = true;
             
             
            this._RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
            this._RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
 
            GridBoundColumn boundColumn = new GridBoundColumn();
            string columnName;
            
            #region RadGrid Columns
            GridClientSelectColumn selectAll = new GridClientSelectColumn();
            selectAll.UniqueName = "SelectOne";
            this._RadGrid1.MasterTableView.Columns.Add(selectAll);
//Other columns
            templateColumn = new GridTemplateColumn();
            templateColumnName = this._PriceDealProductBanTable.BillingAccountNumberColumn.ColumnName;
            this._RadGrid1.MasterTableView.Columns.Add(templateColumn);
            templateColumn.DataField = templateColumnName;
            templateColumn.ItemTemplate = new TextBoxTemplate(templateColumnName);
            templateColumn.HeaderText = "BAN";
            templateColumn.UniqueName = "BAN";           
            templateColumn.AllowFiltering = false;           

The textboxtemplate is as follows:
public class TextBoxTemplate : ITemplate
    {
        protected RadTextBox _textBox;
        string _columnName;
        public TextBoxTemplate(string columnName)
        {
            this._columnName = columnName;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
 
            this._textBox = new RadTextBox();
            this._textBox.ID = this._columnName;
            container.Controls.Add(this._textBox);
            this._textBox.DataBinding += new EventHandler(_textBox_DataBinding);
        }
        void _textBox_DataBinding(object sender, EventArgs e)
        {
            RadTextBox txt = (RadTextBox)sender;
            GridDataItem container = (GridDataItem)txt.NamingContainer;
            txt.Text = ((DataRowView)container.DataItem)[this._columnName].ToString();
        }
    }

When I click on the textbox, I want the row to be automatically selected. How can I achieve this?
Also after selections are made, and i click on a row, all selections are disabled. I want to change this behaviour also. Deselect only happens when I click on the checkbox.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Jul 2014, 03:45 AM
Hi,

You can attach an onfocus event to the TextBox and set the row selected and to select row using checkbox set the ClientSettings property UseClientSelectColumnOnly to true.

C#:
_RadGrid1.ClientSettings.Selecting.AllowRowSelect = true;
_RadGrid1.ClientSettings.Selecting.UseClientSelectColumnOnly = true;
_RadGrid1.ItemCreated += new GridItemEventHandler(_RadGrid1_ItemCreated);
 
void _RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
   GridDataItem dataItem = (GridDataItem)e.Item;
   RadTextBox txtSelect = (RadTextBox)dataItem.FindControl("TextBoxID");// pass the TextBox ID
   txtSelect.Attributes.Add("onfocus", "SelectRow('"+dataItem.ItemIndex+"','"+_RadGrid1.ClientID+"')");
  }
}

JS:
<script type="text/javascript">
  function SelectRow(index,grid) {
    var grid = $find(grid);
    var MasterTable = grid.get_masterTableView();
    MasterTable.get_dataItems()[index].set_selected("true");
   }
</script>

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