之前咩有做个界面的东西,更没有使用过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); }
|