1.设置DataGridView中的列是否为自动调整
dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
2.设置某列是否冻结(水平滚动DataGridView控件时,列是否移动)
dataGridView1.Columns[0].Frozen = true;
3.动态向DataGridView中插入数据
(1)从数据库中查询出数据
(2)循环遍历数据
(3)动态插入DataGridView中
OracleClass.Open(); OracleDataReader dtr = OracleClass.QueryForReader(sql_select); if (dtr != null) { while (dtr.Read()) { int CA_ID = int.Parse(dtr.GetOracleNumber(0).ToString()); int BASEID = int.Parse(dtr.GetOracleNumber(1).ToString()); String BASENAME = dtr.GetOracleString(2).ToString().Substring(0, dtr.GetOracleString(2).ToString().Length - 1); String CA_NAME = dtr.GetOracleString(3).ToString(); String CA_RULETYPE = dtr.GetOracleNumber(4).ToString(); if (CA_RULETYPE.Equals("1")) { CA_RULETYPE = "范围匹配类型"; } else if (CA_RULETYPE.Equals("2")) { CA_RULETYPE = "标准格式类型"; } else if (CA_RULETYPE.Equals("3")) { CA_RULETYPE = "固定格式类型"; } else if (CA_RULETYPE.Equals("4")) { CA_RULETYPE = "逻辑关联类型"; } else { CA_RULETYPE = "无"; } String CA_RULETEXT = dtr.GetOracleString(5).ToString(); if (CA_RULETEXT.Equals("Null") || (CA_RULETEXT.Equals("NULL"))) { CA_RULETEXT = ""; } String CA_RULETEXT2 = dtr.GetOracleString(6).ToString(); if (CA_RULETEXT2.Equals("Null") || (CA_RULETEXT2.Equals("NULL"))) { CA_RULETEXT2 = ""; } String CONTENT = dtr.GetOracleString(7).ToString(); if (CONTENT.Equals("Null") || CONTENT.Equals("NULL")) { CONTENT = ""; } object[] data = new object[] { CA_ID, BASEID, BASENAME, CA_NAME, CA_RULETYPE, CA_RULETEXT, CA_RULETEXT2, CONTENT }; this.dataGridView1.Rows.Add(false, CA_ID, BASEID, BASENAME, CA_NAME, CA_RULETYPE, CA_RULETEXT, CA_RULETEXT2, CONTENT); } OracleClass.Close();
4.在DataGridView的第一列加入checkbox并实现全选功能
(1)设置第一列为checkboxcolumn
(2)在列头加入checkbox
(3)实现全选/全不选功能
private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { for (int i = 0; i < dataGridView1.Rows.Count; i++) { this.dataGridView1.Rows[i].Cells[0].Value = true; } } else { for (int i = 0; i<dataGridView1.Rows.Count;i++ ) { this.dataGridView1.Rows[i].Cells[0].Value = false; } } }
5.清空DataGridView
dataGridView1.Rows.Clear();
6.点击左侧树节点,实现对DataGridView的动态查询
//树节点点击事件 private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node.Text == "应用规则类型" || e.Node.Text == "设备类型名称") { dataGridView1.Rows.Clear(); Load2(); } else { //点击类型节点 if (e.Node.Text == "范围匹配类型" || e.Node.Text == "标准格式类型" || e.Node.Text == "固定格式类型" || e.Node.Text == "逻辑关联类型") { dataGridView1.Rows.Clear(); OracleClass.Open(); int type = 0; if (e.Node.Text == "范围匹配类型") { type = 1; } else if (e.Node.Text == "标准格式类型") { type = 2; } else if (e.Node.Text == "固定格式类型") { type = 3; } else if (e.Node.Text == "逻辑关联类型") { type = 4; } String baseName = e.Node.Text; String sql_selectByTreeNode = "select * from DEVICE_ATTR t where t.CA_RULETYPE = " + type + " "; OracleDataReader dtr = OracleClass.QueryForReader(sql_selectByTreeNode); if (dtr != null) { while (dtr.Read()) { int CA_ID = int.Parse(dtr.GetOracleNumber(0).ToString()); int BASEID = int.Parse(dtr.GetOracleNumber(1).ToString()); String BASENAME = dtr.GetOracleString(2).ToString().Substring(0, dtr.GetOracleString(2).ToString().Length - 1); String CA_NAME = dtr.GetOracleString(3).ToString(); String CA_RULETYPE = dtr.GetOracleNumber(4).ToString(); if (CA_RULETYPE.Equals("1")) { CA_RULETYPE = "范围匹配类型"; } else if (CA_RULETYPE.Equals("2")) { CA_RULETYPE = "标准格式类型"; } else if (CA_RULETYPE.Equals("3")) { CA_RULETYPE = "固定格式类型"; } else if (CA_RULETYPE.Equals("4")) { CA_RULETYPE = "逻辑关联类型"; } else { CA_RULETYPE = "无"; } String CA_RULETEXT = dtr.GetOracleString(5).ToString(); if (CA_RULETEXT.Equals("Null") || CA_RULETEXT.Equals("NULL")) { CA_RULETEXT = ""; } String CA_RULETEXT2 = dtr.GetOracleString(6).ToString(); if (CA_RULETEXT2.Equals("Null") || CA_RULETEXT2.Equals("NULL")) { CA_RULETEXT2 = ""; } String CONTENT = dtr.GetOracleString(7).ToString(); if (CONTENT.Equals("Null") || CONTENT.Equals("NULL")) { CONTENT = ""; } object[] data = new object[] { CA_ID, BASEID, BASENAME, CA_NAME, CA_RULETYPE, CA_RULETEXT, CA_RULETEXT2, CONTENT }; this.dataGridView1.Rows.Add(false, CA_ID, BASEID, BASENAME, CA_NAME, CA_RULETYPE, CA_RULETEXT, CA_RULETEXT2, CONTENT); } OracleClass.Close(); } } //点击设备名称 else { dataGridView1.Rows.Clear(); OracleClass.Open(); String baseName = e.Node.Text; String sql_selectByTreeNode = "select * from DEVICE_ATTR t where t.BASENAME = " + " '" + baseName + "$' "; OracleDataReader dtr = OracleClass.QueryForReader(sql_selectByTreeNode); if (dtr != null) { while (dtr.Read()) { int CA_ID = int.Parse(dtr.GetOracleNumber(0).ToString()); int BASEID = int.Parse(dtr.GetOracleNumber(1).ToString()); String BASENAME = dtr.GetOracleString(2).ToString().Substring(0, dtr.GetOracleString(2).ToString().Length - 1); String CA_NAME = dtr.GetOracleString(3).ToString(); String CA_RULETYPE = dtr.GetOracleNumber(4).ToString(); if (CA_RULETYPE.Equals("1")) { CA_RULETYPE = "范围匹配类型"; } else if (CA_RULETYPE.Equals("2")) { CA_RULETYPE = "标准格式类型"; } else if (CA_RULETYPE.Equals("3")) { CA_RULETYPE = "固定格式类型"; } else if (CA_RULETYPE.Equals("4")) { CA_RULETYPE = "逻辑关联类型"; } else { CA_RULETYPE = "无"; } String CA_RULETEXT = dtr.GetOracleString(5).ToString(); if (CA_RULETEXT.Equals("Null") || CA_RULETEXT.Equals("NULL")) { CA_RULETEXT = ""; } String CA_RULETEXT2 = dtr.GetOracleString(6).ToString(); if (CA_RULETEXT2.Equals("Null") || CA_RULETEXT2.Equals("NULL")) { CA_RULETEXT2 = ""; } String CONTENT = dtr.GetOracleString(7).ToString(); if (CONTENT.Equals("Null") || CONTENT.Equals("NULL")) { CONTENT = ""; } object[] data = new object[] { CA_ID, BASEID, BASENAME, CA_NAME, CA_RULETYPE, CA_RULETEXT, CA_RULETEXT2, CONTENT }; this.dataGridView1.Rows.Add(false, CA_ID, BASEID, BASENAME, CA_NAME, CA_RULETYPE, CA_RULETEXT, CA_RULETEXT2, CONTENT); } OracleClass.Close(); } }
7.判断DataGridView中checkbox选中的个数
//判断checkbox中选中的个数 int count = 0; for (int i = 0; i<dataGridView1.Rows.Count;i++ ) { DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0]; if((Boolean)chk.EditedFormattedValue==true){ count++; } }
8.获取被选中checkbox
//获取被选中checkbox的数据 for (int i = 0; i<this.dataGridView1.Rows.Count;i++ ) { DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0]; if((Boolean)chk.EditedFormattedValue==true){ ruleId = int.Parse(this.dataGridView1.Rows[i].Cells["ruleID"].Value.ToString()); equipmentId = int.Parse(this.dataGridView1.Rows[i].Cells["equipmentId"].Value.ToString()); equipmentName = this.dataGridView1.Rows[i].Cells["equipmentName"].Value.ToString(); propertyName = this.dataGridView1.Rows[i].Cells["propertyName"].Value.ToString(); if (this.dataGridView1.Rows[i].Cells["ruleType"].Value.ToString() == "逻辑关联类型") { ruleType = 4; } else if (this.dataGridView1.Rows[i].Cells["ruleType"].Value.ToString() == "固定格式类型") { ruleType = 3; } else if (this.dataGridView1.Rows[i].Cells["ruleType"].Value.ToString() == "标准格式类型") { ruleType = 2; } else if (this.dataGridView1.Rows[i].Cells["ruleType"].Value.ToString() == "范围匹配类型") { ruleType = 1; } else if (this.dataGridView1.Rows[i].Cells["ruleType"].Value.ToString() == "无") { ruleType = -1; } ruleRequest = this.dataGridView1.Rows[i].Cells["ruleRequest"].Value.ToString(); ruleRequestOther = this.dataGridView1.Rows[i].Cells["ruleRequestOther"].Value.ToString(); ruleExplain = this.dataGridView1.Rows[i].Cells["ruleExplain"].Value.ToString(); break; } }