源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace dgview
{
public partial class Form1 : Form
{
///
/// DataGridView 绑定 DataGridViewComboBoxColumn 一点小问题
///
/// 原文参考 MSDN
///
public Form1()
{
InitializeComponent();
}
private DataTable CreateTable()
{
//动态创建表格
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("StuNo", typeof(String)); //typeof(String) 数据类型
DataColumn dc2 = new DataColumn("StuName", typeof(String));
dt.Columns.AddRange(new DataColumn[] { dc1, dc2 });
DataRow dr = dt.NewRow();
dr["StuNo"] = "A0001";
dr["StuName"] = "张三";
dt.Rows.Add(dr);
DataRow dr2 = dt.NewRow();
dr2["StuNo"] = "A0002";
dr2["StuName"] = "李四";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["StuNo"] = "A0003";
dr3["StuName"] = "王五";
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4["StuNo"] = "A0004";
dr4["StuName"] = "赵六";
dt.Rows.Add(dr4);
return dt;
}
private void button2_Click(object sender, EventArgs e)
{
DataGridViewTextBoxColumn TextBoxColumn = new DataGridViewTextBoxColumn();
TextBoxColumn.HeaderText = "编号";
TextBoxColumn.DataPropertyName = "StuNo";
TextBoxColumn.Name = "StuNo";
DataGridViewTextBoxColumn TextBoxColumn2 = new DataGridViewTextBoxColumn();
TextBoxColumn2.HeaderText = "姓名";
TextBoxColumn2.DataPropertyName = "StuName";
TextBoxColumn2.Name = "StuName";
DataGridViewComboBoxColumn ComboBoxColumn = new DataGridViewComboBoxColumn();
ComboBoxColumn.HeaderText = "配对";
ComboBoxColumn.Name = "info";
ComboBoxColumn.DataSource =CreateTable();
ComboBoxColumn.DisplayMember="StuName";
ComboBoxColumn.ValueMember="StuNo";
ComboBoxColumn.DataPropertyName = "StuNo"; //搞死人,绑定之后显示空白,原来这个没有设置
ComboBoxColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
DataGridViewButtonColumn ButtonColumn = new DataGridViewButtonColumn();
ButtonColumn.Name = "Details";
ButtonColumn.HeaderText = "保存";
ButtonColumn.Text = "保存";
ButtonColumn.DataPropertyName = "保存";
ButtonColumn.UseColumnTextForButtonValue = true;
dataGridView2.Columns.AddRange(new DataGridViewColumn[] { TextBoxColumn, TextBoxColumn2, ComboBoxColumn, ButtonColumn });
//
DataTable dt = CreateTable();
this.dataGridView2.DataSource = dt;
this.dataGridView2.AllowUserToAddRows = false;
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
/*这个地方的索引好像不对,怎么变成1,2,3,0 的顺序了,按钮列的ColumnIndex=0
*然后就是CellContentClick,事件并不是每次都触发了,有的时候不弹出MessageBox.Show 难道我点的不够好。
*
*/
MessageBox.Show(e.RowIndex + "_" + e.ColumnIndex);
if (e.RowIndex < 0 || e.ColumnIndex != dataGridView2.Columns["Details"].Index)
return;
string StuNo = this.dataGridView2.Rows[e.RowIndex].Cells["StuNo"].Value.ToString();
string StuName = this.dataGridView2.Rows[e.RowIndex].Cells["StuName"].Value.ToString();
string info = this.dataGridView2.Rows[e.RowIndex].Cells["info"].Value.ToString();
MessageBox.Show("\tStuNo:" + StuNo + "\t\r\n" + "\tStuName:" + StuName + "\t\r\n" + "\t下拉列表框:" + info + "\t");
}
}
}
//如果下拉列表框 绑定的是不同的数据源
需要如下设置:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells["info"]).Style.NullValue = "10";
}
//即使这样,直接报错,由于this.dataGridView2.Rows[e.RowIndex].Cells["info"].Value
会等于空,报错。
官方解释:
http://msdn.microsoft.com/zh-cn/library/ms404353.aspx
等于弄了一个 “请选择” 我不请选择。
其实到这一步,我们也应该知道怎么办了。