当发生DataGrid的Scroll事件的时候你让你的ComboBox隐藏就可以了,事件执行完以后再让他到具体的位置显示

解决方案 »

  1.   

    to storm97(风暴不再) 怎样执行你的方法?
      

  2.   

    seehttp://support.microsoft.com/?id=323167orhttp://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx
      

  3.   

    1.重写comboxGridstyle从gridtextstyle继承
    2。try grid update
      

  4.   

    gride可以嵌入很多
    combox
    numberupdown等等
      

  5.   

    前几天写了一段代码,你可以试试
    Public Class ComboBoxColumn
        Inherits DataGridColumnStyle    Private cboSelect As ComboBox
        Private isEditing As Boolean    Public Sub New()
            cboSelect = New ComboBox
            cboSelect.Visible = False
        End Sub    Public Sub New(ByVal cbobox As ComboBox)
            cboSelect = cbobox
            cboSelect.Visible = False
        End Sub    Protected Overrides Sub Abort(ByVal rowNum As Integer)
            isEditing = False
            RemoveHandler cboSelect.SelectedValueChanged, AddressOf ComboxValueChanged
            Invalidate()
        End Sub    Protected Overrides Function Commit _
        (ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) _
        As Boolean
            cboSelect.Bounds = Rectangle.Empty        AddHandler cboSelect.SelectedValueChanged, AddressOf ComboxValueChanged        If Not isEditing Then
                Return True
            End If
            isEditing = False        Try
                Dim value As String = cboSelect.SelectedItem            SetColumnValueAtRow(dataSource, rowNum, value)
            Catch
                MessageBox.Show("Error", "")
            End Try        Invalidate()
            Return True
        End Function    Protected Overloads Overrides Sub Edit( _
        ByVal [source] As CurrencyManager, _
        ByVal rowNum As Integer, _
        ByVal bounds As Rectangle, _
        ByVal [readOnly] As Boolean, _
        ByVal instantText As String, _
        ByVal cellIsVisible As Boolean)        Dim value As String = GetColumnValueAtRow([source], rowNum)        If cellIsVisible Then
                cboSelect.Bounds = New Rectangle _
                (bounds.X, bounds.Y, bounds.Width, bounds.Height)            '
                'cboSelect.SelectedText = value            cboSelect.Text = value
                cboSelect.Visible = True
                AddHandler cboSelect.SelectedValueChanged, AddressOf ComboxValueChanged
            Else
                cboSelect.SelectedValue = value
                cboSelect.Visible = False
            End If        If cboSelect.Visible Then
                DataGridTableStyle.DataGrid.Invalidate(bounds)
            End If
        End Sub    Protected Overrides Function GetPreferredSize( _
        ByVal g As Graphics, _
        ByVal value As Object) As Size        Return New Size(100, cboSelect.PreferredHeight)    End Function    Protected Overrides Function GetMinimumHeight() As Integer        Return cboSelect.PreferredHeight    End Function
        Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer        Return cboSelect.PreferredHeight    End Function
        Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer)
            Paint(g, bounds, [source], rowNum, False)
        End Sub    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
            Paint(g, bounds, [source], rowNum, Brushes.Red, Brushes.Blue, alignToRight)
        End Sub    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
            Dim [date] As String = GetColumnValueAtRow([source], rowNum)
            Dim rect As Rectangle = bounds
            g.FillRectangle(backBrush, rect)
            rect.Offset(0, 2)
            rect.Height -= 2
            g.DrawString([date], Me.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))
        End Sub    Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
            MyBase.SetDataGridInColumn(value)
            If Not (cboSelect.Parent Is Nothing) Then
                cboSelect.Parent.Controls.Remove(cboSelect)
            End If
            If Not (value Is Nothing) Then
                value.Controls.Add(cboSelect)
            End If
        End Sub    Private Sub ComboxValueChanged(ByVal sender As Object, ByVal e As EventArgs)
            Me.isEditing = True        MyBase.ColumnStartedEditing(cboSelect)
        End Sub    
        Protected Overrides Function GetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object
            Dim objReturn As Object = MyBase.GetColumnValueAtRow(source, rowNum)
            If IsNothing(objReturn) Then
                Return ""
            Else
                Return objReturn.ToString
            End If
        End Function
      

  6.   

    我有一个往DataGrid里面加Combox的例子可以发给你的,你把有想给我,你可以看一下不过和上面一的差不多,如果你按上面的代码,估计可以做到,但是要做成控件现在没有时间,等等载说了
      

  7.   

    you look:
    http://www.codeproject.com/useritems/datagrid_combobox.aspand saucer():
    http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx
      

  8.   

    我来搭个便车:我用一个控件(由textBox+Button组成)如果单独在FORM中使用,一切正常,但我加入到自定义的DataGridColumnStyle中之后,就出现了以下问题:1、进行编辑时,光标移动键不会进行处理!(后来我自己重写了textBox的ProcessCmdKey方法,自己进行光标键处理解决这问题了,但为什么会这样呢?FORM中就正常的呀!我不明白啊)
    2、从其他列中按TAB或SHIFT TAB进入有自定义控件的列,会跳过一列!但用左右光标键移入,或用鼠标点入的又很正常了!
    3、当有光标键将输入焦点转入有自定义控件新增行的时候会触发datagrid的leave事件!
    但如果自定义控件列在DG中是第一列的时候,又不会的!
    期待的高手的解答!
      

  9.   

    这是softbugg()的VB.NET的代码,我用工具转成C#,你试试:
    ------------------------------------- 
    前几天写了一段代码,你可以试试;
    public class ComboBoxColumn {
        : DataGridColumnStyle    private ComboBox cboSelect;
        private bool isEditing; public void new() {
            cboSelect = new ComboBox;
            cboSelect.Visible = false;
        } public void new( ComboBox cbobox) {
            cboSelect = cbobox;
            cboSelect.Visible = false;
        } protected override void Abort( int rowNum) {
            isEditing = false;
            RemoveHandler cboSelect.SelectedValueChanged, delegate(GAIS) ComboxValueChanged;
            Invalidate();
        } protected override bool Commit ( CurrencyManager dataSource,  int rowNum) {
            cboSelect.Bounds = Rectangle.Empty;        AddHandler cboSelect.SelectedValueChanged, delegate(GAIS) ComboxValueChanged;        if (! isEditing ) {
                return true;
            }
            isEditing = false;        try {
                 string  value = cboSelect.SelectedItem;            SetColumnValueAtRow(dataSource, rowNum, value)
            } catch (GAIS) {
                MessageBox.Show("Error", "");
            }        Invalidate();
            return true;
        } protected  override void Edit(  CurrencyManager [source],  int rowNum,  Rectangle bounds,  bool [readOnly],  string  instantText,  bool cellIsVisible) {         string  value = GetColumnValueAtRow([source], rowNum);        if ( cellIsVisible ) {
                cboSelect.Bounds = new Rectangle            (bounds.X, bounds.Y, bounds.Width, bounds.Height);            //
                //cboSelect.SelectedText = value            cboSelect.Text = value;
                cboSelect.Visible = true;
                AddHandler cboSelect.SelectedValueChanged, delegate(GAIS) ComboxValueChanged;
            } else {
                cboSelect.SelectedValue = value;
                cboSelect.Visible = false;
            }        if ( cboSelect.Visible ) {
                DataGridTableStyle.DataGrid.Invalidate(bounds);
            }
        } protected override Size GetPreferredSize(  Graphics g,  object value) {        return new Size(100, cboSelect.PreferredHeight);    } protected override int GetMinimumHeight() {        return cboSelect.PreferredHeight;    }
     protected override int GetPreferredHeight( Graphics g,  object value) {        return cboSelect.PreferredHeight;    }
     protected  override void Paint( Graphics g,  Rectangle bounds,  CurrencyManager [source],  int rowNum) {
            Paint(g, bounds, [source], rowNum, false);
        } protected  override void Paint( Graphics g,  Rectangle bounds,  CurrencyManager [source],  int rowNum,  bool alignToRight) {
            Paint(g, bounds, [source], rowNum, Brushes.Red, Brushes.Blue, alignToRight);
        } protected  override void Paint( Graphics g,  Rectangle bounds,  CurrencyManager [source],  int rowNum,  Brush backBrush,  Brush foreBrush,  bool alignToRight) {
             string  [date] = GetColumnValueAtRow([source], rowNum);
             Rectangle rect = bounds;
            g.FillRectangle(backBrush, rect);
            rect.Offset(0, 2);
            rect.Height -= 2;
            g.DrawString([date], this.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom));
        } protected override void SetDataGridInColumn( DataGrid value) {
            base.SetDataGridInColumn(value);
            if (! (cboSelect.Parent == null) ) {
                cboSelect.Parent.Controls.Remove(cboSelect);
            }
            if (! (value == null) ) {
                value.Controls.Add(cboSelect);
            }
        } private void ComboxValueChanged( object sender,  EventArgs e) {
            this.isEditing = true;        base.ColumnStartedEditing(cboSelect);
        }    
     protected override object GetColumnValueAtRow( System.Windows.Forms.CurrencyManager source,  int rowNum) {
             object objReturn = base.GetColumnValueAtRow(source, rowNum);
            if ( IsNothing(objReturn) ) {
                return "";
            } else {
                return objReturn.ToString;
            }
        }
      

  10.   

    1.订阅dataGrid的单元格得到当标的事件2.建立新的控件并绑定
    private void dgdFunctionArea_GotFocus(object o, EventArgs e)
    {
    //Create the combo control to be added and set its properties
    comboControl = new ComboBox();
    comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;
    comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
    comboControl.Dock = DockStyle.Fill;
    comboControl.Items.AddRange(new string[5]{"","Information Technology","Computer Science","Bio Technology","Electrical Engg"}); //Create the date time picker control to be added and set its properties
    DateTimePicker dtp = new DateTimePicker();
    dtp.Dock = DockStyle.Fill;
    dtp.Cursor = Cursors.Arrow; //Create the check box control to be added and set its properties
    CheckBox chk = new CheckBox();
    chk.Dock = DockStyle.Fill;
    chk.Cursor = Cursors.Arrow; //Create the radio button control to be added and set its properties
    RadioButton rb = new RadioButton();
    rb.Dock = DockStyle.Fill;
    rb.Cursor = Cursors.Arrow;

    //Add the controls to the respective columns in the data grid
    for(int i = 0 ;i < dataTable.Rows.Count ; i++)
    {
    //if the data in the first column is date time, add a date time control to the grid
    if(dataGrid1[i,0].ToString().Equals("DateTime") &&  hitTestGrid != null && hitTestGrid.Row == i)
    {
    datagridtextBox.TextBox.Controls.Add(dtp);
    comboControl.SendToBack();
    chk.SendToBack();
    rb.SendToBack();
    dtp.BringToFront();
    }
    //if the data in the first column is combo box, add a combo box control to the grid
    else if(dataGrid1[i,0].ToString().Equals("ComboBox")  && hitTestGrid != null && hitTestGrid.Row == i)
    {
    datagridtextBox.TextBox.Controls.Add(comboControl);
    chk.SendToBack();
    dtp.SendToBack();
    rb.SendToBack();
    comboControl.BringToFront();
    }
    //if the data in the first column is check box, add a check box control to the grid
    else if(dataGrid1[i,0].ToString().Equals("CheckBox")  && hitTestGrid != null && hitTestGrid.Row == i)
    {
    datagridtextBox.TextBox.Controls.Add(chk);
    comboControl.SendToBack();
    dtp.SendToBack();
    rb.SendToBack();
    chk.BringToFront();
    }
    //if the data in the first column is radio button, add a radio button control to the grid
    if(dataGrid1[i,0].ToString().Equals("Radio Button") &&  hitTestGrid != null && hitTestGrid.Row == i)
    {
    datagridtextBox.TextBox.Controls.Add(rb);
    comboControl.SendToBack();
    chk.SendToBack();
    dtp.SendToBack();
    rb.BringToFront();
    }
    datagridtextBox.TextBox.BackColor = Color.White;
    }
    }
      

  11.   

    我的第二个问题:
        从其他列中按TAB或SHIFT TAB进入有自定义控件的列,会跳过一列!但用左右光标键移入,或用鼠标点入的又很正常了!有解了:This behavior can be seen when you embedded a control like a textbox or combobox in your derived GridColumnStyle. If you press the tabkey slowly, you may see the cell get focus on the downkey and the cell lose focus on the upkey. One way to avoid this problem is to subclass the embedded control, and override its WndProc method, ignoring the KeyUp.
      

  12.   

    这儿,这儿,好MM,来!http://www.csdn.net/develop/read_article.asp?id=15220
      

  13.   

    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q480q
    对你会有所帮助的
      

  14.   


    我还在考虑怎么把combox加到datagrid中呢