我查询数据库内一张表的数据,有, 币制代码,币制名称,汇率,等等有 30 多个字段,现在都显示到dgv上,我需要做的是 把 币制代码,币制名称,汇率,做成combobox列,币制名称,和汇率是只读的,当币制代码的下拉选项改变时 与其对应的 币制名称,和汇率 也发生改变,球高手 给我思路,我想了好久 也在网上查了一下资料 都 和我想实现的功能 有区别,球高手解答,在线等 , 急急急

解决方案 »

  1.   

    完整代码如下:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WinTest
    {
      
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void Form2_Load(object sender, EventArgs e)
            {
                List<Demo> list = new List<Demo> {new Demo{ ID=1, 
                Name="aaa"},new Demo{ ID=1, 
                Name="bbb"} };            this.dataGridView1.DataSource = list;            for (int i = 0; i < list.Count; i++)
                {
                    ComboBox cb = new ComboBox();
                    this.dataGridView1.Controls.Add(cb);
                 
                }        }        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (dataGridView1.CurrentCell.GetType().Name == "DataGridViewComboBoxCell")
                {
                    ComboBox comboBox = (ComboBox)e.Control;                if (comboBox.Tag==null)
                    {
                        comboBox.Tag = 1;
                        comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
                    }
                  
                }
            }        void comboBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                //1是int类型
                dataGridView1.CurrentRow.Cells[2].Value = "你要改变的值";
            }
        }  
        public class Demo {        public int ID { get; set; }
            public string Name { get; set; }    }
    }
    界面
    namespace WinTest
    {
        partial class Form2
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1});
                this.dataGridView1.Location = new System.Drawing.Point(135, 85);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(240, 150);
                this.dataGridView1.TabIndex = 0;
                this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);
                // 
                // Column1
                // 
                this.Column1.HeaderText = "Column1";
                this.Column1.Items.AddRange(new object[] {
                "汇率1",
                "汇率2",
                "汇率3"});
                this.Column1.Name = "Column1";
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(413, 328);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form2";
                this.Text = "Form2";
                this.Load += new System.EventHandler(this.Form2_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.DataGridViewComboBoxColumn Column1;
        }
    }
      

  2.   

    http://blog.csdn.net/hr3773/article/details/8452737
    这是我写的用combobox实现的dgv的combobox列的效果,根据你需要的值当你离开combobox时把后面的只读的值赋给对应的单元格
      

  3.   

    自己定义个下拉框列,看看这里:http://www.cnblogs.com/csliwei/archive/2010/12/24/DataGridViewMultiComboBoxCSharp.html
      

  4.   

    像这样子的,三个comboBox控件,选择第一个的值,其他两个的值同时发生改变?
      

  5.   

    自已定义三个COMBOX,实现联动后再加到dg中
    public static void DataGridAddCobx(DataGridView DataGD, ComboBox Cobx)  
             {  
                try  
                 {  
                     DataGD.Controls.Add(Cobx);  
                     Rectangle rect = DataGD.GetCellDisplayRectangle(DataGD.CurrentCell.ColumnIndex, DataGD.CurrentCell.RowIndex, false);  
                     Cobx.Left = rect.Left;  
                     Cobx.Top = rect.Top;  
                     Cobx.Width = rect.Width;  
                     Cobx.Height = rect.Height;  
                     Cobx.Visible = true;  
                 }  
                catch (Exception ex)  
                 {  
                    throw new Exception(ex.Message);  
                }  
           }  
      

  6.   

    我用的是DevExpress的GridControl控件实现的,Windows自带的DataGridView控件没实现过,不过应该都差不多吧
      

  7.   


    我用的是DevExpress的GridControl控件实现的,Windows自带的DataGridView控件没实现过,不过应该都差不多吧
      

  8.   

    那datagridview的datasource属性的值不能改变啊
      

  9.   

    既然币制名称,和汇率是只读的那么你就没必要用combox了。用文本显示就是了。币制代码变动的时候去获得名称和汇率设置那两个单元格的值就可以了。