1.设置列
DgvTools.InitDgvTextBoxDropDownColumn(this.dgProLine, DataGridViewContentAlignment.MiddleCenter, "No", "No", "No", "", 15, false, true, true, SystemColors.Info, true, ref ColumnState);
2.定义列弹框方法
DataGridViewTextBoxDropDownColumn DrColumn1 = this.dgProLine.Columns["No"] as DataGridViewTextBoxDropDownColumn; DrColumn1.ButtonCustomClick += new EventHandler<EventArgs>(DrColumn1_ButtonCustomClick);
3.列弹框实现
#region 选择生产线 /// <summary> /// 选择生产线 /// </summary> void DrColumn1_ButtonCustomClick(object sender, EventArgs e) { FrmProLines frm1 = new FrmProLines(true, ""); frm1.EventShowRow += new FrmProLines.GetDgRow(SelectProLines); frm1.ShowDialog(); } void SelectProLines(DataGridViewSelectedRowCollection rows) { try { //为当前行赋值 this.dgProLine.CurrentRow.Cells["_AutoID"].Value = rows[0].Cells["_AutoID"].Value; this.dgProLine.CurrentRow.Cells["No"].Value = rows[0].Cells["No"].Value; this.dgProLine.CurrentRow.Cells["Des"].Value = rows[0].Cells["Des"].Value; this.dgProLine.CurrentRow.Cells["Note"].Value = rows[0].Cells["Note"].Value; this.dgProLine.CurrentRow.Cells["UserCode"].Value = rows[0].Cells["UserCode"].Value; this.dgProLine.CurrentRow.Cells["create_time"].Value = rows[0].Cells["create_time"].Value; this.dgProLine.CurrentRow.Cells["update_time"].Value = rows[0].Cells["update_time"].Value; SendKeys.Send("{Right}"); if (dgProLine.CurrentRow.Index == dgProLine.Rows.Count - 1) { DataRow dr = dtProLine.NewRow(); //插入到指定索引的行的前面 dtProLine.Rows.Add(dr); dtProLine.Rows.Remove(dr); } } catch (Exception ex) { Msg.InfoBox(ex.Message); } } #endregion
4.设置datagridview列可编辑
#region 设置datagridview列状态 protected virtual void SetControlState(int nFlag) { bool flag = true; switch (nFlag) { case 0: //浏览状态 flag = true; break; case 1: //编辑状态 flag = false; break; } //datagridview列编辑设置 this.dgProLine.ReadOnly = nFlag == 1 ? false : true; //设置列为默认的ReadOnly属性。 foreach (DataGridViewColumn dgvCol in this.dgProLine.Columns) { dgvCol.ReadOnly = nFlag == 1 ? (bool)this.ColumnState[dgvCol.Name] : true; } //可添加列 this.dgProLine.AllowUserToAddRows = nFlag == 1 ? true : false; //this.dgDetails.au //根据编辑状态设置列 dgProLine.Columns["NO"].ReadOnly = nFlag == 1 ? false : true; } #endregion
注意:如果还是无法弹出筛选框,则在构造函数中添加下面的语句:
//还可能无法弹出筛选框? this.dgvDetails.AutoGenerateColumns = false;
浙公网安备 33010602011771号