using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; namespace Pub_Class { /// /// 单网格可编辑 (除单据和列表) /// public class ClsGridViewFun { public DataGridView ogrdMain = new DataGridView(); private string sAllowEdit;//可编辑列 public bool EditStatus; public bool DisplayNoCol;//是否显示 序号列 public Int32 NoCol;//序号列 public bool Changelock; public string KeyItem; public bool NotAllowADDRow; public bool NotAllowDELRow; public char chr = Convert.ToChar(","); //建立 可编辑列 public void BuildAllowEditCols(string[] Cols) { Int64 i; string tStr; tStr = ""; for (i = 0; i < Cols.Length; i++) { if (i == 0) { tStr = Cols[i]; } else { tStr = tStr + "," + Cols[i]; } } sAllowEdit= tStr; } //列是否允许编辑 public bool FindAllowEditCol(Int64 Col) { Int64 i; string[] s; if (sAllowEdit.Trim() == "") return false; s=sAllowEdit.Split(chr ); for(i=0;i<=s.Length-1;i++) { if(Col==ClsPub.isInt(s[i])) return true; } return false; } //增行 public void Sub_AddRow() { if (ogrdMain.CurrentRow==null ) { ogrdMain.Rows.Add(); return; } if (GetNullRows() < 1) { ogrdMain.Rows.Insert(ogrdMain.CurrentRow.Index,1); if (ogrdMain.CurrentRow.Index != ogrdMain.RowCount-1) { DisplayCurRow(); RefreshNoCol(); } } } //获得 NULL 行 public Int64 GetNullRows() { Int32 r, c; Int32 lngRows; lngRows = 0; for (r = 0; r < ogrdMain.RowCount; r++) { if (ClsPub.isStrNull(ogrdMain.Rows[r].Cells[0].Value) == "*") { if (DisplayNoCol == true) { for (c = NoCol + 1; c < ogrdMain.ColumnCount; c++) { if (ClsPub.isStrNull(ogrdMain.Rows[r].Cells[c].Value) != "") { break; } } } else { for (c = 1; c < ogrdMain.ColumnCount; c++) { if (ClsPub.isStrNull(ogrdMain.Rows[r].Cells[c].Value) != "") { break; } } } if (c == ogrdMain.ColumnCount) lngRows = lngRows + 1; } } return lngRows; } //删除行 public void Sub_DelRow() { int Scqwghz; //原选中行 int Scqwglz; //原选中列 Scqwghz = ogrdMain.CurrentRow.Index; Scqwglz = ogrdMain.CurrentCell.ColumnIndex; DisplayCurRow(); Changelock = true; Changelock = false; ogrdMain.CurrentRow.DefaultCellStyle.BackColor = Color.Red; if (MessageBox.Show( "确定要删除当前记录?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { ogrdMain.CurrentRow.DefaultCellStyle.BackColor = Color.White; Changelock = true; Changelock = false; return; } ogrdMain.Rows.RemoveAt(ogrdMain.CurrentRow.Index); ogrdMain.Rows.Add(); Changelock = true; Changelock = false; RefreshNoCol(); EditStatus = false; } //显示当前行 public void DisplayCurRow() { Int64 Toprowte; Toprowte = 0; } //刷新行号 public void RefreshNoCol() { int i, m; i = 0; m = 0; for (i = 0; i < ogrdMain.RowCount; i++) { m = m + 1; ogrdMain.Rows[i].Cells[NoCol].Value = m.ToString().Trim(); } ogrdMain.Columns[NoCol].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } // } }