添加列:
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中就简单多了。
我今天也正好用到这个功能,把代码给你,你看看吧。DataTable
dtcom
=
new
DataTable()
dtcom.Columns.Add("Value",
typeof(string))
dtcom.Columns.Add("Name",
typeof(string))
DataRow
drSex
drSex
=
dtcom.NewRow()
drSex[0]
=
"1"
drSex[1]
=
"a"
dtcom.Rows.Add(drSex)
drSex
=
dtcom.NewRow()
drSex[0]
=
"0"
drSex[1]
=
"b"
dtcom.Rows.Add(drSex)
//--
DataTable
dt
=
new
DataTable()
dt.Columns.Add("XH")
dt.Columns.Add("WLH")
dt.Columns.Add("WLM")
dt.Columns.Add("SL")
dt.Columns.Add("DJ")
dt.Columns.Add("ZZF",
typeof(string))
dt.Columns.Add("BZ")
DataRow
dr
=
dt.NewRow()
dr["XH"]
=
"1"
dr["WLH"]
=
"1234567"
dr["WLM"]
=
"德国汉堡制黄色书柜"
dr["SL"]
=
"12"
dr["DJ"]
=
"25610"
dr["ZZF"]
=
"0"
dr["BZ"]
=
"上墙安装"
DataRow
dr2
=
dt.NewRow()
dr2["XH"]
=
"2"
dr2["WLH"]
=
"1234567"
dr2["WLM"]
=
"德国汉堡制黄色书柜"
dr2["SL"]
=
"12"
dr2["DJ"]
=
"25610"
dr2["ZZF"]
=
"1"
dr2["BZ"]
=
"上墙安装"
DataRow
dr3
=
dt.NewRow()
dr3["XH"]
=
"3"
dr3["WLH"]
=
"1234567"
dr3["WLM"]
=
"德国汉堡制黄色书柜"
dr3["SL"]
=
"12"
dr3["DJ"]
=
"25610"
dr3["ZZF"]
=
"1"
dr3["BZ"]
=
"上墙安装"
dt.Rows.Add(dr)
dt.Rows.Add(dr2)
dt.Rows.Add(dr3)
DataGridViewComboBoxColumn
dcb
=
new
DataGridViewComboBoxColumn()
dcb.DataSource
=
dtcom
dcb.ValueMember
=
"Value"
dcb.DisplayMember
=
"Name"
dcb.DataPropertyName
=
"ZZF"
dcb.HeaderText
=
"ZZF"
this.dataGridView1.Columns.Insert(0,
dcb)
dcb.Name
=
"ZZF"
this.dataGridView1.DataSource
=
dt
//处理列无法点击排序
this.dataGridView1.EnableHeadersVisualStyles
=
false
for
(int
i
=
0
i
<
this.dataGridView1.Columns.Count
i++)
this.dataGridView1.Columns[i].SortMode
=
DataGridViewColumnSortMode.NotSortable
this.dataGridView1.Columns["ZZF"].DisplayIndex
=
5
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)