本帖最后由 shadan05 于 2010-12-01 10:19:59 编辑

解决方案 »

  1.   

    http://www.njxsw.com/forum-70-1.html
    去这里问一下
      

  2.   

    思路:
    把XML的内容读出来 写到一个DataTable里去
    DataGridViewk控件 可以灵活的定义列名的d_dataGridView绑定这个DataTable就行
      

  3.   

    下拉列表绑定时候,DataPropertyName和ValueMember做对比, //下拉列表绑定时候,DataPropertyName和ValueMember做对比,
                    DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn();
                    typeColumn.DataSource = GetComBoxDataSource();
                    typeColumn.Width = 200;
                    typeColumn.DataPropertyName = "type";
                    typeColumn.DisplayMember = "Text";
                    typeColumn.ValueMember = "Values";
                    typeColumn.HeaderText = "type";
                    //typeColumn = ComboBoxStyle.DropDownList;
                    this.GV_ColumnInfo.Columns.Add(typeColumn);
      

  4.   

    下面是combox的数据源,一般为name和key
      private DataTable GetComBoxDataSource()
            {
                DataTable dt = new DataTable();            dt.Columns.Add(new DataColumn( "Text", typeof (string)));
                dt.Columns.Add(new DataColumn("Values", typeof(string)));            string[] enumns = Enum.GetNames(typeof (ZCTTDataType));            for (int i = 0; i < enumns.Length;i++ )
                {
                    DataRow row = dt.NewRow();                switch (enumns[i])
                    {
                        case "dateTime":
                            row["Text"] = "dateTime";
                            row["Values"] = "dateTime";
                            break;
                        case "unsignedShort":
                            row["Text"] = "unsignedShort";
                            row["Values"] = "unsignedShort";
                            break;
                        case "ZCTTint":
                            row["Text"] = "int";
                            row["Values"] = "int";
                            break;
                        case "unsignedByte":
                            row["Text"] = "unsignedByte";
                            row["Values"] = "unsignedByte";
                            break;
                        case "ZCTTstring":
                            row["Text"] = "string";
                            row["Values"] = "string";
                            break;
                        case "ZCTTlong":
                            row["Text"] = "long";
                            row["Values"] = "long";
                            break;
                        case "binary":
                            row["Text"] = "binary";
                            row["Values"] = "binary";
                            break;
                        default:
                            break;
                    }
                    dt.Rows.Add(row);
                }
                return dt;
            }
      

  5.   


    就是说  typeColumn.DataSource = 每行都是一个不同的数据。
    问题就在这儿,我该咋样为每行绑定不同的数据源呢?
      

  6.   


     DataGridViewComboBoxColumn obj = new DataGridViewComboBoxColumn();
     obj.HeaderText = headerText;                              
     dgv.Columns.Insert(1, obj); for (int i = 0; i < dgv.Rows.Count - 1; i++)
     {
        ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DataSource = GetAuthors(dgv.Rows[i].Cells[0].Value.ToString());
         ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DisplayMember = "Value";
         ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).ValueMember = "Key";
     }
    我循环在把每行的数据源取到,然后绑定,为啥是空呢?实际上是有值的.
      

  7.   

    首先你的标题与内容不符哦.希望以后注意一下吧!方便大家能更好地回答问题.看见你代码里面有:obj.DataSource = ?;你的数据源应该是有的了恰好不久我也在琢磨这个问题.
    你可以这样试试:
    右键DataGridView->编辑列->增加DataGridViewComboBoxColumn(注意:属性列里面的displaymember和valuemember按自己的需要设定一下,DataPropertyName与数据源的名称一致.)
    作好以上准备后就可以在加载画面的时候调用
    ((DataGridViewComboBoxColumn)dataGridView1.Columns["DataPropertyName"]).DataSource = ?进行绑定了.
    顺便提一下:下拉列的数据绑定跟整个DataGridView数据源的绑定 都必须要分别做的.
    希望能帮你解决这个问题.
      

  8.   

    ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DataSource = GetAuthors(dgv.Rows[i].Cells[0].Value.ToString());
    ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DisplayMember = "Value";
    ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).ValueMember = "Key";
    不知道为什么是空值。
      

  9.   

    函数GetAuthors是做什么的?它的返回值的结构是一个table?
      

  10.   

    List<LWKeyValueArg> 是这个对象集合,每个对象有key、value俩个属性。
      

  11.   


    我是读到对象集合里,和dataTable不一样的吗?
      

  12.   


     d_dataGridView.AutoGenerateColumns = false;
                    
                       //添加名字
                    DataGridViewTextBoxColumn objText = new DataGridViewTextBoxColumn();
                    objText.DataPropertyName = "Item";
                    objText.HeaderText = "名称";
                    objText.Resizable = DataGridViewTriState.True;
                    d_dataGridView.Columns.Add(objText);                //添加作者
                    DataGridViewComboBoxColumn objComboBox = new DataGridViewComboBoxColumn();
                    objComboBox.DataSource = list;
                    objComboBox.DataPropertyName = "Author";
                    objComboBox.HeaderText = "作者";
                    objComboBox.ValueMember = "Author";
                    objComboBox.DisplayMember = "Author";
                    d_dataGridView.Columns.Add(objComboBox);                d_dataGridView.DataSource = list;
      

  13.   

    http://topic.csdn.net/u/20091113/17/e7152fb7-a1d1-430d-9b43-d6c50c5ab757.html