65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ComboBox 中嵌入 DataGridView

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2016年6月3日

CPOL
viewsIcon

14049

downloadIcon

17

这是“在 ComboBox 中嵌入 DataGridView”的替代方案

背景

该项目基于文章 在 ComboBox 中嵌入 DataGridView,其代码为 VB。

Using the Code

该项目的输出是一个 .dll 文件。您必须将此文件附加到您的项目中,并将其用作 Windows Forms 的控件。

请注意

  • 您需要在您的项目中添加一个简单的 DataGridView,该元素将由 GridComboBox 控制,特别是可见性状态。

这是在您的项目中如何使用此组件的简要说明

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; 会更好,以避免初始双击。

历史

  • 2016年6月3日:初始版本
© . All rights reserved.