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 ...
随机推荐
- 怎样使用OpenShare部署和运营企业门户
怎样使用OpenShare部署和运营企业门户 这篇Blog是偏向企业内总体门户部署和运营的指南,是偏向总体管理和规划的.并非针对终端用户的OpenShare软件操作手冊,详细的操作能够上优酷看相关视频 ...
- mysql中 show table status 获取表信息
用法 mysql>show table status; mysql>show table status like 'esf_seller_history'\G; mysql>show ...
- 利用存储过程插入50W+数据
转自:https://www.aliyun.com/jiaocheng/1396184.html 首先,建立部门表和员工表: 部门表: create table dept( id int un ...
- yii的criteria的用法
Yii的Active Recorder包装了很多. 特别是把SQL中 把where,order,limit,IN/not IN,like等常用短句都包含进CDbCriteria这个类中去,这样整个代码 ...
- .Net Core中使用Quartz.Net Vue开即用的UI管理
Quartz.NET Quartz.Net 定制UI维护了常用作业添加.删除.修改.停止.启动功能,直接使用cron表达式设置作业执行间隔,有完整的日志记录. Quartz.NET是一个功能齐全的开源 ...
- DotNetCasClient加载失败问题分析
最近公司在接入整理单点登录方案的时候,选择了CAS方案,实际版本采用了4.0.当我们把服务端附属完毕,基于.NET平台Web版的客户端DotNetCasClient进行定制化修改后,在测试环境通过.然 ...
- Blender插件编写指南
前言 Blender插件是Blender的利器, 用户可以使用各种插件扩充Blender的功能. Blender Python插件以bpy.props, bpy.types.Operator, bpy ...
- SPOJ GSS1 & GSS3&挂了的GSS5
线段树然后yy一下,搞一搞. GSS1: 题意:求最大区间和. #include <cstdio> #include <algorithm> using namespace s ...
- 跳出双重for循环的案例__________跳出当前循环(continue out)
package com.etc.operator; public class demo { public static void main(String[] args) { // break out; ...
- 讲究地使用 List
本篇旨意在于讨论List的基本用法,不做全面讲解,仅仅涉及构造函数List.Add.RemoveAt 先看看这几个函数的代码 1.构造函数 static readonly T[] _emptyArra ...