博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Add UserControl into DataGridView
阅读量:6258 次
发布时间:2019-06-22

本文共 2029 字,大约阅读时间需要 6 分钟。

In order to solve this problems, you can try the following steps.

1.Create a usercontrol and add some controls.

First, right click on the project name, then click "Add" and select "User Control... ". After that, drag the required controls into the "UserControl".

2.Try the following code in form1.cs.

public partial class Form1 : Form{    public Form1()    {        InitializeComponent();        dataGridView1.RowTemplate.Height = 35;        dataGridView1.AllowUserToAddRows = false;    }    private void Form1_Load(object sender, EventArgs e)    {        this.dataGridView1.DataSource = CreateData();        this.dataGridView1.Columns[0].Width = 125;        this.dataGridView1.Columns[2].Width = 125;        for (int i = 0; i < dataGridView1.RowCount; i++)        {            UserControl1 userControl1 = new UserControl1();            this.dataGridView1.Controls.Add(userControl1);            userControl1.Location = this.dataGridView1.GetCellDisplayRectangle(0, i, true).Location;            userControl1.Size = this.dataGridView1.GetCellDisplayRectangle(0, i, true).Size;            UserControl1 userControl2 = new UserControl1();            this.dataGridView1.Controls.Add(userControl2);            userControl2.Location = this.dataGridView1.GetCellDisplayRectangle(2, i, true).Location;            userControl2.Size = this.dataGridView1.GetCellDisplayRectangle(2, i, true).Size;        }    }    private DataTable CreateData()    {        DataTable T_Table1 = new DataTable();        T_Table1.Columns.AddRange(new DataColumn[] {            new DataColumn("UserControl1"),            new DataColumn("GenderID"),            new DataColumn("UserControl2"),            new DataColumn("GenderName"),        });        T_Table1.Rows.Add(new object[] { "", "1", "", "Man" });        T_Table1.Rows.Add(new object[] { "", "2", "", "Female" });        T_Table1.Rows.Add(new object[] { "", "3", "", "Secret" });        return T_Table1;    }}
View Code

The results as shown below.

转载于:https://www.cnblogs.com/jizhiqiliao/p/9936143.html

你可能感兴趣的文章
EF架构~为EF DbContext生成的实体添加注释(T5模板应用)
查看>>
【转】VLAN原理详解
查看>>
python --- json模块和pickle模块详解
查看>>
idea中artifacts、facets、modules是什么意思?
查看>>
FUCKED-BUG之临时对象的生死
查看>>
SP2 PRIME1 - Prime Generator
查看>>
创建和编辑 crontab 文件
查看>>
钉钉发消息
查看>>
20172309_《程序设计与数据结构(下)》_课堂测试修改报告。
查看>>
(二十九)方法调用之解析
查看>>
Springboot文件上传与下载
查看>>
Activity与Fragment数据传递之Fragment从Activity获取数据 分类: ...
查看>>
记一次小的51CTO聚会
查看>>
架構設計_案例說明_by 高老師
查看>>
VR內容開發(PAGE-2)
查看>>
飞舞的蝴蝶
查看>>
Linux环境下C语言模拟内存负载测试
查看>>
专栏《轻松玩转ELK海量可视化日志分析系统》已完结【附代码下载】
查看>>
新手IT人员,如何找到适合自己专业书籍的6个问题?
查看>>
初学者学习linux运维的几个问题及老鸟建议
查看>>