C# DataGridView 使用
之前咩有做个界面的东西,更没有使用过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 使用的更多相关文章
- [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict
一 需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...
- [Winform] DataGridView 总结(FAQ)
Q1. 如何使单元格不可编辑? A:设置 ReadOnly 属性,可以设置的对象包括 DataGridViewRow(行).DataGridViewColumn(列).DataGridViewCel ...
- [Winform] DataGridView 中 DataGridViewComboBox 的可编辑
在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑: 故作如下处 ...
- c#datagridview
//保证显示当前活动单元格 this.Invoke(new Action(() => { dataGridView1.CurrentCell = dataGridView1.Rows[index ...
- DataGridView 在下拉框添加下来事件
DataGridView中有一种下拉框式的列,给这个列添加下拉事件时需要转化一下才可以绑定下拉事件 /// <summary> /// 服务类型 /// </summary> ...
- 设置DataGridView的某个单元格为ComboBox
怎么将DataGridView的 某个单元格设为ComboBox的样式而不是整列都改变样式? 1.最简单的方法:利用DataGridView提供的DataGridViewComboBoxCell. 写 ...
- 用DataGridView导入TXT文件,并导出为XLS文件
使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据.也可以导出.txt,.xls等格式的文件.今天我们就先介绍一下用DataGridView把导入txt文件,导出x ...
- 图解DataGridView编辑列
WinForm中DataGridView功能强大,除了可以自动绑定数据源外,还可以根据需求编辑列.下面以截图说明添加编辑列的步骤(HoverTreeSCJ 项目实际界面). 1.选择DataGridV ...
- datagridview 单元格格式转换注意
datagridview 单元格内容进行比较时要注意正确写法要用强制转换,否则出错Convert.ToString(grd_order.SelectedRows[0].Cells[1].Value)= ...
- C# DataGridView中指定的单元格不能编辑
注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. ReadOnly属性的使用 DataGridView内所有的单元格不能编辑 当DataGridView.Re ...
随机推荐
- HDU - 3622 Bomb Game(二分+2-SAT)
题目大意:玩一个放炸弹游戏,有N次放炸弹的机会,每次放炸弹时,你都有两个位置能够选择.问怎样放炸弹,能使爆炸的炸弹的半径的最小值最大(炸弹爆炸半径能够控制,可是爆炸形成的圈不能有重叠部分) 解题思路: ...
- luogu2024 食物链
题目大意 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B吃 C,C 吃 A.现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种,但是我们并 ...
- luogu3382【模板】三分法
给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 看代码即可. #include <cstdio> #include < ...
- linux下获取按键响应事件【转】
本文转载自:https://my.oschina.net/u/157503/blog/91548 1.问题 通过一个死循环将读取键盘对应的设备文件将触发键盘事件在屏幕上打印出来,按esc退出程序 代码 ...
- 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分
题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...
- PCB Genesis加邮票孔(弧形连接位宽度校正)实现算法
采用弧形作为加接位,当两边距离较远时,会造成连接位变窄,由于之前算法是基于连接位间距做为半径画弧, 必然存在这个缺陷,这边做少许的改进解决此问题. 现将几个种增加孤形连接位的图形对比如下: 一.两边外 ...
- 原生JS---6
原生js学习笔记6——事件 事件对象 鼠标事件 event.clientX在可视区中,鼠标点击的x坐标 event.clientY在可视区中,鼠标点击的y坐标 示例: <!DOCTYPE htm ...
- HTML 14 JS事件
一 :什么是事件 发生的某一件事:触发特定的条件,完成某一项功能 二:学习的目的 在特定的条件下,完成特定的功能 条件满足的情况下,系统会自动执行 ( 回调 ) 绑定的方法 学习要点: 1.事件的两种 ...
- Python 37 基于多线程实现套接字 、gevent 、单线程下实现并发的套接字通信
一:基于多线程实现套接字 可添加多个客户端 from socket import * from threading import Thread def comunicate(conn): while ...
- VHDL之concurrent之block
1 Simple BLOCK The simple block represents only a way of partitioning the code. It allows concurrent ...