我需要在web应用程序中实现。

解决方案 »

  1.   

    参考这个例子,有代码下载.http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q480q
    5.5 How can I put a combobox in a column of a datagrid?
      

  2.   

    Sorry, I post a examep of WinForm. Please go ahead.
      

  3.   

    思路:
    1 写一个类comboForm表示下拉列表,类包含两个成员:Form窗体和DataGrid组件。
    2 写一个类NoKeyUpComboBox(继承ComboBox),目的是屏蔽WM_KEYUP消息,避免在按Tab键时出现问题。
    3 写一个继承于DataGridTextBoxColumn的类,命名为DataGridComboFormColumn。在类中加入一个ComboBox和一个comboForm,类实现下面几个功能:
    a 编辑单元格内容时显示组件NoKeyUpComboBox;
    b ComboBox下拉时显示下拉列表comboForm;
    c 鼠标点击下拉列表时,隐藏comboForm并将用户选定的内容写入单元格(当然,你也可以设置其他隐藏下拉列表的操作,比如按回车键);
    d 下拉列表comboForm不具有焦点时隐藏。代码: 
    //comboForm类:
    public class comboForm:Form
    {
    private DataGrid dataGrid;
    public DataGrid DataGrid
    {
    get {return dataGrid;}
    set {dataGrid=value;}
    }
    public comboForm()
    {
    this.FormBorderStyle=FormBorderStyle.None;
    this.StartPosition=FormStartPosition.Manual;
    dataGrid=new DataGrid();
    this.Controls.Add(dataGrid);
    dataGrid.Dock=DockStyle.Fill;
    dataGrid.CaptionVisible=false;
    }
    }
    //NoKeyUpComboBox类:
    public class NoKeyUpComboBox:ComboBox
    {
    const int WM_KEYUP=0x101;
    protected override void WndProc(ref Message msg)
    {
    if (msg.Msg==WM_KEYUP)
    return;
    base.WndProc(ref msg);
    }
    }
    //DataGridComboFormColumn类:
    public class DataGridComboFormColumn:DataGridTextBoxColumn
    {
    private NoKeyUpComboBox comboBox;
    private CurrencyManager _source;
    private int rowNum;
    private comboForm frm;
    public comboForm Frm
    {
    get {return frm;}
    }
    //我们将使用Index属性表示单元格内容与下拉列表的第Index列的内容相联系
    private int index;
    public int Index
    {
    get {return index;}
    set {index=value;}
    }public DataGridComboFormColumn()
    {
    frm=new comboForm();
    comboBox=new NoKeyUpComboBox();
    frm.Deactivate+=new EventHandler(frm_deactive);
    frm.DataGrid.Click+=new EventHandler(grid_click);
    this.comboBox.DropDown+=new EventHandler(comboBox_dropDown);
    this.comboBox.Leave+=new EventHandler(comboBox_leave);
    }
    //comboBox不具有焦点时隐藏
    private void comboBox_leave(object sender,EventArgs e)
    {
    comboBox.Visible=false;
    }
    //下拉列表--Frm不具有焦点时隐藏
    private void frm_deactive(object sender,EventArgs e)
    {
    frm.Hide();
    comboBox.Visible=false;
    }
    //comboBox下拉时显示下拉列表--Frm
    private void comboBox_dropDown(object sender,EventArgs e)
    {
    //在这里您还可以根据下拉列表的长与宽是否超出屏幕设置下拉列表的位置
    frm.Left=comboBox.PointToScreen(new Point(0,comboBox.Height)).X;
    frm.Top=comboBox.PointToScreen(new Point(0,comboBox.Height)).Y;
    frm.Show();
    frm.BringToFront();
    }
    //点击下拉列表的DataGrid时,将选中的内容写入单元格并隐藏下拉列表--Frm
    private void grid_click(object sender,EventArgs e)
    {
    BindingManagerBase cm=frm.BindingContext[Frm.DataGrid.DataSource, frm.DataGrid.DataMember];
    comboBox.Text=((DataRowView)cm.Current)[index].ToString();
    this.TextBox.Text=((DataRowView)cm.Current)[index].ToString();
    frm.Hide();
    comboBox.Visible=false;
    this.SetColumnValueAtRow(_source,rowNum,this.TextBox.Text);
    }
    //重载Edit方法,使用comboBox代替TextBox
    protected override void Edit(CurrencyManager dataSource,int rowNum,Rectangle bounds,bool readOnly,string instanttext,bool cellVisible)
    {
    base.Edit(dataSource,rowNum,bounds,readOnly,instanttext,cellVisible);
    comboBox.Parent=this.TextBox.Parent;
    comboBox.Left=this.TextBox.Left-2;
    comboBox.Top=this.TextBox.Top-2;
    comboBox.Size=new Size(this.TextBox.Width,this.comboBox.Height);
    comboBox.Text=this.TextBox.Text;
    this.TextBox.Visible=false;
    comboBox.Visible=true;
    comboBox.BringToFront();
    comboBox.Focus();
    _source=dataSource;
    this.rowNum=rowNum;
    }
    }下面的例子说明了如何使用DataGridComboFrom类:
    新建一个Windows 应用程序,加入SqlConnection,连接SQL数据库Northwind,加入下面代码。
    private void Form1_Load(object sender, System.EventArgs e)
    {
    SqlDataAdapter da=new SqlDataAdapter("select ProductName from Products",this.sqlConnection1);
    DataSet ds=new DataSet();
    da.Fill(ds,"products");
    DataSet ds_Combo=new DataSet();
    da.SelectCommand=new SqlCommand("select ProductName,QuantityPerUnit,UnitPrice from Products",this.sqlConnection1);
    da.Fill(ds_Combo,"products");
    DataGridTableStyle dts=new DataGridTableStyle();
    dts.MappingName="products";
    myDataGridColumn col=new myDataGridColumn();
    col.MappingName="ProductName";
    col.Width=100;
    col.Index=0;
    col.HeaderText="ProductName";
    col.Frm.DataGrid.DataSource=ds_Combo;//设置下拉列表的数据源
    col.Frm.DataGrid.DataMember="products";
    dts.GridColumnStyles.Add(col);this.dataGrid1.TableStyles.Add(dts);
    this.dataGrid1.SetDataBinding(ds,"products");
    }
    记得给分哟
      

  4.   

    http://chs.gotdotnet.com/quickstart/aspplus/doc/webdataaccess.aspx#update