vb.net datagridview 动态列和添加问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18889683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:08:02  来源:igfitidea点击:

datagridview dynamic columns and adding issues

vb.netdatagridview

提问by Unknownymous

I have a datagrid and a database bind together, I'm using vb.netand vs2012. My problem is not all columns in database will be displayed in datagrid, it depends on the user what column he wants to display(there is a check list box in form for option what column to display) and also there is one column in database must be separated (I use split here).

我有一个数据网格和一个数据库绑定在一起,我使用的是vb.netvs2012。我的问题不是数据库中的所有列都将显示在数据网格中,这取决于用户想要显示的列(表单中有一个复选框,用于选择要显示的列)并且数据库中必须有一列分开(我在这里使用 split )。

here is my sample db :

这是我的示例数据库:

  Name  |   col1    |   col2    |   col3    |
---------------------------------------------
aa,bb,cc|   111     |   111     |   111     |
dd,ee,ff|   222     |   222     |   222     |
gg,hh,ii|   333     |   333     |   333     |
---------------------------------------------

in that sample db NAME COLUMNmust be separated every ,(I already solved that)

在该示例中 db NAME COLUMN必须分开,(我已经解决了)

this is my sample code for adding column in data-grid and split-ted :

这是我在 data-grid 和 split-ted 中添加列的示例代码:

Dim cl1 As New DataGridViewTextBoxColumn
Dim cl2 As New DataGridViewTextBoxColumn
Dim cl3 As New DataGridViewTextBoxColumn

With cl1
    .HeaderText = "SplitName1"
    .Name = "sn1"
    .Width = 120
    .ReadOnly = True
End With

With cl2
    .HeaderText = "SplitName2"
    .Name = "sn2"
    .Width = 120
    .ReadOnly = True
End With

With cl3
    .HeaderText = "SplitName3"
    .Name = "sn3"
    .Width = 120
    .ReadOnly = True
End With

dg.Columns.Insert(0, cl1)
dg.Columns.Insert(1, cl2)
dg.Columns.Insert(2, cl3)

'dynamic column

Dim n As Integer = 3

   'here it count datatable columns if how many columns to make in datagrid 
   'I start in 1 because column 0 is NAME COLUMN

For colcnt As Integer = 1 To dt.Columns.Count - 1 'dt is datable
    Dim dgvtxt As New DataGridViewTextBoxColumn

    With dgvtxt
         .HeaderText = "Column" & colcnt.ToString
         .Name = "col" & colcnt.ToString
         .AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
         .ReadOnly = True
    End With

    dg.Columns.Insert(n, dgvtxt)
    n += 1
Next

'this will be the variables to store the split names
Dim _SName1 As String = Nothing
Dim _SName2 As String = Nothing
Dim _SName3 As String = Nothing

'for splitting column name

Dim charSeparatorsC() As Char = {","c}
Dim sampDataArray() As String

dg.Rows.Clear()

 'loop through records to get values

For counter As Integer = 0 To dt.Rows.Count - 1

'splitting procedure

sDataArray = dt2.Rows(counter)(0).ToString.Split(charSeparatorsC, StringSplitOptions.RemoveEmptyEntries)

'Load data on the sampDataArray to the Sample Name column variable
'If the array is not existing set an empty string

  Try
      _SName1 = sDataArray(0).ToString
  Catch ex As Exception
      _SName1 = ""
  End Try

  Try
      _SName2 = sDataArray(1).ToString
  Catch ex As Exception
      _SName2 = ""
  End Try

  Try
      _SName3 = sDataArray(2).ToString
  Catch ex As Exception
      _SName3 = ""
  End Try

     'Now this is my problem, how to add the dynamic selected columns 
     'from datable since the code below is specified column of a datagrid which is the cl1,cl2, and cl3.
     'My question is how to get the columns from datatable and add to datagrid(sample output below).
  dgRT.Rows.Add(_SName1, _SName2, _SName3)


Next

Expected output :

预期输出:

If the user selected two columns to display

如果用户选择了两列来显示

|SplitName1|SplitName2|SplitName3|col1|col2|
|------------------------------------------|
|  aa      |   bb     |   cc     |111 |111 |
|  dd      |   ee     |   ff     |222 |222 |
|  gg      |   hh     |   ii     |333 |333 |
|------------------------------------------|

Thank you in advance! I need a helping idea for this.

先感谢您!我需要一个对此有帮助的想法。

回答by Ishey4

This the the reason why you should not use non scalar values in a database... You can use key value pairs if you will not know how many 'columns' you will need.

这就是为什么你不应该在数据库中使用非标量值的原因......如果你不知道你需要多少“列”,你可以使用键值对。

If you had each value in its own column you can just hide the appropiate columns

如果您在自己的列中有每个值,则可以隐藏适当的列