tylike(天外来客),可以具体些吗? 在代码的什么地方改?

解决方案 »

  1.   

    你的意思如果是指初始时所有cell里都有combobox的话,你必须改很多地方。你必须加入一个combobox[] 作为成员对象,并重写paint函数,使每个cell对应的combobox都visible=true,而且使其bounds除去偏移的象素后正好和cell里面的相同
      

  2.   

    那我就没办法了,用别的grid吧,呵呵
    flexcell,本身就提供这样的功能,但是我看了演示,也不是真接显示的
    exgrid2000功能也不错.但我还没搞懂,只能帮你这么多了
      

  3.   

    try:private void Form1_Load(object sender, System.EventArgs e)
    {
      daEmp.Fill(ds, "Employees");
      DataGrid1.DataSource = ds;
      DataGrid1.DataMember = "Employees";
      
      for(int i = 0; i < this.DataGrid1.VisibleRowCount - 1; i++)
      {    //把MyCombo做成一个局部变量就可以了
        //其它相应的变化自己搞定吧:)
        ComboBox  MyCombo=new ComboBox();
        MyCombo.Name = "MyCombo";
        MyCombo.Items.Add("Sales Representative");
        MyCombo.Items.Add("Inside Sales Coordinator");
        MyCombo.Items.Add("Vice President, Sales");
        MyCombo.Items.Add("Sales Manager");
        MyCombo.Items.Add("Flunky");
        MyCombo.TextChanged+=new EventHandler(Ctrls_TextChanged);
        MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
        MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
        MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
        MyCombo.Text = DataGrid1[i,3].ToString();
        //还可以设置MyCombo的Anchor等属性
        MyCombo.Visible = true;    DataGrid1.Controls.Add(MyCombo);
     
      }
    }
      

  4.   

    FileNewExit((呵呵))谢谢你,但是当调节 combobox所在列的 width时,combobox的width没有跟着变化,可以让combobox跟着所在列的width变化而变化吗?
      

  5.   

    这样的话,我觉得生成一个ArrayList吧,以后更好操作
    try:ArrayList ComboBoxes = new ArrayList();private void Form1_Load(object sender, System.EventArgs e)
    {
    ///.....这些不变
       ComboBoxes.Add(MyCombo);//添加这句
       DataGrid1.Controls.Add(MyCombo);
    }private void DataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      for(int i = 0; i < this.DataGrid1.VisibleRowCount - 1; i++)
      {
       ComboBox MyCombo = ComboBoxes[i] as ComboBox;//取第i行的ComboBox
       if(MyCombo != null)
       {
         MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
         MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
         MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
       }
      }
    }
      

  6.   

    FileNewExit((呵呵)) ,真是太谢谢你了,不过当移动 datagrid的滚动条时,有些 combobox就出现在 datagrid的上部,可以帮我再改改吗?
      

  7.   

    ArrayList ComboBoxes = new ArrayList();
    int Rows;//新增变量,用来记录当前DataGrid的行数
    ==========================================================
    private void Form1_Load(object sender, System.EventArgs e)
    {
      daEmp.Fill(ds, "Employees");
      Rows = ds.Tables[0].Rows.Count;//其实,行数是这样取得的
              
      DataGrid1.DataSource = ds;
      DataGrid1.DataMember = "Employees";  for(int i = 0; i < Rows; i++)//不像原来那样,只对当前可见行添加ComboBox,而是对所有行如此
      {
    ComboBox  MyCombo=new ComboBox();
    MyCombo.Name = "MyCombo";
    MyCombo.Items.Add("Sales Representative");
    MyCombo.Items.Add("Inside Sales Coordinator");
    MyCombo.Items.Add("Vice President, Sales");
    MyCombo.Items.Add("Sales Manager");
    MyCombo.Items.Add("Flunky");
    MyCombo.TextChanged+=new EventHandler(Ctrls_TextChanged);
    MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
    MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
    MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
    MyCombo.Text = DataGrid1[i,3].ToString();
    MyCombo.Visible = true;
      ComboBoxes.Add(MyCombo);
        
    DataGrid1.Controls.Add(MyCombo);
       
      }

    }=============================================================
    private void DataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      for(int i = firstRow; i < this.DataGrid1.VisibleRowCount + firstRow; i++)
        if(i < Rows)
        {
         ComboBox MyCombo = ComboBoxes[i] as ComboBox;
         if(MyCombo != null)
         {
          MyCombo.Left = DataGrid1.GetCellBounds(i,3).Left;
          MyCombo.Top = DataGrid1.GetCellBounds(i,3).Top;
          MyCombo.Width = DataGrid1.GetCellBounds(i,3).Width;
          MyCombo.Visible = this.DataGrid1.FirstVisibleColumn <= 3;
         }
        }
    }========================================================
    DataGrid1_CurrentCellChanged  去掉
    int firstRow = 0;//新增变量,用来存储当前DataGrid中的首行(我们看到的首行)
    private void DataGrid1_Scroll(object sender, System.EventArgs e)
    {
     int y = 40;//当ColumnHeaderVisible为false的情况下40表示的是Caption部分高度与ColumnHeader高度之和,不知道有没有什么函数可以获取到这两个值.同时,这个程序不能在ColumnHeaderVisible为true的情况下运行,因为在此情况下测试老是失败:-(,如果有人知道如何处理此种情况,请给我发个短消息,谢谢~~
     for(;;)
    if(this.DataGrid1.GetCellBounds(firstRow,3).Y < y)
    {//表示向下滚动了
      ComboBox MyCombo = ComboBoxes[firstRow++] as ComboBox;
         if(MyCombo != null)
           MyCombo.Visible = false;
    }
    else if(this.DataGrid1.GetCellBounds(firstRow,3).Y > y)
    {//表示向上滚动了
      ComboBox MyCombo = ComboBoxes[firstRow--] as ComboBox;
         if(MyCombo != null)
            MyCombo.Visible = false;
    }
    else
         break;//从死循环中跳出
    }//在Scroll中我们并没有处理水平滚动,但程序对此依然有效,因为水平滚动没有改变firstRow的值.=================================================
    除了上述改动,其余部分不变
      

  8.   

    其实,你完全可以使用GlacialList来完成你的功能,GlacialList控件见:http://www.codeproject.com/cs/miscctrl/aa_listview.asp?target=GlacialList
      

  9.   

    FileNewExit((呵呵)) ,真谢谢你,但你最后改动的还有问题,就是移动datagrid的滚动条的时候,还是会造成问题,你试过吗?
      

  10.   

    DataGrid1_Scroll的向上滚动部分改写为:else if(this.DataGrid1.GetCellBounds(firstRow,3).Y > y)
     {
      int i = this.DataGrid1.VisibleRowCount + firstRow - 1;
      if(i < Rows)
      { 
       ComboBox MyCombo = ComboBoxes[i] as ComboBox;
       if(MyCombo != null)
          MyCombo.Visible = false;
      }
      firstRow--;
      }
      

  11.   

    你最好自己写一个类
    public class MyDataGridComboBoxColumn : DataGridTextBoxColumn
    {
    public NoKeyUpCombo ColumnComboBox = null;
    private System.Windows.Forms.CurrencyManager _source = null;
    private int _rowNum;
    private bool _isEditing = false;

    public MyDataGridComboBoxColumn()
    {
    ColumnComboBox = new NoKeyUpCombo();
        
    ColumnComboBox.KeyPress+=new KeyPressEventHandler(Text_KeyPress);
    ColumnComboBox.Leave += new EventHandler(LeaveComboBox);
    ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);
    ColumnComboBox.SelectionChangeCommitted += new System.EventHandler(ComboStartEditing);
    }

    public void MyComboValueChanged(int rowChanging, object newValue)
    {
    Console.WriteLine("index changed {0} {1}", rowChanging, newValue);
    } private void ComboStartEditing(object sender, EventArgs e)
    {
    _isEditing = true;
    base.ColumnStartedEditing((Control) sender);
    }

    private void ComboIndexChanged(object sender, EventArgs e)
    {
    MyComboValueChanged(_rowNum , ColumnComboBox.Text); 
    } 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;
    if(!_isEditing) 
    {
    ColumnComboBox.Visible=false;
    return true;
    }
    try 
    {
    SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);
    }
    catch 
    {
    return false;
    }
    finally 
    {
    _isEditing=false;
    ColumnComboBox.Visible=false;
    }
    return true;
    }
    .
    .
    .
    .
    .
    接下来的一些事件和方法属性什么的,你可以自己去定义然后,你用这个类
    MyDataGridSearchComboBoxColumnS col11=new MyDataGridSearchComboBoxColumnS(dg);
    col11.MappingName="Cause";
    col11.HeaderText="原因";
    col11.Width=100;
                               col11.EnterKeyPress +=new EventHandler(this.CellEnterPress);
      

  12.   

    哦,对了上面的
    public NoKeyUpCombo ColumnComboBox = null;
    ColumnComboBox = new NoKeyUpCombo();
    是我自己写的另外一个类
    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);
    }
    }我觉得,你还是把上面的那,改为:
    public ComboBoxColumnComboBox = null;
    ColumnComboBox = new ComboBox();
      

  13.   

    上面的回答应该已经解决了,我没有细看:)我只是想说:
    要让DataGrid或DataList或Repeater等对象中的ComboBox、DropDownList等对象绑定数据,你必须给你的DataGrid等对象添加事件:OnItemDataBind。然后在相应的事件处理代码中对ComboBox等对象进行其数据的绑定。
      

  14.   

    FileNewExit((呵呵)) ,sorry,还是不行.
     Hexudong1979(何须懂何必懂),你那个类也是要点击才可以看到 combobox的吧?
      

  15.   

    >>>>>FileNewExit((呵呵)) ,sorry,还是不行我测试是正确的啊.留下你的E-mail,我把代码发给你吧
      

  16.   

    我也是在拖動scroll時出現死機﹕
    [email protected]
      

  17.   

    FileNewExit((呵呵)) ,太谢谢你了,我的email是:[email protected]