在form里datagridview本身里面没有listbox,用代码怎么才能把listbox加到datagridview的columns中??

解决方案 »

  1.   

    要实现什么功能啊,没有listbox为什么不用combobox?
      

  2.   

    2 datagridview_Click 
      中判断是否该显示Listbox 
    是: 
    if (dataGridView1.CurrentCell.ColumnIndex==0) 
    {   Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false); 
    Listbox.Left = rect.Left; 
    Listbox.Top = rect.Top; 
    Listbox.Width = rect.Width; 
    Listbox.Height = rect.Height; 
    Listbox.Visible = true; 
    }
      

  3.   

    if (dataGridView1.CurrentCell.ColumnIndex==0)  
    {    Rectangle rect = dataGridView1.GetCellDisplayRectangle
     这是什么意思了...一运行就报这个错误
      

  4.   

      Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);  
    是一句话!
      

  5.   

    自已创建一个 DataGridViewColumn 和相应的DataGridViewListBoxCell
    较麻烦. listbox的没做过, 有一个用DateTimePicker的. 
    public class CalendarColumn : DataGridViewColumn
        {
            public CalendarColumn()
                : base(new CalendarCell())
            {
            }
            public override DataGridViewCell CellTemplate
            {
                get
                {
                    return base.CellTemplate;
                }
                set
                {
                    if (value != null && !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
                    {
                        throw new InvalidCastException("Must   be   a   CalendarCell ");
                    }
                    base.CellTemplate = value;
                }
            }
        }    public class CalendarCell : DataGridViewTextBoxCell
        {        public CalendarCell()
                : base()
            {            // this.Style.Format = "d ";
            }        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
            {            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
                CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
                ctl.Value = Convert.ToDateTime(this.Value.ToString());
            }        public override Type EditType
            {
                get
                {
                    return typeof(CalendarEditingControl);
                }
            }        public override Type ValueType
            {
                get
                {
                    return typeof(DateTime);
                }
            }        public override object DefaultNewRowValue
            {
                get
                {
                    DateTime a = Convert.ToDateTime("00:00:00");
                    return a;
                }
            }
        }    class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
        {        DataGridView dataGridView;
            private bool valueChanged = false;
            int rowIndex;        public CalendarEditingControl()
            {
    //            this.Format = DateTimePickerFormat.Time;
                this.Format = DateTimePickerFormat.Custom;
                this.CustomFormat = "HH:mm";            
                this.ShowUpDown = true;
            }
            public object EditingControlFormattedValue
            {
                get
                {
                   
                    return this.Value.ToLongTimeString();
                }
                set
                {
                    String newValue = value as String;                if (newValue != null)
                    {                    this.Value = Convert.ToDateTime(newValue);
                    }
                }
            }
            public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
            {
                return EditingControlFormattedValue;
            }        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
            {
                this.Font = dataGridViewCellStyle.Font;
                this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
                this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
            }
            public int EditingControlRowIndex
            {
                get
                {
                    return rowIndex;
                }
                set
                {
                    rowIndex = value;
                }
            }
            public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
            {            switch (key & Keys.KeyCode)
                {
                    case Keys.Left:
                    case Keys.Up:
                    case Keys.Down:
                    case Keys.Right:
                    case Keys.Home:
                    case Keys.End:
                    case Keys.PageDown:
                    case Keys.PageUp:
                        return true;
                    default:
                        return false;
                }
            }
            public void PrepareEditingControlForEdit(bool selectAll)
            {        }
            public bool RepositionEditingControlOnValueChange
            {
                get
                {
                    return false;
                }
            }
            public DataGridView EditingControlDataGridView
            {
                get
                {
                    return dataGridView;
                }
                set
                {
                    dataGridView = value;
                }
            }
            public bool EditingControlValueChanged
            {
                get
                {
                    return valueChanged;
                }
                set
                {
                    valueChanged = value;
                }
            }
            public Cursor EditingPanelCursor
            {
                get
                {
                    return base.Cursor;
                }
            }        protected override void OnValueChanged(EventArgs eventargs)
            {            valueChanged = true;            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
                base.OnValueChanged(eventargs);
            }
        }
      

  6.   

    是一句话.我在FORM里画好了..就想在Columns里插入一个listbox,,可没有.
    刚才你写的试过了..没有效果
    谢谢
      

  7.   

    我添加的是combobox代码Form_Load中LoadcomboBoxPointType();
     cmbPointType.Visible = false;
     cmbPointType.SelectedIndexChanged+=new EventHandler(cmbPointType_SelectedIndexChanged);
    this.dgvGCPs.Controls.Add(cmbPointType);public void LoadcomboBoxPointType()
    {
       //透明色初始化设置
       cmbPointType.DrawMode = DrawMode.OwnerDrawFixed;
       cmbPointType.DropDownStyle = ComboBoxStyle.DropDownList;
        cmbPointType.DrawItem += new DrawItemEventHandler(cmbTransparence_DrawItem);
        mbPointType.ItemHeight = 18;
        cmbPointType.BeginUpdate();
        cmbPointType.Items.Clear();
         //循环遍历添加每一项
        cmbPointType.Items.Add("控制点");
        cmbPointType.Items.Add("检查点");
        cmbPointType.EndUpdate();
     }private void dataGridView_Click(object sender, EventArgs e)
    {
    if (dataGridView1.CurrentCell.ColumnIndex == (int)GeoRectificationTable.TableAttributeIndex.pointType && dataGridView1.CurrentCell.RowIndex < (myGeoRectificationTable.BasePointNumber > myGeoRectificationTable.WarpPointNumber ? myGeoRectificationTable.BasePointNumber : myGeoRectificationTable.WarpPointNumber))
                    {
                        Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);
                        string sexValue = dataGridView1.CurrentCell.Value.ToString();
                        cmbPointType.Left = rect.Left;
                        cmbPointType.Top = rect.Top;
                        cmbPointType.Width = rect.Width;
                        cmbPointType.Height = rect.Height;
                        cmbPointType.Visible = true;
                    }
                    else
                    {
                        cmbPointType.Visible = false;
                    }
    }private void cmbTransparence_DrawItem(object sender, DrawItemEventArgs e)
            {
                ComboBox cmbTransparence = (ComboBox)(sender);
                if (e.Index < 0) return;            e.DrawBackground();            //得到绘制的矩形框
                Rectangle rect = new Rectangle(4, e.Bounds.Top + 2, e.Bounds.Height + 10, e.Bounds.Height - 4);            string colorName = cmbTransparence.Items[e.Index].ToString();
                Font pFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);            //在指定的矩形内绘制文本
                e.Graphics.DrawString(colorName, pFont, Brushes.Black, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                //在指定的边界范围内绘制聚集框
                e.DrawFocusRectangle();
            }