C#Winform的DataGridView控件使用详解1—七种DataGridViewColumn类型使用方法


在展示和处理二维数据时,我们常常会想起Excel。但是使用Excel自带的各种函数处理较为繁琐的步骤时显得略显吃力,如果使用Excel自带宏编程,可能会对VB语言不甚熟悉或是感觉不nice。这个时候,熟悉C#编程语言的你,就需要用到DataGridView控件,设计一款二维数据处理利器。 此处,我们开始进入对DataGridView摸索学习的环节…

DataGirdView控件Column类型

拿到一个空白的任人蹂躏的DataGridView控件,我们开始开始敲动键盘开始我们的设计之旅~
在这里插入图片描述
根据以上操作步骤,我们就能很轻松完成一个个列的手动创建。这个时候,你会发现,我们在添加列的时候,列【类型】下垃框中竟然有足足6种类型,这可比我们Excel中常用的多了。这里,让我们研究下各种不同类型的列有何不同表现和用途。
在这里插入图片描述

DataGridViewButtonColumn列:按钮

按钮列就等同于我们的button控件,button控件能完成的,它都能完成。比如button的Click事件,我们可以通过datagridview1_CellClick()来判断和完成。
在这里插入图片描述
这里的第2、3列,我们用到的就是DataGridViewButtonColumn对象。这里实现了对文件的移除和查看功能。

// 文件一览框内,点击"移除"与"查看"按钮,对应事件
//dtFileroute为DataGridView控件的Name
private void dtFileroute_CellClick(object sender, DataGridViewCellEventArgs e)
{
	//通过点击对象的index来判断点击的是哪个按钮
    if (e.ColumnIndex == 1)// 点击的是"移除"按钮 
    {
        this.dtFileroute.Rows.RemoveAt(e.RowIndex);//删除改行
    }
    // 点击的是"查看"按钮
    if (e.ColumnIndex == 2)
    {
        try
        {
            string path = this.dtFileroute[0, e.RowIndex].Value.ToString();//获取第一列文本框中文件地址
            // 将选中查看的对象在TextBox中显示
            //OpenTxt()将文本文件加载到一个TextBox控件中显示
            OpenTxt(path, Convert.ToInt16(e.RowIndex));//传入文件全路径,打开显示在txtVer中
            //handle()将TextBox中内容在datagridview1中展示出来
            handle();//将txt文件处理成表格
        }
        catch
        {
        	MessageBox.Show("文件打开失败!");
        	return;
        }
    }
}

以上OpenTxt()和handle()就不进行代码展示了,如果需要源代码可以去本人上传资源《高铁有砟轨道大养机数据报表转换工具》中下载研究。我们点击【查看】按钮的结果如下:
在这里插入图片描述

DataGridViewCheckBoxColumn列:复选框

复选框列等同于我们的CheckBox控件,我们可以在该列内进行勾选和取消勾选操作。

//给DataGridViewCheckBoxCell对象赋值为true(勾选)和false(取消勾选)
((DataGridViewCheckBoxCell)this.dataGridView1.Rows[0].Cells[1]).Value = true;
//判断是否被勾选,isOK == "true"勾选 isOK == "false"不勾选
string isOK = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString();

DataGridViewComboBoxColumn列:下拉框

下拉框列中,我们可以手动或者代码添加多个下拉的Item。这里,我们应该关心的是怎么添加和删除Item、怎么获取或者设置选中的Item对象。如果需要实现ComboBox的SelectChange事件,我们可以使用datagridview_CellValueChanged()来进行实现。

//添加下拉Item对象
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Items.Add("Item1");
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Items.Add("Item2");
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Items.Add("Item3");
//按照Item的text来删除下拉对象
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Items.Remove("Item2");
//按照Item集合下标删除下拉对象
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Items.RemoveAt(1);
//给ComboBox赋予默认值,通过指定Item集合下标赋值
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Value = ((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Items[1];
//可以直接给ComboBox单元格赋予值,该值必须为下拉框中包含的对象
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Value = "Item1";
//赋值时,赋值对象不包含在下拉框集合中,会提示异常
((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Value = "hhh";//异常
//获取ComboBOX单元格选中的值
string item = ((DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[2]).Value.ToString();

DataGridViewImageColumn列:图片

图片列对象用于存储图片对象,但由于控件限制,我们添加分辨率太高的图片就会显示为X,所以为了美观,我们要设置缩略图模式。

//给ImageCell对象赋予图片的值,可以与imageList1控件关联
this.dataGridView1.Rows[0].Cells[3].Value = imageList1.Images[0];
//设置缩略图模式
this.dataGridView1.Rows[0].Cells[3]).ImageLayout = DataGridViewImageCellLayout.Zoom; 

DataGridViewLinkColumn列:链接

链接列下cell.value可为网址对象,类似于Office中的超链接。但如果不用于超链接功能,也能实现类似于不可点击修改的Textcell功能。

//给cell对象赋值网址
this.dataGridView1.Rows[0].Cells[4].Value = "https://mp.csdn.net";
//使用"iexplore.exe"调用网址
System.Diagnostics.Process.Start("iexplore.exe",this.dataGridView1.Rows[0].Cells[4].Value.ToString());

DataGridViewTextBoxColumn列:文本框

这是我们新建DataGridView列时的默认对象,常用于展示一系列字符串对象。为完成数据展示功能,可新建类似Excel表格对象,完成数据输入。

//方式一:给TextCell对象赋予值
this.dataGridView1.Rows[0].Cells[5].Value = "字符串值";
//方式二:给TextCell对象赋予值
this.dataGridView1[5,0].Value = "字符串值";
  • 61
    点赞
  • 369
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
C# 可视化 WinForm 中,可以使用 DataGridView 控件来显示数据,并且可以实现分页功能。下面是一个简单的实现分页功能的示例代码: 首先,在窗体中添加一个 DataGridView 控件和两个 Button 控件,一个用于上一页,一个用于下一页。然后在窗体的 Load 事件中,初始化 DataGridView 控件并设置数据源: ``` private void Form1_Load(object sender, EventArgs e) { // 初始化 DataGridView 控件 dataGridView1.AutoGenerateColumns = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.MultiSelect = false; dataGridView1.ReadOnly = true; // 设置数据源 dataGridView1.DataSource = GetData(1, pageSize); } ``` 其中,GetData() 方法用于获取数据源,第一个参数表示当前页码,第二个参数表示每页显示的数据条数。这个方法需要根据具体的业务逻辑来实现。 接下来,实现上一页和下一页的按钮点击事件: ``` private void btnPrev_Click(object sender, EventArgs e) { if (currentPage > 1) { currentPage--; dataGridView1.DataSource = GetData(currentPage, pageSize); } } private void btnNext_Click(object sender, EventArgs e) { if (currentPage < totalPages) { currentPage++; dataGridView1.DataSource = GetData(currentPage, pageSize); } } ``` 其中,currentPage 表示当前页码,totalPages 表示总页数。在点击上一页和下一页按钮时,需要判断当前是否到达了第一页或最后一页,如果没有,则更新当前页码并重新设置数据源。 最后,需要根据总数据条数和每页显示的数据条数计算总页数,并在窗体中显示出来: ``` private void DisplayPageInfo() { // 计算总页数 int totalCount = GetTotalCount(); totalPages = (int)Math.Ceiling((double)totalCount / pageSize); // 显示当前页码和总页数 lblPageInfo.Text = string.Format("第 {0} 页,共 {1} 页", currentPage, totalPages); } ``` 其中,GetTotalCount() 方法用于获取总数据条数,需要根据具体的业务逻辑来实现。 完整的代码示例: ``` public partial class Form1 : Form { private int pageSize = 10; // 每页显示的数据条数 private int currentPage = 1; // 当前页码 private int totalPages = 0; // 总页数 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 初始化 DataGridView 控件 dataGridView1.AutoGenerateColumns = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.MultiSelect = false; dataGridView1.ReadOnly = true; // 设置数据源 dataGridView1.DataSource = GetData(1, pageSize); // 显示当前页码和总页数 DisplayPageInfo(); } private void btnPrev_Click(object sender, EventArgs e) { if (currentPage > 1) { currentPage--; dataGridView1.DataSource = GetData(currentPage, pageSize); DisplayPageInfo(); } } private void btnNext_Click(object sender, EventArgs e) { if (currentPage < totalPages) { currentPage++; dataGridView1.DataSource = GetData(currentPage, pageSize); DisplayPageInfo(); } } private void DisplayPageInfo() { // 计算总页数 int totalCount = GetTotalCount(); totalPages = (int)Math.Ceiling((double)totalCount / pageSize); // 显示当前页码和总页数 lblPageInfo.Text = string.Format("第 {0} 页,共 {1} 页", currentPage, totalPages); } private DataTable GetData(int pageNum, int pageSize) { // 根据当前页码和每页显示的数据条数获取数据源 // 这里只是一个示例,需要根据具体的业务逻辑来实现 DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Age", typeof(int)); for (int i = 1; i <= pageSize; i++) { int id = (pageNum - 1) * pageSize + i; dt.Rows.Add(id, "Name" + id, 20 + i); } return dt; } private int GetTotalCount() { // 获取总数据条数 // 这里只是一个示例,需要根据具体的业务逻辑来实现 return 100; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ping_Kingzero

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值