2016-08-24 65 views
0

我想混合一个datagridview与2个控件类型的列(复选框& TextBox),数据来自一个存储过程,我也写这样我有一点灵活性。DataGridView,将一个类型列的单元格更改为另一个类型

在存储过程中

我返回一个空列作为在GridView一个“选择”列,但我想细胞转变到基于标准的其他类型时遇到问题......

我把问题与数据类型时,控制类型之间的转换,我已经试过alsort的diferent办法先转换值,控制第一,等等等等,但没有什么工作100%.....

目前我有SP在字段中返回字符串'False',然后用这个标准创建一个复选框.....它工作正常,但值仍然是一个sstring,即使在将其转换为布尔值之后,数据类型为也一个布尔值的复选框但价值是“字符串” ......这已经结束了我的头,我不知所措......

NewCntrl2 = New DataGridViewCheckBoxCell 
NewCntrl2.Value = Convert.ToBoolean(DGV.Cells(0).Value) 
NewCntrl2.ValueType = GetType(System.Boolean) 
DGV.Cells(0) = NewCntrl2 

这是代码转换textboxcolumncell一个复选框单元格

任何想法,为什么复选框的值仍然是一个字符串(“假”)......

与它现在做的事情是,当我处理cellclick事件,我不能检查问题或使用“不值”技术

---- EDIT取消选中该复选框----

这是我使用的子,它创建若干个不同类型的控件。

Public Sub posted_CreateControls() 
    Dim Cell_0 As String 
    Dim Cell_1 As String 
    Dim Cell_2 As String 
    Dim NewCntrl As DataGridViewButtonCell 
    Dim NewCntrl2 As DataGridViewCheckBoxCell 

    For Each Rw As DataGridViewRow In dgvPosted.Rows 

     With Rw 

      Cell_0 = .Cells(1).Value.ToString 
      Cell_1 = .Cells(2).Value.ToString 
      Cell_2 = .Cells(3).Value.ToString 


      'this column starts with a value 'False' returned by the SP, 
      'we dont want checkboxes on all rows and using false string was the only method 
      'i could find to easily do this 
      'if both assign and unassign id are present then we need a checkbox 
      Select Case Cell_0 
       Case String.Empty 
        .Cells(0).Value = String.Empty 

       Case Else 
        If Not Cell_1 = String.Empty Then 
         NewCntrl2 = New DataGridViewCheckBoxCell 
         NewCntrl2.Value = Convert.ToBoolean(.Cells(0).Value) 
         NewCntrl2.ValueType = GetType(System.Boolean) 
         .Cells(0) = NewCntrl2 
        Else 
         .Cells(0).Value = String.Empty 
        End If 

      End Select 

      'Create an Assign button for each row in columnindex 0(Assign) if ColumnIndex(2)(Edit) contains M, L or I 
      Select Case Cell_2 
       Case "M", "L", "I", "P" 
        NewCntrl = New DataGridViewButtonCell 
        .Cells(1) = NewCntrl 
        NewCntrl.Tag = Cell_0 
        NewCntrl.Value = "Assign" 
      End Select 

      'Create an UnAssign button in columnindex 1(UnAssign) if the value of columnindex 1(Unassign) is not empty 
      If Not Cell_1 = vbNullString Then 
       NewCntrl = New DataGridViewButtonCell 
       .Cells(2) = NewCntrl 
       NewCntrl.Tag = Cell_1 
       NewCntrl.Value = "UnAssign" 
      End If 

      'Create an Edit button on columnindex 2(Edit) if ColumnIndex 2(Edit) is not M, L or empty string 
      Select Case Cell_2 
       Case "M", "L", "P", vbNullString 
        'Do Nothing 
       Case Else 
        NewCntrl = New DataGridViewButtonCell 
        .Cells(3) = NewCntrl 
        NewCntrl.Tag = Cell_2 
        NewCntrl.Value = "Edit" 
      End Select 
     End With 
    Next 

    NewCntrl = Nothing 
End Sub 

这造成我多么希望他们的复选框,但值保持为这是造成cell.click事件失败,因为我正在尝试设置的复选框值为true和.value的=不串。值部分失败,因为某种原因值保持一个字符串,但该复选框值类型,其实是布尔.....

回答

0

而不是返回错误,请返回:

`select CAST(0 as bit) as somefield` 

将返回一个布尔领域。我还建议添加一个复选框列而不是添加单个单元格。

+0

我也试过,但我碰到问题就来了,当我需要从某些行删除复选框......不是所有的行应该有他们,我试过翻领..改变复选框细胞文本框细胞,但couldn” t找到一种方法来防止它引起的FormatValue错误。但是我没有我上面给的代码试试这个...我会尝试一下,看看它是否工作 –

+0

我仍与列方法去,处理cellformatting事件禁用/启用任何复选框/更改单元格样式到一个正常的文本框单元格。 – FloatingKiwi

+0

我需要的细胞是完全空白的,它不是一个标准表在这里居住并具有空行来分隔父行,每个父行有多个子行,他们也有子行。然而,不用担心表格的复杂性,我现在唯一的问题是,当我将单元格控件更改为另一种类型时,它的值类型(尽管正在更改)仍然保留其旧值....布尔类型如何包含值的字符串?.......没有错误,我想我只是错过了一些东西。 –

相关问题