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

gridview contextmenu Clear Value

21 Answers 148 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Somnath
Top achievements
Rank 1
Somnath asked on 08 Feb 2011, 03:29 PM
HI

Please let me know any way to display ONLY 'Clear value' item , when right click on the radgridview cell.

Thank You

21 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 03:47 PM
Hello Somnath,

This will leave only the Clear Value option for you.

this.radGridView1.ContextMenuOpening += new Telerik.WinControls.UI.ContextMenuOpeningEventHandler(this.radGridView1_ContextMenuOpening);
private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    RadDropDownMenu menu = new RadDropDownMenu();
    for (int i = 0; i <= e.ContextMenu.Items.Count-1; i++)
    {
        if (string.Equals("Clear Value", e.ContextMenu.Items[i].Text))
        {
            menu.Items.Add(e.ContextMenu.Items[i]);
            break;
        }
    }
    e.ContextMenu = menu;
}

hope that helps
Richard
0
Somnath
Top achievements
Rank 1
answered on 08 Feb 2011, 04:21 PM
Hello,

Its really Helps me..
Thank You
0
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 04:25 PM
You're welcome Somnath. Glad I could help
Richard
0
Somnath
Top achievements
Rank 1
answered on 23 Feb 2011, 12:39 PM

HI

I have two GridviewdecimalColumn in a Radgridview which are 'Debit'  & 'Credit'...
I want following case---
When user Insert Debit value then Credit value of that row should Be 0
or  When user Insert Credit value then Debit value of that row should Be 0

For that I have written Following Code------

But its not work Properly.....

private void JournalEntryDataGridView_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
 if(e.ColumnIndex == 2) //Debit Column
            {                
                   e.Row.Cells["Credit"].Value = 0;                 
            }
 if(e.ColumnIndex == 3) //Credit Column
            {                
                   e.Row.Cells["Debit"].Value = 0;                 
            }
  
}

Let me know any Solution on this Case....
Thank You

0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 01:31 PM
Hello Somnath,

From what I understand you wish to change the value of one cell based on the input of another. Is that correct?

If so, do you wish this to happen for inserting and updating, or one of these only?
Thanks
Richard
0
Somnath
Top achievements
Rank 1
answered on 23 Feb 2011, 01:36 PM
Hello

yes,I wish to change the value of one cell based on the input of another
And I wish this to happen for both inserting and updating.....
.....
Waiting for your reply

Thank You
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 02:37 PM
Hello Somnath,

i believe that this should do what you want

private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "Debit"
    {
        if (Convert.ToInt32(e.Value) > 0)
        
            e.Row.Cells["Credit"].Value = 0;
        }
    }
    else  if (e.Column.Name == "Credit"
    {
        if (Convert.ToInt32(e.Value) > 0)
        {
            e.Row.Cells["Debit"].Value = 0;
        }
    
}

Let me know if there are any issues
Richard
0
Somnath
Top achievements
Rank 1
answered on 23 Feb 2011, 02:45 PM
Hello Richard

It works for me...
Thank You
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 02:48 PM
Glad I could help, Somnath.
Regards
Richard
0
Somnath
Top achievements
Rank 1
answered on 23 Feb 2011, 04:18 PM

Hello Richard

I have another issue as follows---

I have a Gridview Having two ComboBoxcolumns 'Name' and 'Detail', both the columns have diffrent Binding Source
When user change the 'Name' value as soon as the 'Detail' value of the same row Should be null and text will be empty..

waiting for your reply

Thank You

0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 04:22 PM
Hello Somnath,

Before I look at this for you, please can I confirm that I understand your requirement.
You have a radGridView with 2 GridViewComboBox Columns which are called name and detail

Each one of these is bound to a different data source

When you change the value in the Name drop down, you want the value of the Detail cell on the same row to be null, and no value selected.

Is that correct?
Thanks
Richard
0
Somnath
Top achievements
Rank 1
answered on 24 Feb 2011, 08:22 AM
Hi Richard

 Yes,Exactly Correct...

When User change the value in the Name drop down, he want the value of the Detail cell on the same row to be null, no value selected AND TEXT SHOULD BE EMPTY.

Thank You
0
Richard Slade
Top achievements
Rank 2
answered on 24 Feb 2011, 10:39 AM
Hi Somnath,

You just need to handle the CellValueChangedEvent. For exmaple

private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "TheName")
    {
        e.Row.Cells["TheDetails"].Value = null;
    }
}

hope that helps
Richard
0
Somnath
Top achievements
Rank 1
answered on 24 Feb 2011, 11:17 AM

Hello Richard

I already used this code but its not working properly....
I want when user change 'Name' dropdown item then 'Detail'  text of the same row immediately become Empty (none)....

Hope you get exact issue
Thank you
0
Richard Slade
Top achievements
Rank 2
answered on 24 Feb 2011, 11:31 AM
Hi Somnath,

I can look at that for you if you wish, but my advice would be to stick with the method I proposed. The reason for this is that if the user changes the name in the drop down, and then changes it back again whilst still in edit mode, then you have already lost the value in the detail column before the name value has actually changed.

In other words, once you have picked a different name from the drop down before the value is commited, then the value in the detail field has already been changed and commited.
Regards,
Richard
0
Somnath
Top achievements
Rank 1
answered on 24 Feb 2011, 12:08 PM
Hello
 Its Correct ,what you write here...
But my requirement is different from what you understand.....

So,Please provide me solution as per I said earlier....
Thanks You
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 24 Feb 2011, 12:28 PM
Hello Somnath,

This should do what you need,

namespace RadControlsWinFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(284, 262);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.CellEndEdit += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEndEdit);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

Form1.cs
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;
  
namespace RadControlsWinFormsApp1
{
    public partial class Form1 : Form
    {
        List<Name> names = new List<Name>();
        List<Detail> details = new List<Detail>();
        List<Person> people = new List<Person>();
        RadDropDownListEditorElement m_editorelement;
  
        public Form1()
        {
            InitializeComponent();
            this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(radGridView1_CellEditorInitialized);
              
  
            this.radGridView1.AutoGenerateColumns = false;
  
            names.Add(new Name(1, "Richard"));
            names.Add(new Name(2, "Somnath"));
  
            details.Add(new Detail(1, "Detail 1"));
            details.Add(new Detail(2, "Detail 2"));
  
            people.Add(new Person(1, 1));
            people.Add(new Person(2, 2));
  
            GridViewComboBoxColumn nameColumn = new GridViewComboBoxColumn();
            nameColumn.FieldName = "NameId";
            this.radGridView1.Columns.Add(nameColumn);
            nameColumn.DataSource = names;
            nameColumn.Name = "TheName";
            nameColumn.DisplayMember = "TheName";
            nameColumn.ValueMember = "Id";
  
            GridViewComboBoxColumn detailColumn = new GridViewComboBoxColumn();
            detailColumn.FieldName = "DetailId";
            this.radGridView1.Columns.Add(detailColumn);
            detailColumn.DataSource = details;
            detailColumn.Name = "TheDetails";
            detailColumn.DisplayMember = "DetailName";
            detailColumn.ValueMember = "Id";
  
            this.radGridView1.DataSource = people;
        }
  
        void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            if (e.Column.Name == "TheName")
            {
                  RadDropDownListEditor editor = (RadDropDownListEditor)this.radGridView1.ActiveEditor;
                  RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
                  m_editorelement = editorElement;
                    
                  m_editorelement.SelectedIndexChanged += new Telerik.WinControls.UI.Data.PositionChangedEventHandler(m_editorelement_SelectedIndexChanged);
            }
        }
  
        void m_editorelement_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (m_editorelement.SelectedValue != null && m_editorelement.SelectedValue != this.radGridView1.CurrentCell.Value && this.radGridView1.CurrentRow.Index > -1)
            
                this.radGridView1.CurrentRow.Cells["TheDetails"].Value = null;
            }
        }
  
  
        private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            if (m_editorelement != null)
            {
                m_editorelement.SelectedIndexChanged -= new Telerik.WinControls.UI.Data.PositionChangedEventHandler(m_editorelement_SelectedIndexChanged);
            }
        }
    }
  
    public class Person
    {
        public Person(int nameId, int detailId)
        {
            this.NameId = nameId; this.DetailId = detailId;
        }
  
        public int NameId
        { get; set; }
  
        public int DetailId
        { get; set; }
    }
  
    public class Detail
    {
        public Detail(int id, string detailName)
        {
            this.Id = id; this.DetailName = detailName;
        }
  
        public int Id
        {get; set;}
  
        public string DetailName
        {get; set;}
    }
  
    public class Name
    {
        public Name(int id, string theName)
        {
            this.Id = id; this.TheName = theName;
        }
  
        public int Id
        {get; set;}
  
        public string TheName
        {get; set;}
  
    }
}

Hope that helps
Richard
0
Somnath
Top achievements
Rank 1
answered on 24 Feb 2011, 03:23 PM
Hello

Thank for your reply
It closer to my requirement but  text of Detail field of the row still apper...

0
Richard Slade
Top achievements
Rank 2
answered on 24 Feb 2011, 03:28 PM
Hello Somnath,

I'm not sure I understand. Either that or we are seeing different results. I have added this link to a video so you can see it working on my PC here.
When the name is changed, the detail cell is cleared and the value is set to null. Once you have changed the name, you have to re-pick the detail.
Richard
0
Somnath
Top achievements
Rank 1
answered on 24 Feb 2011, 04:05 PM
Hello Richard

With few changes ,the code provided by You Works Fine for me

Thanks a lot
Have a nice day....
 
0
Richard Slade
Top achievements
Rank 2
answered on 24 Feb 2011, 04:08 PM
Glad to have been able to help Somnath.
Please remember to mark as answer and if you have a moment, I'd be interested to know what you needed to change to complete your requirement.
Thanks
Richard
Tags
GridView
Asked by
Somnath
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Somnath
Top achievements
Rank 1
Share this question
or