2

私はデータグリッドとデータベースをバインドしており、vb.netvs2012を使用しています。私の問題は、データベースのすべての列がデータグリッドに表示されるわけではないことです。ユーザーが表示したい列に依存します(表示する列のオプションのフォームにチェックリストボックスがあります)。また、データベースには1つの列が必要です分離されます (ここでは分割を使用します)。

ここに私のサンプルデータベースがあります:

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

そのサンプルでは、​​名前の列を毎に区切る必要があります,(私はすでにそれを解決しました)

これは、 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

期待される出力:

ユーザーが表示する列を 2 つ選択した場合

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

前もって感謝します!これには役立つアイデアが必要です。

4

1 に答える 1

0

これが、データベースで非スカラー値を使用すべきではない理由です...必要な「列」の数がわからない場合は、キーと値のペアを使用できます。

独自の列に各値がある場合は、適切な列を非表示にすることができます

于 2014-02-23T02:02:38.517 に答える