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

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

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

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

一、建了个窗体工程

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

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

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

如下图:简单。

二、上菜,代码

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

首先,窗体加载:

	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);
        }

其次,按钮保存表里的值,保存到Hashtable中。

	private void button1_Click(object sender, EventArgs e)
{
Hashtable HashFieldTex = new Hashtable();
int iNum = m_CAtDataGridView.Rows.Count; try
{
for (int i = 0; i < iNum; i++)
{
string Chos = m_CAtDataGridView.Rows[i].Cells[1].Value.ToString();
string Textrue = m_CAtDataGridView.Rows[i].Cells[2].Value.ToString();
HashFieldTex.Add(Chos, Textrue);
}
}
catch (Exception ex)
{
string mes = ex.Message;
}
}

最后,给第一列添加序列号。

	private void m_CAtDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
using (SolidBrush b = new SolidBrush(m_CAtDataGridView.RowHeadersDefaultCellStyle.ForeColor))
e.Graphics.DrawString((e.RowIndex + 1).ToString(),
e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y);
}

最后效果:不美,因为没有把写给列 设置宽度大小!

代码全景:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; namespace GridViewAndContorl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} 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);
} private void button1_Click(object sender, EventArgs e)
{
Hashtable HashFieldTex = new Hashtable();
int iNum = m_CAtDataGridView.Rows.Count; try
{
for (int i = 0; i < iNum; i++)
{
string Chos = m_CAtDataGridView.Rows[i].Cells[1].Value.ToString();
string Textrue = m_CAtDataGridView.Rows[i].Cells[2].Value.ToString();
HashFieldTex.Add(Chos, Textrue);
}
}
catch (Exception ex)
{
string mes = ex.Message;
}
} private void m_CAtDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
using (SolidBrush b = new SolidBrush(m_CAtDataGridView.RowHeadersDefaultCellStyle.ForeColor))
e.Graphics.DrawString((e.RowIndex + 1).ToString(),
e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y);
}
}
}

新手新学,见效就笑吧!

代码免费下载:http://download.csdn.net/detail/cartzhang/5741101

源码免分下载

最新文章

  1. mysql 创建存储过程报错
  2. js判断手机连接网络类型
  3. 类的构造器[constructor]_C#
  4. Android:简单的开场界面
  5. MapReduce自定义二次排序流程
  6. Python prettytable的使用方法
  7. Spring中 bean定义的parent属性机制的实现分析
  8. locate 不能使用
  9. Java IO - BufferedReader &amp; BufferedWriter
  10. Android学习路径图
  11. Oracle数据块损坏的恢复实例
  12. 为用户增加sudo权限(修改sudoers文件)
  13. IdentityServer(15)- 第三方快速入门和示例
  14. Java源码分析系列之HttpServletRequest源码分析
  15. 单例模式的优化之路(java)
  16. IT阅读——关于“业务”
  17. django 1.9.7 css控制模板样式
  18. [leetcode]Subsets II @ Python
  19. html5和css3学习历程
  20. stringstream快速实现String和int之间的转换

热门文章

  1. Androlid入门之文件系统操作(三)文件读写
  2. B3403 [Usaco2009 Open]Cow Line 直线上的牛 deque
  3. PCB 内网实现《OCR文字识别》实现逻辑
  4. Building a Space Station(bfs)
  5. Docker EE/Docker CE简介与版本规划
  6. Django之ORM的增删改查操作流程
  7. ACM_魔仙岛探险(深搜)
  8. C - Football
  9. Eclipse 每次ctrl-c ctrl-v 就变慢?
  10. div自动适应浏览器窗口水平和垂直居中