日期:2014-05-17  浏览次数:21061 次

C# DataGridView控件的常用操作

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 == "逻辑关联类型"