本文介绍了在datagridview中动态创建列和添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,

             i have a query regarding add rows dynamically in datagridview .I need to have a datagridview which contains 4 columns as follows:

Column headers : Test1 ,Test2, Test3, Test4 (Creation of columns  is also dynamic)

Test1 ,Test2 and Test3 are of type DataGridViewComboBoxColumn  and Test4 of type DataGridViewTextBoxColumn


Example: Suppose i have 4 rows in datagridview everytime the 1 and last row(4) under Test1  should have combo box items same and 2nd and 3rd have same data


Test1            Test2                            Test3                           Test4

items(1,2)      items(5,5)                      items(6,7)                   first
items(4,5)      items(5,5)                      items(6,7)                   first
items(4,5)      items(5,5)                      items(6,7)                   first
items(1,2)      items(5,5)                      items(6,7)                   first

Please help me in this regard.

Thanks & Regards,
Sampath





我的尝试:



'DataGridView1.Rows.Add()



'Dim cmb As New DataGridViewComboBoxColumn()

'Dim cmb1 As New DataGridViewComboBoxColumn()

'Dim cmb2 As New DataGridViewComboBoxColumn()

'Dim txtbox As New DataGridViewTextBoxColumn()

'Dim cmbOthers As新的DataGridViewComboBoxColumn()



'对于i = 0到3



'cmb.HeaderText =选择数据

'cmb.Name =cmb

'cmb.MaxDropDownItems = 4

'cmb.Items.Add(1 / 3)

'cmb.Items.Add(1/2)

'DataGridView1.Columns.Add(cmb)



'cmb1.HeaderText =选择数据1

'cmb1.Name =cmb1

'cmb1.MaxDropDownItems = 4

'c mb1.Items.Add(True)

'cmb1.Items.Add(False)

'DataGridView1.Columns.Add(cmb1)



What I have tried:

'DataGridView1.Rows.Add()

'Dim cmb As New DataGridViewComboBoxColumn()
'Dim cmb1 As New DataGridViewComboBoxColumn()
'Dim cmb2 As New DataGridViewComboBoxColumn()
'Dim txtbox As New DataGridViewTextBoxColumn()
'Dim cmbOthers As New DataGridViewComboBoxColumn()

' For i = 0 To 3

'cmb.HeaderText = "Select Data"
'cmb.Name = "cmb"
'cmb.MaxDropDownItems = 4
'cmb.Items.Add("1/3")
'cmb.Items.Add("1/2")
'DataGridView1.Columns.Add(cmb)

'cmb1.HeaderText = "Select Data1"
'cmb1.Name = "cmb1"
'cmb1.MaxDropDownItems = 4
'cmb1.Items.Add("True")
'cmb1.Items.Add("False")
'DataGridView1.Columns.Add(cmb1)

推荐答案

Dim cmb(2) As DataGridViewComboBoxColumn
Dim txt As New DataGridViewTextBoxColumn


With DataGridView1
    For i As Int16 = 0 To 2
        cmb(i) = New DataGridViewComboBoxColumn
        cmb(i).HeaderText = "Data" & i
        .Columns.Add(cmb(i))

    Next
    .Columns.Add(txt)
    For i As Int16 = 0 To 3
        .Rows.Add()
    Next
End With


这篇关于在datagridview中动态创建列和添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 19:08