资源简介

C# winform datagridview 里的下拉框中 既可以从下拉框里选择数据,也可以让客户自己手动往里输入数据,

资源截图

代码片段和文件信息

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;

namespace WinFormAPP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindComboxItems();
            this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(EnabledComboBoxWrite);
        }
        private void EnabledComboBoxWrite(object sender DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                cb.DropDownstyle = ComboBoxstyle.DropDown;
                cb.Validating += new System.ComponentModel.CancelEventHandler(cb_Validating);
            }

        }

        void cb_Validating(object sender System.ComponentModel.CancelEventArgs e)
        {
            DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
            if (cbo.Text.Trim() == string.Empty) return;

            DataGridView grid = cbo.EditingControlDataGridView;
            object value = cbo.Text;

            if (cbo.Items.IndexOf(value) == -1)
            {
                DataGridViewComboBoxColumn cboCol = (DataGridViewComboBoxColumn)grid.Columns[grid.CurrentCell.ColumnIndex];
                cbo.Items.Add(value);
                cboCol.Items.Add(value);
                grid.CurrentCell.Value = value;
            }
        }
        private void BindComboxItems()
        {
            DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dataGridView1.Columns[0];
            col.Items.Add(““);
            col.Items.Add(“aaa“);
            col.Items.Add(“bbb“);
            col.Items.Add(“ccc“);
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-12-13 13:19  WinFormAPP\
     目录           0  2013-12-13 13:20  WinFormAPP\WinFormAPP\
     文件         872  2013-12-13 13:19  WinFormAPP\WinFormAPP.sln
     文件       18944  2013-12-24 16:35  WinFormAPP\WinFormAPP.suo
     目录           0  2013-12-13 13:19  WinFormAPP\WinFormAPP\bin\
     目录           0  2013-12-17 17:21  WinFormAPP\WinFormAPP\bin\Debug\
     文件        9728  2013-12-24 16:34  WinFormAPP\WinFormAPP\bin\Debug\WinFormAPP.exe
     文件       26112  2013-12-24 16:34  WinFormAPP\WinFormAPP\bin\Debug\WinFormAPP.pdb
     文件       11600  2013-12-24 16:35  WinFormAPP\WinFormAPP\bin\Debug\WinFormAPP.vshost.exe
     文件         490  2010-03-17 22:39  WinFormAPP\WinFormAPP\bin\Debug\WinFormAPP.vshost.exe.manifest
     文件        1918  2013-12-24 16:34  WinFormAPP\WinFormAPP\Form1.cs
     文件        2788  2013-12-24 16:23  WinFormAPP\WinFormAPP\Form1.Designer.cs
     文件        6001  2013-12-24 16:23  WinFormAPP\WinFormAPP\Form1.resx
     目录           0  2013-12-13 13:19  WinFormAPP\WinFormAPP\obj\
     目录           0  2013-12-13 13:19  WinFormAPP\WinFormAPP\obj\x86\
     目录           0  2013-12-24 16:34  WinFormAPP\WinFormAPP\obj\x86\Debug\
     文件        4440  2013-12-17 17:20  WinFormAPP\WinFormAPP\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6323  2013-12-24 16:34  WinFormAPP\WinFormAPP\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         352  2013-12-24 16:23  WinFormAPP\WinFormAPP\obj\x86\Debug\GenerateResource.read.1.tlog
     文件         814  2013-12-24 16:23  WinFormAPP\WinFormAPP\obj\x86\Debug\GenerateResource.write.1.tlog
     文件        7897  2013-12-17 17:21  WinFormAPP\WinFormAPP\obj\x86\Debug\ResolveAssemblyReference.cache
     目录           0  2013-12-13 13:19  WinFormAPP\WinFormAPP\obj\x86\Debug\TempPE\
     文件        1021  2013-12-24 16:35  WinFormAPP\WinFormAPP\obj\x86\Debug\WinFormAPP.csproj.FileListAbsolute.txt
     文件        9728  2013-12-24 16:34  WinFormAPP\WinFormAPP\obj\x86\Debug\WinFormAPP.exe
     文件         180  2013-12-24 16:23  WinFormAPP\WinFormAPP\obj\x86\Debug\WinFormAPP.Form1.resources
     文件       26112  2013-12-24 16:34  WinFormAPP\WinFormAPP\obj\x86\Debug\WinFormAPP.pdb
     文件         180  2013-12-17 17:21  WinFormAPP\WinFormAPP\obj\x86\Debug\WinFormAPP.Properties.Resources.resources
     文件         491  2013-12-13 13:19  WinFormAPP\WinFormAPP\Program.cs
     目录           0  2013-12-13 13:19  WinFormAPP\WinFormAPP\Properties\
     文件        1356  2013-12-13 13:19  WinFormAPP\WinFormAPP\Properties\AssemblyInfo.cs
     文件        2871  2013-12-13 13:19  WinFormAPP\WinFormAPP\Properties\Resources.Designer.cs
............此处省略4个文件信息

评论

共有 条评论