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

    用NodeJS+Express开发WEB应用---第一篇 大漠穷秋2014-03-28 预热 为了对后面的内容理解更加透彻,推荐首先阅读下面这篇很好的文章: http://www.nodebeginn ...

  2. autofac的小知识点

    autofac 注入中i遇到的泛型传参问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  3. Element-UI 笔记

    1.表单验证 官官方地址 :     https://element.eleme.cn/#/zh-CN/component/form 使用rules进行表单字段验证  https://blog.csd ...

  4. 字符串转为JSON对象

    经常写字符串转为JSON对象,但是每次没有说一次就成功的,老是搞错属于哪个包的方法,遂记录一下 JSONObject.parseObject(str);这个方法需要导入包 com.alibaba.fa ...

  5. C# 处理oralce 时间

     addWorkSql.Append("to_date(' " + DateTime.Now.ToString("yyyy-MM-dd HH:ss:mm") + ...

  6. BZOJ 4057 状压DP

    思路: 状压一下 就完了... f[i]表示选了的集合为i 转移的时候判一判就好了.. //By SiriusRen #include <cstdio> #include <cstr ...

  7. POJ 3264 线段树 ST

    题意:给你一个数列,从中挑一段,问你这段数的最大值减最小值是多少. 思路:线段树. // by Sirius_Ren #include <cstdio> #include <algo ...

  8. chapter6 数据结构基础之习题 Parentheses Balance

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  9. 将DataTable某一列的值整体赋值给 另一个DataTable

    将 DataTable某一列的值,赋值给 另一个DataTable: DataSet _ds=bll.GetAllList(); //将要取其中一列 DataView view = _ds.Table ...

  10. create-react-app 引入ant design 及 使用 less

    全局引入: 第一步:全局安装 create-react-app npm install create-react-app -g 第二步:安装 yarn npm install -g yarn 第三步: ...