提供一个笨办法
加一个隐藏的comobox
然后当点击到datagrid的某一格的时候,设定comobox的位置并显示

解决方案 »

  1.   

    winform
    http://www.csdn.net/develop/Read_Article.asp?Id=16434
      

  2.   

    我用的是webform
    模板列的方法能说得详细一些吗
    小弟新手
      

  3.   

    谢谢acewang(**^o^**) 
    webform会不会区别很大呢?
      

  4.   

    添加一个模板列  <asp:TemplateColumn SortExpression="it" HeaderText="it" FooterText="it">
          <ItemTemplate>
       <asp:RadioButton ID="radio" Runat="server"></asp:RadioButton>
    </ItemTemplate>
    </asp:TemplateColumn>
      

  5.   

    ASP.NET很灵活,用模板列,或自己写HTML代码都可以。
    windows form的麻烦一点,要自己定义,好在.net的结构比较开放,下面是一个combobox列.using System;
    using System.Drawing;
    using System.Data;
    using System.Collections;
    using System.Windows.Forms;
    using System.ComponentModel;namespace Health.Applications.RIS
    {
    public class DataGridComboBox : ComboBox
    {
    private DataGridComboBoxColumn m_column; public bool isInEditOrNavigateMode = true; //
    // Basic ComboBox used for all ComboBoxes displayes in a datagrid
    //
    public DataGridComboBox(DataGridComboBoxColumn column) : base()
    {
    //base.DropDownStyle =ComboBoxStyle.DropDownList;
    m_column = column;
    } protected override bool ProcessKeyMessage(ref Message m)
    {
    return base.ProcessKeyMessage (ref m);
    } protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    return base.ProcessCmdKey (ref msg, keyData);
    } } /// <summary>
    /// 此类用于在DataGrid里添加一个含ComboBox输入的列。
    /// by wangchuanju  2003.11
    /// </summary>
    public class DataGridComboBoxColumn : DataGridColumnStyle
    {
    private DataGridComboBox _comboBox;
    private object _dataSource;
    private string _displayMember, _valueMember;
    private string _oldValue="";
    private object _defaultValue;
    private bool _inEdit= false;
    private int _xMargin = 2;
    private int _yMargin = 1; //public DataGridComboBoxColumn(PropertyDescriptor prop)
    //{
    //}
    public DataGridComboBoxColumn(string colName, object dataSource, string displayMember, string valueMember, object defaultValue, DataGrid dataGrid)
    {
    _comboBox = new DataGridComboBox(this);
    _comboBox.Visible = false;
    _dataSource = dataSource;
    _displayMember = displayMember;
    _valueMember = valueMember;
    _defaultValue = defaultValue;

    _comboBox.DataSource = _dataSource;
    _comboBox.DisplayMember = _displayMember;
    _comboBox.ValueMember = _valueMember;
    //_comboBox.DropDownStyle = ComboBoxStyle.DropDownList;  该句会引起在ComboBox框里无法用键盘上下箭头键进行滚动
    Graphics gContext = dataGrid.CreateGraphics();
    float widest = 0;
    SizeF stringSize = new SizeF(0,0);
    if (_dataSource is DataTable)
    {
    DataTable table = _dataSource as DataTable;
    foreach (DataRow dr in table.Rows)
    {
    stringSize = gContext.MeasureString(dr[_displayMember].ToString(), dataGrid.Font);
    if (stringSize.Width > widest)
    widest = stringSize.Width;
    }
    }
    else
    {
    PropertyDescriptor pd;
    if (_dataSource is IList)
    {
    foreach (object item in (_dataSource as IList))
    {
    pd = TypeDescriptor.GetProperties(item)[_displayMember];
    if (pd != null)
    {
    stringSize = gContext.MeasureString(pd.GetValue(item).ToString(), dataGrid.Font);
    if (stringSize.Width > widest)
    widest = stringSize.Width;
    }
    }
    }
    }
    gContext.Dispose(); _comboBox.DropDownWidth = (int)Math.Ceiling(widest) + 20; this.Width = _comboBox.DropDownWidth + 25;   // add the space for the dropdown arrow

    this.MappingName = colName;
    this.HeaderText = colName;
    dataGrid.Controls.Add(_comboBox); _comboBox.SelectedIndexChanged += new EventHandler(_comboxBox_SelecteIndexChanged);
    } private void _comboxBox_SelecteIndexChanged(object sender, System.EventArgs e)
    {
    if (_inEdit)
    {
    object _value = _comboBox.SelectedValue;
    if (_value == null && _defaultValue != null)
    _value = _defaultValue; this.ColumnStartedEditing(_comboBox);

    if(NullText.Equals(_value) || _value == null) 
    {
    _value = System.Convert.DBNull; 
    }
    this.SetColumnValueAtRow((CurrencyManager)this.DataGridTableStyle.DataGrid.BindingContext[this.DataGridTableStyle.DataGrid.DataSource, this.DataGridTableStyle.DataGrid.DataMember], this.DataGridTableStyle.DataGrid.CurrentCell.RowNumber, _value);
    _comboBox.SelectedValue = _value;
    }
    } public DataGridComboBox DisplayComboBox
    {
    get { return _comboBox; }
    } public object DisplayDataSource
    {
    get { return _dataSource; }
    set 

    _dataSource = value;
    _comboBox.DataSource = _dataSource;
    }
    } public string DisplayMember
    {
    get { return _displayMember; }
    set 
    {
    _displayMember = value;
    _comboBox.DisplayMember = _displayMember;
    }
    } public string ValueMember
    {
    get { return _valueMember; }
    set 
    {
    _valueMember = value;
    _comboBox.ValueMember = _valueMember;
    }
    } public object DefaultValue
    {
    get { return _defaultValue; }
    set 
    {
    _defaultValue = value;
    }
    } protected override void SetDataGridInColumn(DataGrid value)
    {
    base.SetDataGridInColumn(value);
    if ( _comboBox.Parent != value && _comboBox.Parent != null)
    _comboBox.Parent.Controls.Remove(_comboBox); if ( value != null )
    value.Controls.Add(_comboBox);
    }
      

  6.   

    protected override void UpdateUI(CurrencyManager source, int rowNum, string instantText)
    {
    _comboBox.Text = GetText(GetColumnValueAtRow(source, rowNum));
    if(instantText!=null)
    {
    _comboBox.Text = instantText;
    }
    }
    protected override void Abort(int rowNum)
    {
    _comboBox.Text = _oldValue;
    HideComboBox();
    EndEdit();
    } protected override bool Commit(CurrencyManager dataSource, int rowNum)
    {
    HideComboBox();
    if(!_inEdit) 
    {
    return true;
    } try 
    {
    object _value = _comboBox.SelectedValue;

    if (_value == null && _defaultValue != null) _value = _defaultValue; if(NullText.Equals(_value)) 
    {
    _value = System.Convert.DBNull; 
    }
    this.SetColumnValueAtRow(dataSource, rowNum, _value);
    }
    catch 
    {
    _comboBox.Text = _oldValue;
    return false;
    }
    finally 
    {
    EndEdit();
    }
    return true;
    } protected override void ConcedeFocus()
    {
    _comboBox.Visible = false;
    //_inEdit = false;
    //this.DataGridTableStyle.DataGrid.Focus();
    } protected override void ColumnStartedEditing(Control editingControl)
    {
    base.ColumnStartedEditing (editingControl);
    }
    protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
    {
    _comboBox.Text = "";
    Rectangle originalBounds = bounds;
    _oldValue = _comboBox.Text; if (!cellIsVisible) return; bounds.Offset(_xMargin, _yMargin);
    bounds.Width -= _xMargin * 2;
    bounds.Height -= _yMargin ;
    _comboBox.Bounds = bounds;
    if (!readOnly)
    _comboBox.Visible = true; _comboBox.SelectedValue = GetText(GetColumnValueAtRow(source, rowNum));
    if (instantText != null)
    {
    _comboBox.SelectedValue = instantText;
    } _comboBox.RightToLeft = this.DataGridTableStyle.DataGrid.RightToLeft;
    _comboBox.Focus(); if (instantText == null)
    _comboBox.SelectAll();
    else
    {
    int end = _comboBox.Text.Length;
    _comboBox.Select(end, 0);
    } if(_comboBox.Visible)
    this.DataGridTableStyle.DataGrid.Invalidate(originalBounds); _inEdit = true;
    } protected override int GetMinimumHeight()
    {
    return _comboBox.PreferredHeight + _yMargin;
    } protected override int GetPreferredHeight(Graphics g, object value)
    {
    return FontHeight + _yMargin;
    } protected override Size GetPreferredSize(Graphics g, object value)
    {
    Size extends = Size.Ceiling(g.MeasureString(GetText(value), this.DataGridTableStyle.DataGrid.Font));
    extends.Width += _xMargin * 2 + DataGridTableGridLineWidth;
    extends.Height += _yMargin;
    return extends;
    } 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)
    {
    Brush backBrush = new SolidBrush(this.DataGridTableStyle.BackColor);
    Brush foreBrush = new SolidBrush(this.DataGridTableStyle.ForeColor);
    Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
    }
    protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
    {
    string text = GetText(GetColumnValueAtRow(source, rowNum));
    string tempText; if (_dataSource is DataTable)
    {
    DataTable table = _dataSource as DataTable;
    foreach (DataRow dr in table.Rows)
    {
    if (dr[_valueMember].ToString() == text)
    {
    text = dr[_displayMember].ToString();
    break;
    }
    }
    }
    else
    {
    PropertyDescriptorCollection pdc;
    PropertyDescriptor pd;
    if (_dataSource is IList)
    {
    foreach (object item in (_dataSource as IList))
    {
    pdc = TypeDescriptor.GetProperties(item);
    pd = pdc[_valueMember];
    if (pd != null)
    {
    tempText = pd.GetValue(item).ToString();
    if (tempText == text)
    {
    pd = pdc[_displayMember];
    if (pd != null)
    text = pd.GetValue(item).ToString();
    }
    }
    }
    }

    } PaintText(g, bounds, text, backBrush, foreBrush, alignToRight);
    } private void PaintText(Graphics g ,Rectangle bounds, string text, bool alignToRight) 
    {
    Brush backBrush = new SolidBrush(this.DataGridTableStyle.BackColor);
    Brush foreBrush = new SolidBrush(this.DataGridTableStyle.ForeColor);
    PaintText(g, bounds, text, backBrush, foreBrush, alignToRight);
    }
    private void PaintText(Graphics g ,Rectangle bounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight) 
    {
    Rectangle rect = bounds;
    RectangleF rectF = rect;
    StringFormat format = new StringFormat();

    if (alignToRight )
    format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
    switch(Alignment)
    {
    case HorizontalAlignment.Left : format.Alignment = StringAlignment.Near; break;
    case HorizontalAlignment.Right : format.Alignment = StringAlignment.Far; break;
    case HorizontalAlignment.Center : format.Alignment = StringAlignment.Center; break;
    }
    format.FormatFlags |= StringFormatFlags.NoWrap; g.FillRectangle(backBrush, rect);
    rectF.Offset(0, _yMargin);
    rectF.Height -= _yMargin;
    g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, rectF, format); format.Dispose();
    } private string GetText(object val) 
    {
    if(val==System.DBNull.Value) 
    {
    return this.NullText;
    }
    if(val!=null) 
    {
    return val.ToString();
    }
    else 
    {
    return string.Empty;
    }
    } private int DataGridTableGridLineWidth
    {
    get
    {
    if(this.DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid) 

    return 1;
    }
    else
    {
    return 0;
    }
    }
    } private void HideComboBox()
    {
    if(_comboBox.Focused)
    {
    this.DataGridTableStyle.DataGrid.Focus();
    }
    _comboBox.Visible = false;
    } private void EndEdit()
    {
    _inEdit = false;
    Invalidate();
    }
    }
    }
      

  7.   

    谢谢大家的帮助
    可能我说的不太清楚
    1。我用的是webform
    2。我需要每行的下拉框中的数据从数据库读出来,每行都是不一样的