C#中的winform 程序怎样在datagridview 动态添加按钮行

C#中的winform 程序怎样在datagridview 动态添加按钮行,第1张

我今天也正好用到这个功能,把代码给你,你看看吧。

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

public Form1()

{

InitializeComponent()

this.Load += new EventHandler(Form1_Load)

}

List<string>strSourec = new List<string>{ "1", "2" }

void Form1_Load(object sender, EventArgs e)

{

foreach (string item in strSourec)

{

DataGridViewButtonColumn Column1 = new DataGridViewButtonColumn()

Column1.HeaderText = item

this.dataGridView1.Columns.Add(Column1)

}

DataGridViewRow dr = new DataGridViewRow()

for (int i = 0i <strSourec.Counti++)

{

DataGridViewButtonCell dgvbc = new DataGridViewButtonCell()

dgvbc.Value = strSourec[i]

dr.Cells.Add(dgvbc)

}

dataGridView1.Rows.Add(dr)

this.dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown)

}

void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

{

if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null) return

MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString())

}

参考:

http://zhidao.baidu.com/link?url=JImY3prv3dVBJlYSnTOIenUJnx6xyRRVFZYpnmg_flu755xA7RgADZtRswoKr1QMcCjClxWjmbHtRgsyFnZYya

不明白的再私信我


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11403894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存