查MSDN关键字DataGridColumnStyle,里面有DataGrid列写的内容,包括示例,好像就是ComboBox的

解决方案 »

  1.   

    參見:如何在DataGrid中加入ComboBox控件
    http://blog.csdn.net/tjvictor/archive/2007/01/22/1489807.aspx
      

  2.   

    谢谢楼上的几位,不过你们根本没仔细看我的问题。我不是不知道怎样添加ComboBox到DataGrid,我只是在给ComboBox预设SelectedValue初值的时候,不知道为什么语句实效了
      

  3.   

    lz: 你的要求无非是根据行中某一列的值,动态填充另一列的COMBOX,我这里在VS2005里用DataGridView实现类似的需要,现在给你贴出来,希望对你有所参考:
            private void FillGrid()
            {
                DataGridViewRow dgr = null;
                ArrayList blockArr = (ArrayList)this.Tag;            int count = blockArr.Count;
                int i = 0; string first="";            dataGridView1.RowCount = count;
                try
                {
                    foreach (InPutBlock block in blockArr)
                    {
                        first = block.ParentDocument.FileName;
                        dgr=dataGridView1.Rows[i];
                        FillRow(first, ref dgr, block);
                        i++;
                    }            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        private void FillRow(string firstCol, ref DataGridViewRow dgr, InPutBlock inputBlock)
            {
                try
                {
                    dgr.Cells[0].Value = firstCol;
                    dgr.Cells[1].Value = inputBlock.ValName;
                    dgr.Cells[2].Value = inputBlock.GetTabName();
                    //dgr.Cells[2].Tag = inputBlock.InType;          //将输入端子类型附加到第二个单元格tag上 
                    FillCommbox(inputBlock.InType);
                    DataGridViewComboBoxCell comCell = (DataGridViewComboBoxCell)dgr.Cells[3];
                    comCell.Items.AddRange(nameArr.ToArray());
                    if(inputBlock.PinName!="")
                        dgr.Cells[3].Value = inputBlock.PinName;                //dataGridView1.Rows.Add(dgr);
                    dgr.Tag = inputBlock;                           //为保存做准备,一行对应一个定值参数
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        private void FillCommbox(InputType type)
            {
                XmlFile xmlFile = new XmlFile(strResDir);            switch (type)
                {
                    case InputType.AI:
                        xmlFile.LoadXml(strResDir + "\\AI.xml");
                        break;
                    case InputType.DI:
                        xmlFile.LoadXml(strResDir + "\\DI.xml");
                        break;
                    case InputType.FI:
                        xmlFile.LoadXml(strResDir + "\\Frequency.xml");
                        break;
                    case InputType.MI:
                        xmlFile.LoadTotalMeasure();
                        break;
                    case InputType.YI:
                        xmlFile.LoadXml(strResDir + "\\Pressboard.xml");
                        break;
                }            nameArr =xmlFile.NameArr;
                indexArr = xmlFile.IndexArr;        }
      

  4.   

    谢谢 sdl2005lyx,可惜我不会2005
      

  5.   

    我也碰到和你一样的问题,不过我是在2005下,不用DataGridViewComboBoxCell的原因是它没有SelectedValue等事件