日期:2014-05-17  浏览次数:20943 次

(winform) DataGridView手动添加行后,怎样为行中的DataGridViewComboBoxColumn添加下拉选项?
我的一个程序中使用到了DataGridView,里面的行不是绑定数据源产生,而是手动添加的(具体是当用户选择一个文件后,就添加一行显示这个文件的路径)。 行中有一列是DataGridViewComboBoxColumn,值有“启用”和“停用”。
但现在的问题是:我DataGridView.Rows.Add()添加行后,不知道怎样将“启用”和“停用”添加到新行里面的ComboBox。
请指教.

------解决方案--------------------

 OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "E:\\";//注意这里写路径时要用c:\\而不是c:\
            openFileDialog.Filter = "文本文件
------解决方案--------------------
*.*
------解决方案--------------------
C#文件
------解决方案--------------------
*.cs
------解决方案--------------------
所有文件
------解决方案--------------------
*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            string fName = "";
            int rowcount = this.dataGridView1.Rows.Count;
            this.dataGridView1.Columns.Add("ID", "编号");
            this.dataGridView1.Columns.Add("FilePath", "文件路径");
            this.dataGridView1.Columns.Add("FileState", "文件状态");
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                DataGridViewComboBoxCell combox = new DataGridViewComboBoxCell();
                combox.Items.Clear();
                combox.Items.Add("启用");
                combox.Items.Add("停用");
                DataGridViewRow dr = dataGridView1.Rows[rowcount];
                fName = openFileDialog.FileName;
                dr.Cells[0].Value = rowcount.ToString();
                dr.Cells[1].Value = fName;
                dr.Cells[2] = combox;
                dr.Cells[2].Value = combox.Items[0];
            }