C#DataGridView控件怎么增加行、列?

时间:2024-01-03 18:10:41 买帖  | 投诉/举报

篇首语:本文由小编为大家整理,主要介绍了C#DataGridView控件怎么增加行、列?相关的知识,希望对你有一定的参考价值。

参考技术A

添加列:
DataGridViewColumn column = new DataGridViewColumn();
设置column属性如:column.HeaderText = "列名";
dgv1.columns.add(column);

添加行:
DataGridViewRow row = new DataGridViewRow();
设置row属性
dgv1.rows.add(row);

(一)。自适应窗体的代码:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1

public partial class Form1 : Form

//1.声明自适应类实例
AutoSizeFormClass asc = new AutoSizeFormClass();
public Form1()

InitializeComponent();
//如果加入"皮肤",则不能在Form1_Load中记录控件的大小和位置,因为有些控件如dataGridView的子控件还未完成
//而要在在Form1_SizeChanged中,第一次改变时,记录控件的大小和位置
this.skinEngine1.SkinFile = "EmeraldColor1.ssk";

//2. 为窗体添加Load事件,并在其方法Form1_Load中,调用类的初始化方法,记录窗体和其控件的初始位置和大小
private void Form1_Load(object sender, EventArgs e)

// asc.controllInitializeSize(this);

//3.为窗体添加SizeChanged事件,并在其方法Form1_SizeChanged中,调用类的自适应方法,完成自适应
private void Form1_SizeChanged(object sender, EventArgs e)

asc.controlAutoSize(this);
//  this.WindowState = (System.Windows.Forms.FormWindowState)(2);//记录完控件的初始位置和大小后,再最大化



(二)。自适应类的代码

using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1

class AutoSizeFormClass

//(1).声明结构,只记录窗体和其控件的初始位置和大小。
public struct controlRect

public int Left;
public int Top;
public int Width;
public int Height;

实际操作起来可能没有你想象的那么简单,你需要响应Form Resize之类的事件,然后根据事件,实时逐个调整控件的大小。在WPF中就简单多了。

DataGridView里怎么增加自定义的CheckBox控件

参考技术A 如果是设计的时候设置,添加列的时候ColumnType属性选择DataGridViewCheckBoxColumn如果是代码添加列,如下:DataGridViewColumnmyCol=newDataGridViewCheckBoxColumn();myGrid.Columns.Add(myCol);本回答被提问者采纳

以上是关于C#DataGridView控件怎么增加行、列?的主要内容,如果未能解决你的问题,请参考以下文章