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

GridViewComboBoxColumn select change event

14 Answers 1031 Views
GridView
This is a migrated thread and some comments may be shown as answers.
anu
Top achievements
Rank 1
anu asked on 28 Apr 2011, 09:47 PM
I have a RadGridview databound to custom object list. it has only one field which is GridviewComboBoxColumn . If i change a value in combobox it waits for the me to press tab or click somewhere before it adds that row to the grid and show the add next row field. How can do this as soon as the selection is made on the GridViewComboBoxColumn  and not necessarily to move away from it?

thanks

Anu

14 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 11:46 AM
Hello anu,

In my point of view this is not very user friendly because if you make a mistake you will have already added that row and in order to modify it you will have to select that row and change that value, but if you want it to behave like this, you can easily handle the selection changed event of the editor, like so:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AllowAddNewRow = true;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
        var list = new List<Test>
        {
            new Test{Id = 1, Name = "Name1"},
            new Test{Id = 2, Name = "Name2"},
            new Test{Id = 3, Name = "Name3"},
        };
 
        var dropDownColumn = new GridViewComboBoxColumn();
        dropDownColumn.DataSource = list;
        dropDownColumn.DisplayMember = "Name";
        dropDownColumn.ValueMember = "Id";
        radGridView1.Columns.Add(dropDownColumn);
    }
 
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        if (e.Row is GridViewNewRowInfo)
        {
            var editor = e.ActiveEditor as RadDropDownListEditor;
            if (editor != null)
            {
                editor.ValueChanged -= new EventHandler(editor_ValueChanged);
                editor.ValueChanged += new EventHandler(editor_ValueChanged);
            }
        }
    }
 
    void editor_ValueChanged(object sender, EventArgs e)
    {
        radGridView1.EndEdit();
    }
}
 
public class Test
{
    public int Id { get; set; }
 
    public string Name { get; set; }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
anu
Top achievements
Rank 1
answered on 29 Apr 2011, 03:31 PM
Hi Emanuel ,
Thanks, that worked great.
I have one more question related to this.
if i click on the dropdown column on the grid (which put the cell in edit mode) and then if click away from the cell without making any selection from the dropdown, it keep on adding empty rows to the grid. I am not sure how to prevent this from happening.

So if i do a mix of some times selected and sometimes just clicked away i see a bunch of empty rows added
my grid look like this

Name1
Name 2
<empty row here because i started the editor but clicked away w/o selecting any name>
Name 3
Name 4
<empty row here because i started the editor but clicked away w/o selecting any name>
<empty row here because i started the editor but clicked away w/o selecting any name>
Name 10


Thanks for your help
Anu

0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 03:39 PM
Hello again,

You can adapt the ValueChanged event like so:
void editor_ValueChanged(object sender, EventArgs e)
{
    var editor = radGridView1.ActiveEditor as RadDropDownListEditor;
    if (editor != null)
    {
        var editorElement = editor.EditorElement as RadDropDownListEditorElement;
        if (editorElement.SelectedIndex == -1)
        {
            radGridView1.CancelEdit();
        }
        else
        {
            radGridView1.EndEdit();
        }
    }
}

This way you will cancel the edit operation if selected index == -1 and end the edit operation otherwise.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
anu
Top achievements
Rank 1
answered on 29 Apr 2011, 03:57 PM
looks like it is not firing editor_ValueChanged event when i do this, may be because i am not actually choosing any value

thanks
0
anu
Top achievements
Rank 1
answered on 29 Apr 2011, 04:04 PM

it cancelled the empty rows when i did this

 

 

 

 

private void radGV_UserAddingRow(object sender, GridViewRowCancelEventArgs e)

 

 

{

 

 

 

if(e.Rows[0].Cells["Id"].Value==null)

 

 

e.Cancel =

 

true;

 

 

}


Thanks

0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 04:10 PM
Hello again,

Strange because on my version it is not adding the row when you have no selection, although the UserAddingRow will work in this case,  i would suggest using the RowValidating event for this:
void radGridView1_RowValidating(object sender, RowValidatingEventArgs e)
{
    if (e.Row is GridViewNewRowInfo)
    {
        if (e.Row.Cells[0].Value == null)
        {
            e.Cancel = true;
            radGridView1.CancelEdit();
        }
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
anu
Top achievements
Rank 1
answered on 29 Apr 2011, 04:49 PM
may be 'cause i haven't upgraded to latest,i am still using Q3 2010-

anyways, i made the change you suggested

Thanks a lot for your help

Anu
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 04:51 PM
Hello again,

Glad to be able to help,

If you have any more questions please just let me know,

Best Regards,
Emanuel Varga

WinForms MVP
0
anu
Top achievements
Rank 1
answered on 29 Apr 2011, 06:18 PM
Hi Emanuel ,
I just have one more question.
I am binding a gridview to an object heirarchy data(as shown in code)- users and their assigned branches.
When i open the first user it shows his branches correctly, but when i open the second one the first one's branch list is cleared and it shows the second one correctly. So i can either never open more than one and show them correctly. I am sure it might be something one the data binding but can't figure out, can you please help


private void loadradGV_UserAdmin()
        {
  
            try
            {
                             GridViewTemplate partsTemplate = new GridViewTemplate();
                if (!_radGV_UserAdminColumnsCreated)
                {
                    _radGV_UserAdminColumnsCreated = true;
                    //manually add columns
                    radGV_UserAdmin.MasterTemplate.AutoGenerateColumns = false;
                    radGV_UserAdmin.AllowEditRow = false;
                    radGV_UserAdmin.EnableFiltering = true;
                    radGV_UserAdmin.AllowDeleteRow = false;
                    radGV_UserAdmin.AllowAddNewRow = false;
                    radGV_UserAdmin.GridBehavior = new CustomGridBehavior();//to enable copy paste from readonly columns
                    radGV_UserAdmin.AllowCellContextMenu = false;
  
  
                    GridViewTextBoxColumn colUserId = new GridViewTextBoxColumn("UserId");
                    colUserId .Width = 0;
                    colUserId .IsVisible = false;
                    colUserId .HeaderText = "UserId";
                    colUserId .ReadOnly = true;
                     radGV_UserAdmin.MasterTemplate.Columns.Add(colUserId );
  
                                        GridViewTextBoxColumn colFullName = new GridViewTextBoxColumn("Name");
                    colFullName.Width = 200;
                    colFullName.HeaderText = "User";
                    radGV_UserAdmin.MasterTemplate.Columns.Add(colFullName);
  
                                             
  
                    this.radGV_UserAdmin.GroupSummaryEvaluate += new GroupSummaryEvaluateEventHandler(radGV_userAdmin_GroupSummaryEvaluate);
  
  
                 
  
                                
                  
                
                 
  
                }
  
               //for child display
  
                this.radGV_UserAdmin.MasterTemplate.Templates.Add(partsTemplate);
  
                partsTemplate.AllowEditRow = false;
  
  
                GridViewTextBoxColumn colUserId = new GridViewTextBoxColumn("UserId");
                colUserId.Width = 0;
                colUserId.IsVisible = false;
                colUserId.HeaderText = "UserId";
                partsTemplate.Columns.Add(colUserId);
  
                GridViewComboBoxColumn colBranch = new GridViewComboBoxColumn("BrId");
                colBranch.Width = 300;
                colBranch.DropDownStyle = RadDropDownStyle.DropDown;
                colBranch.HeaderText = "Authorized Branch";
                colBranch.DataSource = GetallbranchList();
                colBranch.ValueMember = "BrId";
                colBranch.DisplayMember = "BrName";
                colBranch.FieldName = "BrId";
                partsTemplate.Columns.Add(colBranch);
  
                GridViewRelation relation = new GridViewRelation(this.radGV_UserAdmin.MasterTemplate);
                relation.ChildTemplate = partsTemplate;
                relation.RelationName = "Branches_Parts";
                relation.ParentColumnNames.Add("UserId");
                relation.ChildColumnNames.Add("UserId");
                radGV_UserAdmin.Relations.Add(relation);
  
  
  
                List<UserAdmin> userAdminList = _userAdminBL.GetUserAdminList(getFilter());
                radGV_UserAdmin.DataSource = userAdminList;
                partsTemplate.DataSource = userAdminList;
                partsTemplate.DataMember = "AssignedBranches";
  
                
  
                radGV_UserAdmin.BestFitColumns();
               
            }
            catch (Exception ex)
            {
                               ErrorHandler.HandleError(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
  
            }
            finally
            {
                            }
        }
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 06:24 PM
Hello again,

Without having your database i cannot test this, could you please create a small sample and post the entire code here so that i can help you?

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
anu
Top achievements
Rank 1
answered on 29 Apr 2011, 09:16 PM
if i click user 1 it show 4 branches
then when i click the user2 it shows 4 branches for user 2 but the user1 data is cleared.

form1.cs code: (sample with data),
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls;
  
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private bool _radGV_UserAdminColumnsCreated = false;
        private void loadradGV_UserAdmin()
        {
  
            try
            {
                GridViewTemplate partsTemplate = new GridViewTemplate();
                if (!_radGV_UserAdminColumnsCreated)
                {
                    _radGV_UserAdminColumnsCreated = true;
                    //manually add columns 
                    radGV_UserAdmin.MasterTemplate.AutoGenerateColumns = false;
                    radGV_UserAdmin.AllowEditRow = false;
                    radGV_UserAdmin.EnableFiltering = true;
                    radGV_UserAdmin.AllowDeleteRow = false;
                    radGV_UserAdmin.AllowAddNewRow = false;
                    //radGV_UserAdmin.GridBehavior = new CustomGridBehavior();//to enable copy paste from readonly columns 
                    radGV_UserAdmin.AllowCellContextMenu = false;
  
  
                    GridViewTextBoxColumn colUserId = new GridViewTextBoxColumn("UserId");
                    colUserId.Width = 0;
                    colUserId.IsVisible = false;
                    colUserId.HeaderText = "UserId";
                    colUserId.ReadOnly = true;
                    radGV_UserAdmin.MasterTemplate.Columns.Add(colUserId);
  
                    GridViewTextBoxColumn colFullName = new GridViewTextBoxColumn("FullName");
                    colFullName.Width = 200;
                    colFullName.HeaderText = "User";
                    radGV_UserAdmin.MasterTemplate.Columns.Add(colFullName);
  
  
  
  
  
  
  
  
  
  
  
  
                }
  
                //for child display 
  
                this.radGV_UserAdmin.MasterTemplate.Templates.Add(partsTemplate);
  
                partsTemplate.AllowEditRow = false;
  
  
                GridViewTextBoxColumn colUserIdc = new GridViewTextBoxColumn("UserId");
                colUserIdc.Width = 0;
                colUserIdc.IsVisible = false;
                colUserIdc.HeaderText = "UserId";
                partsTemplate.Columns.Add(colUserIdc);
  
                GridViewComboBoxColumn colBranch = new GridViewComboBoxColumn("BusinessId");
                colBranch.Width = 300;
                colBranch.DropDownStyle = RadDropDownStyle.DropDownList;
                colBranch.HeaderText = "Authorized Branch";
                colBranch.DataSource = GetallbranchList();
                colBranch.ValueMember = "BrId";
                colBranch.DisplayMember = "BrName";
                colBranch.FieldName = "BusinessId";
                partsTemplate.Columns.Add(colBranch);
  
                GridViewRelation relation = new GridViewRelation(this.radGV_UserAdmin.MasterTemplate);
                relation.ChildTemplate = partsTemplate;
                relation.RelationName = "Branches_Parts";
                relation.ParentColumnNames.Add("UserId");
                relation.ChildColumnNames.Add("UserId");
                radGV_UserAdmin.Relations.Add(relation);
  
  
                List<UserAdmin> userAdminList = GetUserList();
                radGV_UserAdmin.DataSource = userAdminList;
                partsTemplate.DataSource = userAdminList;
                partsTemplate.DataMember = "AssignedBranches";
  
  
  
                radGV_UserAdmin.BestFitColumns();
  
             
  
            }
            catch (Exception ex)
            {
  
            }
            finally
            {
            }
        }
  
        List<Branch> GetallbranchList()
        {
            List<Branch> lst = new List<Branch>();
            Branch br= new Branch();
            int max = 20;
            for (int i = 1; i <= max; i++)
            {
                br = new Branch();
                br.BrId = i;
                br.BrName = "br"+i.ToString();
                lst.Add(br);
            }
            return lst;
          
        }
        List<UserAdmin> GetUserList()
        {
            List<UserAdmin> lst = new List<UserAdmin>();
            List<UserAssignedBranch> Brlst = new List<UserAssignedBranch>();
  
            UserAdmin usr = new UserAdmin();
            UserAssignedBranch brAs = null;
            int set = 3;
            for (int i = 1; i <= 10; i++)
            {
                usr = new UserAdmin();
                usr.UserId = i;
                usr.FullName = "UsrName:" + i.ToString();
                Brlst = new List<UserAssignedBranch>();
                if(i==1)
                for (int j=10;j>=7;j--)
                {
                    brAs = new UserAssignedBranch();
                    brAs.BusinessId = j;
                    brAs.UserId = i;
                    Brlst.Add(brAs);
                }
                if (i == 2)
                {
                    Brlst = new List<UserAssignedBranch>();
                    for (int j = 8; j >= 5; j--)
                    {
                        brAs = new UserAssignedBranch();
                        brAs.UserId = i;
                        brAs.BusinessId = j;
                        Brlst.Add(brAs);
  
                    }
                }
                if (i == 3)
                {
                    Brlst = new List<UserAssignedBranch>();
                    for (int j = 4; j >= 1; j--)
                    {
                        brAs = new UserAssignedBranch();
                        brAs.UserId = i;
                        brAs.BusinessId = j;
                        Brlst.Add(brAs);
  
                    }
                }
  
                    usr.AssignedBranches = Brlst;
                lst.Add(usr);
            }
            return lst;
  
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
  
            loadradGV_UserAdmin();
        }
    }
  
    public class Branch{
      
        public int BrId {get;set;}
        public string BrName{get;set;}
    }
    public class UserAdmin
    {
  
        
  
        public int? UserId {get;set;}
         
  
         
  
        public string FullName {get;set;}
  
        
        private List<UserAssignedBranch> _AssignedBranches;
  
        public List<UserAssignedBranch> AssignedBranches{get;set;}
         
    }
  
    public class UserAssignedBranch
    {
  
        public int? UserId {get;set;}
       
  
       
  
        public int? BusinessId { get; set; }
         
  
    }
}
0
Svett
Telerik team
answered on 04 May 2011, 09:22 AM
Hello Anu,

In your case, you should use object-relation hierarchy. You can read more about that in the online documentation. Also, you can use the following code snippet:

private void loadradGV_UserAdmin()
{
 
    try
    {
        List<UserAdmin> userAdminList = GetUserList();
        radGV_UserAdmin.AutoGenerateHierarchy = true;
        radGV_UserAdmin.AutoGenerateColumns = false;
        radGV_UserAdmin.DataSource = userAdminList;
 
        GridViewTemplate partsTemplate = radGV_UserAdmin.MasterTemplate.Templates[0];
        partsTemplate.Columns.Clear();
 
        if (!_radGV_UserAdminColumnsCreated)
        {
            _radGV_UserAdminColumnsCreated = true;
            //manually add columns
            radGV_UserAdmin.MasterTemplate.AutoGenerateColumns = false;
            radGV_UserAdmin.AllowEditRow = false;
            radGV_UserAdmin.EnableFiltering = true;
            radGV_UserAdmin.AllowDeleteRow = false;
            radGV_UserAdmin.AllowAddNewRow = false;
            //radGV_UserAdmin.GridBehavior = new CustomGridBehavior();//to enable copy paste from readonly columns
            radGV_UserAdmin.AllowCellContextMenu = false;
 
 
            GridViewTextBoxColumn colUserId = new GridViewTextBoxColumn("UserId");
            colUserId.Width = 0;
            colUserId.IsVisible = false;
            colUserId.HeaderText = "UserId";
            colUserId.ReadOnly = true;
            radGV_UserAdmin.MasterTemplate.Columns.Add(colUserId);
 
            GridViewTextBoxColumn colFullName = new GridViewTextBoxColumn("FullName");
            colFullName.Width = 200;
            colFullName.HeaderText = "User";
            radGV_UserAdmin.MasterTemplate.Columns.Add(colFullName);
        }
 
        //for child display
        partsTemplate.AllowEditRow = false;
 
        GridViewComboBoxColumn colBranch = new GridViewComboBoxColumn("BusinessId");
        colBranch.Width = 300;
        colBranch.DropDownStyle = RadDropDownStyle.DropDownList;
        colBranch.HeaderText = "Authorized Branch";
        colBranch.DataSource = GetallbranchList();
        colBranch.ValueMember = "BrId";
        colBranch.DisplayMember = "BrName";
        colBranch.FieldName = "BusinessId";
        partsTemplate.Columns.Add(colBranch);
 
        radGV_UserAdmin.BestFitColumns();
    }
    catch (Exception ex)
    {
 
    }
    finally
    {
    }
}

I hope this helps.

Kind regards,
Svett
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
0
anu
Top achievements
Rank 1
answered on 09 May 2011, 11:11 PM
Hi Svett,
it worked; thanks for your help.
I have one other question, is it possible to add custom text to the add new box on the second level (Heirarchylevel2) like "Add new row" and also a button next to it to click and fire an event to save the change?(like the attachment)
Thanks
0
Svett
Telerik team
answered on 12 May 2011, 07:41 AM
Hi Anu,

You need to create a custom row/cell element. You can read more about customizing cells in this documentation article.

Regards,
Svett
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
anu
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
anu
Top achievements
Rank 1
Svett
Telerik team
Share this question
or