[*] Hello Snoopy

.NET and Flash Blog
如何在DataGrid中显示ComboBox
//Author:yinzx
//Date:2004-06-08
//Editor:UltraEdit-32
//Ref:www.codeproject.com

//说明:前一段时间要实现如何在DataGrid中显示ComboBox,在网上找了好多文章,但我的基础较差,好多看不明白
//        但这种效果实现的原理有两种:一种是计算ComboBox的位置;另一种不用,而是继承DataGridTextBoxColumn
//        我最后采用了后一种方式,效果不错。这里只是抛砖引玉,要实现更高级的效果,还要加入一特性。
//        如有Bloger对此感兴趣,可与我联系

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace yinzx
{
    
public class GridComboBoxSample:System.Windows.Forms.Form
    
{
        
private DataGrid grd = new DataGrid();
        
        [STAThread]
        
public static void Main() 
        
{
            Application.Run(
new GridComboBoxSample());
        }

        
public GridComboBoxSample()
        
{
            
this.Text = "如何在DataGrid中实现ComboBox";
            
this.FormBorderStyle = FormBorderStyle.FixedDialog;
            
this.StartPosition = FormStartPosition.CenterScreen;
            
this.MinimizeBox = this.MaximizeBox = false;
            
            
this.Controls.Add(grd);            
            grd.Dock 
= DockStyle.Fill;
            
            
//生成一个DataTable(表)
            DataTable t = new DataTable("PersonInfo");
            t.Columns.Add(
"Name",typeof(string));
            t.Columns.Add(
"Address",typeof(string));
            t.Rows.Add(
new string[]{"yinzx","China"});
            
            
//生成一个DataGridTableStyle(表样式)
            DataGridTableStyle ts = new DataGridTableStyle();
            DataGridTextBoxColumn c1 
= new DataGridTextBoxColumn();
            DataGridComboBoxColumn c2 
= new DataGridComboBoxColumn();            
            c1.MappingName 
= c1.HeaderText  = "Name";
            c2.MappingName 
= c2.HeaderText  = "Address";
            c2.FillComboBox(
new string[]{"China","Canada","France"});
            ts.MappingName 
= "PersonInfo";
            ts.GridColumnStyles.Add(c1);
            ts.GridColumnStyles.Add(c2);
            
            
//把表及表样式绑定到grd上
            grd.TableStyles.Add(ts);
            grd.DataSource 
= t;
        }
        
    }

    
    
public class DataGridComboBoxColumn:DataGridTextBoxColumn
    
{
        
private ComboBox _cbo = new ComboBox();
        
private CurrencyManager _source;
        
private int _iRowNum;
        
        
public DataGridComboBoxColumn()
        
{
            _cbo.Leave 
+= new EventHandler(cbo_Leave);
            _cbo.Visible 
= false;
        }
        
        
        
public void FillComboBox(string[] cboData)
        
{
            
//填充数据到_cbo中
            _cbo.Items.AddRange(cboData);
        }


        
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
        
{
            
if (!this.DataGridTableStyle.DataGrid.Controls.Contains(_cbo)) this.DataGridTableStyle.DataGrid.Controls.Add(_cbo);
            _iRowNum 
= rowNum;
            _source  
= source;
            _cbo.Bounds 
= bounds;//关键语句:把_cbo准确定位到相应的单元格,且不用计算位置
            
            
try
            
{
                _cbo.Text 
= (string)this.GetColumnValueAtRow(_source, _iRowNum);            
            }

            
catch{}
            _cbo.Visible 
= true;
            _cbo.Focus();
        }

        
        
public void cbo_Leave(object sender, EventArgs e) 
        
{
            
try
            
{
                
this.SetColumnValueAtRow(_source,_iRowNum ,_cbo.Text);
            }

            
catch{}
            _cbo.Visible 
= false;
        }
        
    }

}

posted on 2004-07-05 16:06  HelloSnoopy  阅读(731)  评论(0编辑  收藏  举报