datagrid 绑定 combox 取值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace datagridview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.RowCount = 10;
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            DataRow dr = dt.NewRow();
            dr[0] = "1";
            dr[1] = "haiyang";
            dt.Rows.Add(dr);
            DataGridViewComboBoxColumn dvcb = new DataGridViewComboBoxColumn();

            dvcb.DataSource=dt;
            dvcb.ValueMember = "Id";
            dvcb.DisplayMember = "Name";
            this.dataGridView1.Columns.Add(dvcb);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.Rows[0].Cells[1].Value.ToString();
            object name = dataGridView1.Rows[0].Cells[1].FormattedValue.ToString();
        }
    }
}

你可能感兴趣的:(C#)