DataGrid中如何加入Combox

主  题:  DataGrid中如何加入Combox?
作  者:  HungryBoy (饥饿的男孩)
等  级:  ^
信 誉 值:  99
所属论坛:  .NET技术 C#
问题点数:  20
回复次数:  5
发表时间:  2003-9-26 14:42:38

CSDN上我搜索过了,如果您的答案同CSDN以前的答案一样就不要回复了!!

谢谢!!



回复人: brightheroes(太菜了,请原谅) ( 五级(中级)) 信誉:100 2003-9-26 14:47:01 得分:0

切,上google搜

回复人: HungryBoy(饥饿的男孩) ( 一级(初级)) 信誉:99 2003-9-26 14:53:57 得分:0

没搜到!!

回复人: HungryBoy(饥饿的男孩) ( 一级(初级)) 信誉:99 2003-9-26 15:02:48 得分:0

早就搜索过了!没找到答案!

回复人: houlinghouling(新玲) ( 一级(初级)) 信誉:100 2003-9-26 15:04:54 得分:2

用datagrid中的模版列来实现

回复人: luoqing(明天将会...) ( 五级(中级)) 信誉:90 2003-9-26 15:05:01 得分:18

using System.Drawing;

public delegate void ComboValueChanged(int changingRow, object newValue);

// Step 1. Derive a custom column style from DataGridTextBoxColumn
//  a) add a ComboBox member
//  b) track when the combobox has focus in Enter and Leave events
//  c) override Edit to allow the ComboBox to replace the TextBox
//  d) override Commit to save the changed data
public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
    public NoKeyUpCombo ColumnComboBox = null;
    private System.Windows.Forms.CurrencyManager _source = null;
    private int _rowNum;
    private bool _isEditing = false;
    ComboValueChanged _valueChanging;

    public DataGridComboBoxColumn(ComboValueChanged valueChanging) : base()
    {
        _valueChanging = valueChanging;
        ColumnComboBox = new NoKeyUpCombo();

        ColumnComboBox.Leave += new EventHandler(LeaveComboBox);
        // ColumnComboBox.Enter += new EventHandler(ComboMadeCurrent);
        ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);
        ColumnComboBox.SelectionChangeCommitted += new System.EventHandler(ComboStartEditing);
    }

    private void ComboStartEditing(object sender, EventArgs e)
    {
        _isEditing = true;
        base.ColumnStartedEditing((Control)sender);
    }

    private void ComboIndexChanged(object sender, EventArgs e)
    {
        _valueChanging(_rowNum, ColumnComboBox.Text);
    }

    // private void ComboMadeCurrent(object sender, EventArgs e)
    // {
    //          //_isEditing = true;
    // }

    private void LeaveComboBox(object sender, EventArgs e)
    {
        if (_isEditing)
        {
            SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text);
            _isEditing = false;
            Invalidate();
        }
        ColumnComboBox.Hide();
    }

    protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
    {
        base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);

        _rowNum = rowNum;
        _source = source;

        ColumnComboBox.Parent = this.TextBox.Parent;
        ColumnComboBox.Location = this.TextBox.Location;
        ColumnComboBox.Size = new Size(this.TextBox.Size.Width, ColumnComboBox.Size.Height);
        ColumnComboBox.SelectedIndexChanged -= new System.EventHandler(ComboIndexChanged);
        ColumnComboBox.Text = this.TextBox.Text;
        ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);

        this.TextBox.Visible = false;
        ColumnComboBox.Visible = true;
        ColumnComboBox.BringToFront();
        ColumnComboBox.Focus();
    }

    protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
    {
        if (_isEditing)
        {
            _isEditing = false;
            SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);
        }
        return true;
    }
}

public class NoKeyUpCombo : ComboBox
{
    const int WM_KEYUP = 0x101;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (m.Msg == WM_KEYUP)
        {
            //ignore keyup to avoid problem with tabbing & dropdownlist;
            return;
        }
        base.WndProc(ref m);
    }
}

回复人: HungryBoy(饥饿的男孩) ( 一级(初级)) 信誉:99 2003-9-26 15:17:19 得分:0

谢谢楼上的!

怎么这么复杂啊!我好像没看懂啊!

该问题已经结贴 ,得分记录: houlinghouling (2)、 luoqing (18)、

Contributors: FHL