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

源码免分下载

C# DataGridView 使用的更多相关文章

  1. [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict

    一  需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...

  2. [Winform] DataGridView 总结(FAQ)

    Q1.  如何使单元格不可编辑? A:设置 ReadOnly 属性,可以设置的对象包括 DataGridViewRow(行).DataGridViewColumn(列).DataGridViewCel ...

  3. [Winform] DataGridView 中 DataGridViewComboBox 的可编辑

    在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑: 故作如下处 ...

  4. c#datagridview

    //保证显示当前活动单元格 this.Invoke(new Action(() => { dataGridView1.CurrentCell = dataGridView1.Rows[index ...

  5. DataGridView 在下拉框添加下来事件

    DataGridView中有一种下拉框式的列,给这个列添加下拉事件时需要转化一下才可以绑定下拉事件 /// <summary> /// 服务类型 /// </summary> ...

  6. 设置DataGridView的某个单元格为ComboBox

    怎么将DataGridView的 某个单元格设为ComboBox的样式而不是整列都改变样式? 1.最简单的方法:利用DataGridView提供的DataGridViewComboBoxCell. 写 ...

  7. 用DataGridView导入TXT文件,并导出为XLS文件

    使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据.也可以导出.txt,.xls等格式的文件.今天我们就先介绍一下用DataGridView把导入txt文件,导出x ...

  8. 图解DataGridView编辑列

    WinForm中DataGridView功能强大,除了可以自动绑定数据源外,还可以根据需求编辑列.下面以截图说明添加编辑列的步骤(HoverTreeSCJ 项目实际界面). 1.选择DataGridV ...

  9. datagridview 单元格格式转换注意

    datagridview 单元格内容进行比较时要注意正确写法要用强制转换,否则出错Convert.ToString(grd_order.SelectedRows[0].Cells[1].Value)= ...

  10. C# DataGridView中指定的单元格不能编辑

    注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. ReadOnly属性的使用 DataGridView内所有的单元格不能编辑 当DataGridView.Re ...

随机推荐

  1. GitHub上排名前100的Android开源库介绍

    GitHub上排名前100的Android开源库介绍 文章来源: http://www.open-open.com/news/view/1587067#6734290-qzone-1-31660-bf ...

  2. 【POJ 3635】 Full Tank

    [题目链接] http://poj.org/problem?id=3635 [算法] 优先队列BFS 实现类似于堆优化dijkstra [代码] #include <algorithm> ...

  3. runC爆严重安全漏洞,主机可被攻击!使用容器的快打补丁

    runC 是 Docker,Kubernetes 等依赖容器的应用程序的底层容器运行时.此次爆出的严重安全漏洞可使攻击者以 root 身份在主机上执行任何命令. 容器的安全性一直是容器技术的一个短板. ...

  4. clone的rails目录下命令无效问题

    异常坑爹,在公司克隆自己的项目.然后在项目目录下rails s还有一大堆命令无效,提示 Usage: rails new APP_PATH [options]   找了半天总算找到解决办法了,在项目目 ...

  5. 洛谷P2916 [USACO08NOV]为母牛欢呼(最小生成树)

    P2916 [USACO08NOV]为母牛欢呼Cheering up the C… 题目描述 Farmer John has grown so lazy that he no longer wants ...

  6. python请求服务器时如何隐藏User-Agent

    本文结合上一篇文章“python利用有道翻译实现“语言翻译器”的功能”的实现代码,对其进行加工,实现请求服务器时隐藏User-Agent. python实现隐藏User-Agent的一般做法有两种: ...

  7. C99新增内容之复合文字(compound literal)

    前言: 最近在复习C,发现了一些新东西,例如:变长数组,复合文字,指针的兼容性等.今天先简单谈一下复合文字. 正文: 假如需要向带有一个int参量的函数传递一个值,您可以传递一个int变量,也可以传递 ...

  8. node js koa js严格模式

      当前为配置 非原创 引用于“得金” ### nodejs项目配置终端命令 1. 检查本地 nodejs 版本`$node -v` 如果版本低就升级 2. 安装 n 升级命令 `$npm insta ...

  9. .net core2.0 中使用DB First

    一.新建一个控制台测试项目 1.1.添加引用 1.2.修改项目文件 1.3.添加红框的内容 <ItemGroup> <DotNetCliToolReference Include=& ...

  10. nginx单机1w并发设置

    关闭keep-live,提高链接回收 keeplive_timeout 0; events{ worker_connections 1024; } more /proc/sys/net/core/so ...