在组合框中嵌入一个DataGridView

介绍 GridComboBox的c#实现 背景 这个项目是基于  http://www.codeproject.com/articles/492662/embeding-a-datagridview-in-a-combobox,其代码是在VB , 使用的代码 这个项目的输出是一个.dll文件。必须将此文件附加到项目中,并像Windows窗体的控件一样使用 请记住: 你必须在你的项目

  

 

中添加一个简单的DataGridView,这个元素将被GridComboBox控制,特别是可见性状态。 这是一个小简短如何使用这个组件在你的项目: 隐藏,收缩,复制Code

using System;
using TemporalAPP.GraphicSourceDataSetTableAdapters;
using System.Data;
using System.Windows.Forms;
using static TemporalAPP.GraphicSourceDataSet;
using GridComboBox;

namespace TemporalAPP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ArticuloArancelDataTable tabla = new ArticuloArancelDataTable();
            new ArticuloArancelTableAdapter().Fill(tabla);
            dataGridView1.DataSource = tabla; // whatever datasource of DataGridView
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.Columns[0].Width = 1;
            accGridComboBox1.AddDataGridView(dataGridView1, false); // SO IMPORTANT!
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            dataGridView1.Visible = true;
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (accGridComboBox1.SelectedValue == null) return;
            accGridComboBox1.Text = ((DataRowView)accGridComboBox1.SelectedValue).Row["Codigo"].ToString();
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Return || e.KeyCode == Keys.Enter)
            {
                if (accGridComboBox1.SelectedValue == null) return;
                accGridComboBox1.Text = ((DataRowView)accGridComboBox1.SelectedValue).Row["Codigo"].ToString();
            }
        }
    }

, 重要提示: DataGridView的默认可见状态为true,所以你必须双击GridComboBox控件,直到它缩小;之后你就可以使用它了。我认为这是一个问题。 第一次运行: 初始双击后: 选择项目后: , 历史 我想如果有人可以帮助DataGridView的初始状态。在我看来,用visibility=false会更好;避免初始双击。 本文转载于:http://www.diyabc.com/frontweb/news278.html

posted @ 2020-08-05 02:44  Dincat  阅读(151)  评论(0编辑  收藏  举报