More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

How to allow a user to type a new value into a DataGridView ComboBox Options · View
hong
Posted: Saturday, August 06, 2011 8:24:38 AM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 335
Points: 711
Location: Australia
AS mentioned in How to Create Editable ComboBox in DataGridView, there is no property to enable this for a DataGridView ComboBox. You need to handle the EditingControlShowing event in order to change the DropDownStyle of the ComboBox control to DropDown. Then in the CellValidating event, add the FormattedValue value to the ComboBox list if it doesn't already exist. Note that this method only works when when the DataSource property is not set. Otherwise it will fire the exception"Items collection cannot be modified when the DataSource property is set."

Code:
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
            {
                DataGridViewComboBoxEditingControl combo = e.Control as DataGridViewComboBoxEditingControl;
                combo.DropDownStyle = ComboBoxStyle.DropDown;
                combo.TextChanged += new EventHandler(combo_TextChanged);
            }
        }



Code:
        void combo _TextChanged(object sender, EventArgs e)
        {
            this.dataGridView1.NotifyCurrentCellDirty(true);
        }



Code:
        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == myColumn.Index)
            {
                object eFormattedValue = e.FormattedValue;
                if (!myColumn.Items.Contains(eFormattedValue))
                {
                    myColumn.Items.Add(eFormattedValue);
                }
            }
       }



Sponsor
Posted: Saturday, August 06, 2011 8:24:38 AM
rinomasari
Posted: Saturday, April 21, 2012 4:25:20 AM
Rank: Newbie
Groups: Member

Joined: 4/21/2012
Posts: 1
Points: 3
follow the link it will explain how datagridview

http://csharp.net-informations.com/datagridview/csharp-datagridview-tutorial.htm

masari.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.059 seconds.