关闭

C# DataGridView 使用

发表于:2013-7-17 10:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:cartzhang    来源:51Testing软件测试网采编

  之前咩有做个界面的东西,更没有使用过DataGirdView 这个控件。

  现在本来是准备用DeV呢,结果发现我的DEV没有注册,只好暂时用这个DataGridView来替代使用了。

  我现在要是设置两列多行的,一列是属性字段名称值,一列是添加了Combox,下拉表来选择对应的纹理文件路径。

  经过各种搜索查询,终于基本搞定能用。

  一、建了个窗体工程

  给工程来个名字,叫GridViewAndControl,然后来个工具箱里的DataGridView,起来个名字叫m_CAtDataGridView,然后加了个按钮,

  用来保存你选择后对应属性名称和下拉框选择项。

  先说明一下,保存的时候用Hashtable,你懂的,也就是属性名称字段不能重复,否则作为key,是不能存到Hashtable中的啊。

  如下图:简单。

  二、上菜,代码

  有三个事件,窗体加载事件,Form1_Load; 按钮保存,button_click事件;还加了一个添加序号的RowPostPaint,这个网上找到。

  首先,窗体加载:

<SPAN style="WHITE-SPACE: pre"> </SPAN>private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
            m_CAtDataGridView.Columns.Insert(0, newColumn);
            newColumn.HeaderText = "选择";           
            DataGridViewTextBoxColumn Texture = new DataGridViewTextBoxColumn();
            Texture.HeaderText = "字段";
            m_CAtDataGridView.Columns.Insert(1, Texture);
            //
            DataGridViewComboBoxColumn dcob = new DataGridViewComboBoxColumn();
            m_CAtDataGridView.Columns.Insert(2, dcob);
            dcob.HeaderText = "纹理路径";           
            dcob.Items.AddRange(new string[] { "Test1", "Test2", "Test3", "Test4" });


            //foreach (string field in table_Field)
            for (int i = 0; i < 5;i++ )
            {
                DataGridViewRow newrow = new DataGridViewRow();
                newrow.CreateCells(m_CAtDataGridView);
                newrow.Cells[2].Value = "Test2";// 设置默认值
                newrow.Cells[0].Value = true;
                newrow.Cells[1].Value = "t" + i.ToString();
                m_CAtDataGridView.Rows.Add(newrow);
            }           
            dcob.Selected = true;
            // 不显示新添加行
            m_CAtDataGridView.AllowUserToAddRows = false;
            m_CAtDataGridView.AutoSize = false;
            //m_CAtDataGridView.RowHeadersVisible = false;
            // 行颜色变化
            m_CAtDataGridView.RowsDefaultCellStyle.BackColor = Color.FromArgb(255, 90, 0);
            m_CAtDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(50, 205, 50);
            m_CAtDataGridView.GridColor = Color.FromArgb(16, 139, 87);
        }

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号