请教各位:
我在datagrid中一列加入了combobox,当选择了combobox中某个item,对应后面的一列显示这个item对应的ID值。用的是 selectIndexChange后,对后一列的内容赋值。
但是比如第一行选了“a”,后面显示“1”,
第二行选择“b”,可combobox选项显示的是“a”,后一列显示的也是“1”!
当重新点击第二行后,combobox选项才变为“b”,后面的显示也才改为“2”。
请问如何在第一次选择时就能正确显示ID列。

解决方案 »

  1.   

    可能是你的细节做得不好,绑定combobox在datagrid中,能做出你所要得。
      

  2.   

    你贴出你的combobox列这部分代码。
      

  3.   

    ComboBox的select函数。
    使用CurrentCell的值获得函数的参数
      

  4.   

    继承DataGridColumnStyle类,再封装个带ComboBox的类。
    处理可以在这个类里加事件做
      

  5.   

    我绑了个ComboBox在DataGridColumnStyle里面,然后加载SelectedIndexChanged事件进行处理,已经达到lz要的效果。不过细节部分可能还要进行判断处理。namesDataTable是绑定在DataGrid里面的数据源。
    private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
    ComboBox combo = sender as ComboBox;
    DataGrid parent = combo.Parent as DataGrid;
    int i = parent.CurrentRowIndex;
    if (combo.SelectedIndex == 0)
    {
    namesDataTable.Rows[i][0] = "AAA";
    }
    else if (combo.SelectedIndex == 1)
    {
    namesDataTable.Rows[i][0] = "BBB";
    }
    else if (combo.SelectedIndex == 2)
    {
    namesDataTable.Rows[i][0] = "CCC";
    }
    else
    {
    namesDataTable.Rows[i][0] = "OTHER";
    }
    }
      

  6.   

    refresh后还是不行啊。
    我就是用的继承DataGridColumnStyle类,代码: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);
    }
    }
      

  7.   

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.ComponentModel;namespace DataGridColumnStyleExample
    {
    public class DataGridComboBoxColumn : DataGridColumnStyle
    {
    private ComboBox cbColumn; private TextBox tbxColumn; private bool _isEditing = false; private bool _isUpdateUI = true; private HorizontalAlignment _itemAlignment; private string[] _strItems; public string[] Items
    {
    set
    {
    _strItems = value;
    }
    } [Browsable(true)]
    public virtual HorizontalAlignment ItemAlignment
    {
    get
    {
    return _itemAlignment;
    }
    set
    {
    _itemAlignment = value;
    }
    } public DataGridComboBoxColumn() : base()
    {
    cbColumn = new ComboBox();
    cbColumn.Bounds =  new Rectangle(0,0,0,0);
    tbxColumn = new TextBox();
    tbxColumn.BackColor = System.Drawing.Color.White;
    tbxColumn.ReadOnly = this.ReadOnly;
    tbxColumn.BorderStyle = BorderStyle.None;
    tbxColumn.Visible = false;
    } public void Init()
    {
    if (_strItems != null)
    {
    cbColumn.Items.AddRange(_strItems);
    }
    } private void cbColumn_TextChanged(object sender,EventArgs e)
    {
    string strValue = cbColumn.Text;
    if(_isEditing)
    {
    ColumnStartedEditing(tbxColumn);
    }
    cbColumn.SelectedIndexChanged -=new EventHandler(cbColumn_TextChanged);
    cbColumn.TextChanged -=new EventHandler(cbColumn_TextChanged);
    cbColumn.Text = strValue;
    if(cbColumn.SelectionStart<cbColumn.Text.Length &&
    cbColumn.SelectionStart >0)
    {
    cbColumn.SelectionStart =  cbColumn.SelectionStart;
    return;
    }
    cbColumn.SelectionStart =  cbColumn.Text.Length;
    } public ComboBox MyComboBox
    {
    get
    {
    return cbColumn;
    }
    } protected override void Abort(int rowNum)
    {
    _isEditing = false;
    cbColumn.SelectedIndexChanged -=new EventHandler(cbColumn_TextChanged);
    cbColumn.TextChanged -=new EventHandler(cbColumn_TextChanged);
    Invalidate();
    }

    protected  override void ColumnStartedEditing(System.Windows.Forms.Control editingControl)
    {
    _isEditing = true;
    _isUpdateUI = true; base.ColumnStartedEditing (editingControl);
    } protected  override void ConcedeFocus()
    {
    _isUpdateUI = true;
    _isEditing = false;
    cbColumn.Bounds =  new Rectangle(0,0,0,0);
    tbxColumn.Visible = false;
    base.ConcedeFocus ();
    } protected override bool Commit(CurrencyManager dataSource, int rowNum) 
    {
    cbColumn.SelectedIndexChanged -=new EventHandler(cbColumn_TextChanged);
    cbColumn.TextChanged -=new EventHandler(cbColumn_TextChanged);
    cbColumn.Bounds =  new Rectangle(0,0,0,0);
    if(dataSource == null || dataSource.Count ==0)
    {
    _isEditing = false;
    Invalidate();
    return true;
    }
    if(!_isEditing)
    {
    _isUpdateUI = true;
    return true;
    }
    _isEditing = false;
    if(!_isUpdateUI)
    {
    return true;
    }
    object objValue = cbColumn.Text;
    if (NullText.Equals(objValue))
    {
    objValue = Convert.DBNull;
    SetColumnValueAtRow(dataSource,rowNum , objValue); 
    Invalidate();
    return true;
    } SetColumnValueAtRow(dataSource,rowNum , objValue); 
    Invalidate();
    return true;
    } protected  override void UpdateUI(CurrencyManager source, int rowNum, string instantText)
    {
    _isUpdateUI = false;
    tbxColumn.Visible = false;
    base.UpdateUI (source, rowNum, instantText);
    } protected override void Edit(CurrencyManager source, int rowNum,Rectangle bounds, bool readOnly,string instantText, bool cellIsVisible) 
    {
    if(!this.ReadOnly)
    {
    tbxColumn.Visible = false;
    if(_isEditing)
    {
    cbColumn.Bounds =  new Rectangle
    (bounds.X + 2, bounds.Y + 2, 
    bounds.Width - 4, bounds.Height - 4);
    cbColumn.Visible = true;
    cbColumn.Focus();
    return;
    }
    string strValue = String.Empty;

    if (cellIsVisible) 
    {
    cbColumn.Bounds =  new Rectangle
    (bounds.X + 2, bounds.Y + 2, 
    bounds.Width - 4, bounds.Height - 4);
    cbColumn.Text = strValue;
    cbColumn.Visible = true;

    else 
    {
    cbColumn.Bounds =  new Rectangle(0,0,0,0); }
    _isEditing = true;
    if(cbColumn.Visible == true)
    {
    DataGridTableStyle.DataGrid.Invalidate(bounds);
    cbColumn.Focus();
    cbColumn.SelectedIndexChanged +=new EventHandler(cbColumn_TextChanged);
    cbColumn.TextChanged +=new EventHandler(cbColumn_TextChanged);
    }

    else
    {
    tbxColumn.ReadOnly = this.ReadOnly;
    _isEditing = false;
    string strValue = String.Empty;
    tbxColumn.Text = strValue;
    if (cellIsVisible) 
    {
    tbxColumn.Bounds =  new Rectangle
    (bounds.X + 2, bounds.Y + 2, 
    bounds.Width - 4, bounds.Height - 4);
    tbxColumn.Text = strValue;
    tbxColumn.Visible = true;
    tbxColumn.Focus();

    else 
    {
    cbColumn.Bounds =  new Rectangle(0,0,0,0);
    }
    }
    } protected override Size GetPreferredSize( Graphics g,  object value) 
    {
    return new Size(cbColumn.Width+2, cbColumn.PreferredHeight + 4);
    } protected override int GetMinimumHeight() 
    {
    return cbColumn.PreferredHeight + 4;
    } protected override int GetPreferredHeight(Graphics g, object value) 
    {
    return cbColumn.PreferredHeight + 4;
    } protected override void Paint(Graphics g,  Rectangle bounds, CurrencyManager source, int rowNum) 
    {
    Paint(g, bounds, source, rowNum);
    } 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 strValue = this.GetText(GetColumnValueAtRow(source, rowNum));
    this.PaintText(g,bounds,strValue,backBrush,foreBrush,alignToRight);
    } protected override void SetDataGridInColumn(System.Windows.Forms.DataGrid value) 
    {
    base.SetDataGridInColumn(value); if(tbxColumn.Parent != null)
    {
    tbxColumn.Parent.Controls.Remove(tbxColumn);
    }
    if (value != null) 
    {
    value.Controls.Add(tbxColumn);
    }
    if (cbColumn.Parent != null) 
    {
    cbColumn.Parent.Controls.Remove(cbColumn);
    }
    if (value != null) 
    {
    value.Controls.Add(cbColumn);
    }
    } protected string GetText(object value)
    {
    if(value is DBNull)
    {
    return this.NullText;
    }
    return value.ToString();
    } protected void PaintText(Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
    {
    Rectangle rectangle = textBounds;
    StringFormat format = new StringFormat();
    if (alignToRight)
    {
    format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
    }
    format.Alignment = (this.ItemAlignment == HorizontalAlignment.Left) ? StringAlignment.Near : ((this.ItemAlignment == HorizontalAlignment.Center) ? StringAlignment.Center : StringAlignment.Far);
    format.FormatFlags |= StringFormatFlags.NoWrap;
    g.FillRectangle(backBrush, rectangle);
    rectangle.Offset(0, 2 );
    rectangle.Height -= 2;
    g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, (RectangleF) rectangle, format);
    format.Dispose();
    }
    }
    }
      

  8.   

    用我的看看,不过可能会有点bug。
      

  9.   

    谢谢各位,用lookatliu(独孤常败)的方法搞定,散分~!