Datagridview中的ComboBox

其实不应该算标题所说,
这里没有用Datagridview控件里的DatagridviewComboBoxColumn列,
因为之前用这个,有功能实现不了,
就是显示的问题,由于我是自己写代码获得的数据源,
绑定的时候DataPropertyName显示不了数据,....
kv值都很正常.....
无奈换了个思路.
研究了好几天,终于弄成功了,思路太重要了... 
记录下,以方便以后参考.,
本文用是添加控件的方式实现的,创建一个ComboBox,为其写入内容,
再在指定单元格获得焦点的时候将ComboBox写入到该单元格,
根据该列的内容显示为序号+内容,
单元格内显示是内容的序号+内容,这里写了几个循环来实现这一功能.
保存的时候又循环全部行来将指定行写入显示值的一部分的值.

View Code
  1 using System;

  2 using System.Collections.Generic;

  3 using System.ComponentModel;

  4 using System.Data;

  5 using System.Drawing;

  6 using System.Linq;

  7 using System.Text;

  8 using System.Windows.Forms;

  9 

 10 namespace MIS

 11 {

 12     public partial class FormHrDept : Form

 13     {

 14         DataSet ds = new DataSet();

 15         DataTable dt = new DataTable();

 16         ComboBox cbb1 = new ComboBox();

 17 

 18 

 19         public FormHrDept()

 20         {

 21             InitializeComponent();

 22         }

 23 

 24         private void FormHrDept_Load(object sender, EventArgs e)

 25         {

 26             find();

 27             findComBoBoxValue();

 28             setParentDeptIdCellValue();

 29 

 30 

 31         }

 32         private void find()//查找数据

 33         {

 34             ds.Tables.Clear();

 35             ds = appcode.DALL.selectAll_Dataset("HrDept");

 36             ds.Tables[0].TableName = "HrDept";

 37             dt = ds.Tables[0];

 38             dataGridView1.DataSource = dt;

 39         }

 40         private void setParentDeptIdCellValue()//设置parentDeptID字段的显示值

 41         {

 42             for (int i = 0; i < dataGridView1.Rows.Count; i++)

 43             {

 44                 string cellValue = dataGridView1.Rows[i].Cells["parentDeptID"].Value.ToString();

 45                 DataRow[] dr = ds.Tables[0].Select("DeptID=" + cellValue + "");//在dt中查找包含该值的行,这里的行数只会是1,因为此字段为主键

 46                 DataTable dtt = new DataTable();

 47                 dtt = ds.Tables[0].Clone();

 48                 dtt.ImportRow(dr[0]);

 49                 string DeptNamevalue = dtt.Rows[0]["DeptName"].ToString();//取得结果行中Name字段的值, 

 50                 string cellText = cellValue.Trim() + ":" + DeptNamevalue;//字段拼接

 51                 DataGridViewCell cell = dataGridView1.Rows[i].Cells["parentDeptID"];//创建DataGridViewCell对象,赋值为当前单元格

 52                 cell.Value = cellText;//为对象赋值

 53             }

 54         }

 55         private void findComBoBoxValue()//为下拉列表填充内容

 56         {

 57             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

 58             {

 59                 string DeptID = ds.Tables[0].Rows[i]["DeptID"].ToString().Trim();

 60                 string DeptValue = ds.Tables[0].Rows[i]["DeptName"].ToString().Trim();

 61                 string parDeptID = ds.Tables[0].Rows[i]["PARENTDEPTID"].ToString().Trim();

 62                 string cbbText = DeptID + ":" + DeptValue;

 63                 cbb1.Items.Add(cbbText);

 64                 cbb1.DropDownStyle = ComboBoxStyle.DropDownList;//设置只允许选择,不允许编辑 

 65             }

 66         }       

 67 

 68         private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)

 69         {

 70             find();

 71             setParentDeptIdCellValue();

 72         }

 73         private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)

 74         {

 75             ((DataTable)dataGridView1.DataSource).Rows.Add(dt.NewRow());

 76             dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["DeptID"];

 77             dataGridView1.BeginEdit(false);

 78         }

 79         private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)

 80         {

 81             int index = dataGridView1.CurrentCell.RowIndex;

 82             string value = dataGridView1.Rows[index].Cells["DeptName"].Value.ToString();

 83             bool yesNo = appcode.others.showMessage_Yes_No("是否删除名称为:\"" + value + "\"的记录?删除后,将不可恢复!");

 84             if (yesNo)

 85             {

 86                 dataGridView1.Rows.RemoveAt(index);

 87                 int i = appcode.others.UpdateByDataSet(ds, "HrDept", appcode.others.connString());

 88                 if (i != -1)

 89                 {

 90                     panel1.Focus();

 91                 }

 92             }

 93         }

 94         private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)

 95         {

 96             panel1.Focus();

 97             if (ds.HasChanges())

 98             { 

 99                     for (int k = 0; k < dataGridView1.Rows.Count; k++)//保存时先循环整个表格

100                     {

101                         string cellvalue = dataGridView1["parentDeptID", k].Value.ToString().Trim();//取得当前行的parentDeptID字段的值

102                         object deptID = dataGridView1["deptID", k].Value;//取得当前行deptID字段值

103                         if (deptID==null||deptID.ToString()=="")//deptID字段是主键,不可能为空

104                         {

105                             ds.Tables[0].Rows.RemoveAt(k);

106                             break;//如果deptID字段为空,就证明此行是新添加的行,不予下面的操作

107                         }

108                         int index = cellvalue.IndexOf(':');//取得冒号的索引

109                         // string result = cellvalue.Remove(0, index);

110                         string result1 = cellvalue.Remove(index);//删除冒号索引以后的字符

111                         ds.Tables[0].Rows[k]["parentDeptID"] = result1;//修改ds的parentDeptID字段值

112                     }

113                     int i = appcode.others.UpdateByDataSet(ds, "HrDept", appcode.others.connString());

114                     if (i != -1)

115                     {

116                         appcode.others.showMessage_Successfully_saved();

117                         find();//重新执行一次刷新

118                         //findComBoBoxValue();

119                         setParentDeptIdCellValue();//重新设置parentDeptID字段显示的值

120                     } 

121             }

122         }

123         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)

124         {

125             this.Close();

126         }

127 

128         private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)

129         {

130 

131         }

132         private voiddataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)

133         {//单元格获得焦点时发生

134             int columnIndex = dataGridView1.Columns["parentDeptID"].Index;//取得要添加comboBox的列索引

135             if (e.ColumnIndex == columnIndex)//如果当前索引等于comboBox的列索引,这正是我要做的.

136             {

137                 displayComboBoxOnCell(dataGridView1, cbb1, e.RowIndex, e.ColumnIndex);//调用方法;

138                 dataGridView1.Controls.Add(cbb1);

139                 string cellvalue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim();

140                 for (int j = 0; j < cbb1.Items.Count; j++)

141                 {

142                     string value2 = cbb1.GetItemText(cbb1.Items[j]);

143                     if (value2 == cellvalue)

144                     {

145                         cbb1.SelectedIndex = j;

146                         break;

147                     }

148                 }

149             }

150         }

151         private void displayComboBoxOnCell(DataGridView dgv, ComboBox cbb, int rowIndex, int columnIndex)

152         {//将ComboBox显示到单元格

153             DataGridViewCell dgvCell = dgv.Rows[rowIndex].Cells[columnIndex];//获取当前单元格

154             Rectangle rect = dgv.GetCellDisplayRectangle(dgvCell.ColumnIndex, dgvCell.RowIndex, false);//取得单元格的界值,也就是位置

155             cbb.Location = rect.Location;//设置ComboBox位置为当前单元格的位置

156             cbb.Size = rect.Size;//大小相同

157             cbb.Visible = true;          //显示ComboBox

158         }

159 

160         private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)//当单元格失去焦点时

161         {

162             updataComboBoxCellValue(dataGridView1, cbb1, e.RowIndex, e.ColumnIndex);

163         }

164         private void updataComboBoxCellValue(DataGridView dgv, ComboBox cbb, int rowIndex, int columnIndex)

165         {//该方法将comboBox的选中内容赋值给Cell单元格

166             int colIndex = dataGridView1.Columns["parentDeptID"].Index;//取得要添加comboBox的列索引

167             if (columnIndex == colIndex)//如果该索引等于当前活动列的索引,说明是当前要更改值的列

168             {

169                 DataGridViewCell cell = dgv.Rows[rowIndex].Cells[colIndex];//将当前单元格赋值给Cell对象

170                 if (cell.Value != null && cell.Value.ToString() != cbb.Text)//如果不为空,并且不是原值

171                 {

172                     cell.Value = cbb.Text;//将cbb的值赋值给cell

173                 }

174                 cbb.Visible = false;//将comboBox设置隐藏

175             }

176         }

177     }

178 }

 

你可能感兴趣的:(datagridview)