This is a migrated thread and some comments may be shown as answers.

ComboBox and Validation

9 Answers 271 Views
GridView
This is a migrated thread and some comments may be shown as answers.
mampus
Top achievements
Rank 1
mampus asked on 11 Oct 2010, 04:18 AM
Hello guys,
I have 2 problems:
1. I'm try to use gridViewComboBox, but i got this problem:
- how can I put my item(s) collection without connect throught the database??

2. I try to making a validation for each cells using the cellvalidating events, the problem is, why if I create a new row, the validation doesn't work???
here my codes:

    Private Sub TabelBuku_CellValidating(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles TabelBuku.CellValidating
        Dim kolom = TryCast(e.Column, GridViewDataColumn)
        If ((TypeOf e.Row Is GridViewDataRowInfo) AndAlso (kolom IsNot Nothing) AndAlso (kolom.FieldName = "IdBuku")) Then
            Dim baris = DirectCast(e.Row, GridViewDataRowInfo)
            Dim nilai = DirectCast(e.Value, String)
            If (String.IsNullOrEmpty(nilai)) Then
                e.Cancel = True
                lblKesalahan.BackColor = Color.LightYellow
                lblKesalahan.ForeColor = Color.Red
                lblKesalahan.Text = "ID Buku Harus Diisi!!"
                baris.ErrorText = "ID Buku Harus Diisi!!"
            Else
                baris.ErrorText = String.Empty
                lblKesalahan.BackColor = Color.Transparent
                lblKesalahan.ForeColor = Color.LimeGreen
                lblKesalahan.Text = "EDITOR DATA BUKU"
            End If
        End If
    End Sub
 

9 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 11 Oct 2010, 06:45 AM
Hello Mampus,

1. If by that question you mean how to set the data source for the GridViewComboBox column, i would suggest Creating the column on DataBindingComplete and set the DataSource for the column there, and the necessary relations, like this:
Private Sub RadGridView1_DataBindingComplete(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewBindingCompleteEventArgs) Handles RadGridView1.DataBindingComplete
    Dim grid = DirectCast(sender, RadGridView)
    Dim comboBoxColumn As New GridViewComboBoxColumn("ComboColumn")
    comboBoxColumn.DataSource = New Object() {"aaa", "bbb", "ccc"}
    comboBoxColumn.FieldName = "Id"
    comboBoxColumn.ValueMember = "Id"
    grid.Columns.Add(comboBoxColumn)
End Sub

And for the CellValidation, it was not working because of this check here: (TypeOf e.Row Is GridViewDataRowInfo), because when you are creating a new row, the type of the row is GridViewNewRowInfo, but in order to avoid checking for all the necessary types you the simplest way to do this is to change it to:
Private Sub RadGridView1_CellValidating(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles RadGridView1.CellValidating
        Dim kolom = TryCast(e.Column, GridViewDataColumn)
        If ((kolom IsNot Nothing) AndAlso (kolom.FieldName = "Name")) Then
 
            Dim nilai = DirectCast(e.Value, String)
            If (String.IsNullOrEmpty(nilai)) Then
                e.Cancel = True
                lblKesalahan.BackColor = Color.LightYellow
                lblKesalahan.ForeColor = Color.Red
                lblKesalahan.Text = "ID Buku Harus Diisi!!"
                e.Row.ErrorText = "ID Buku Harus Diisi!!"
            Else
                e.Row.ErrorText = String.Empty
                lblKesalahan.BackColor = Color.Transparent
                lblKesalahan.ForeColor = Color.LimeGreen
                lblKesalahan.Text = "EDITOR DATA BUKU"
            End If
        End If
    End Sub

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 11 Oct 2010, 02:34 PM
thanks for the answers, it's really helpfull...
i'm doing this way:
Dim kolom = TryCast(e.Column, GridViewDataColumn)
  Dim namaKolom As String
  If ((TypeOf e.Row Is GridViewDataRowInfo Or (TypeOf e.Row Is GridViewNewRowInfo)) AndAlso (kolom IsNot Nothing) AndAlso (kolom.FieldName = namaKolom)) Then
      Dim nilai = DirectCast(e.Value, String)
      If (String.IsNullOrEmpty(nilai)) Then
      e.cancel = True
          e.Row.ErrorText = "Tahun Terbit Harus Diisi!!"
          With lblKesalahan
              .BackColor = Color.LightYellow
              .ForeColor = Color.Red
              .Text = "Tahun Terbit Harus Diisi!!"
          End With
      Else
          e.Row.ErrorText = String.Empty
          With lblKesalahan
              .BackColor = Color.Transparent
              .ForeColor = Color.LimeGreen
              .Text = "Editor Data Buku"
          End With
      End If
  End If
0
Emanuel Varga
Top achievements
Rank 1
answered on 11 Oct 2010, 02:47 PM
Hello Mampus,

Glad to be able to help, if you have any more questions please just let me know, and if the question has been solved, please mark the question as answered, so that others can find the answers to their questions faster.

If you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 16 Oct 2010, 05:36 PM
hello there..
I still don't understand, how  add items to the gridviewComboBox...??
0
Emanuel Varga
Top achievements
Rank 1
answered on 16 Oct 2010, 05:56 PM
Hello Mampus,

Just set the DataSource for the GridViewComboBoxColumn when you are creating the column, or do you need something else?

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 17 Oct 2010, 02:52 AM
Hi...
yeah, i want to add some items to the gridviewcombobox...
example: i have colums name "Genre"....I want to add some items,  like "Pop", "Rock","Jazz"...
how i do that..??
I already tried like above, but it didn't Work...
 
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 17 Oct 2010, 08:32 AM
Hello again,

Please take a look at the following example:
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports Telerik.WinControls.UI
 
Partial Public Class Form1
    Inherits Form
    Private WithEvents RadGridView1 As RadGridView
    Public Sub New()
        InitializeComponent()
        Me.Controls.Add(InlineAssignHelper(RadGridView1, New RadGridView()))
        RadGridView1.Dock = DockStyle.Fill
 
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
        Me.RadGridView1.DataSource = New ProductsCollection(1000)
    End Sub
 
    Private Sub radGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As GridViewBindingCompleteEventArgs) Handles RadGridView1.DataBindingComplete
        'set the original columns visibility to false
        RadGridView1.Columns("BuyerId").IsVisible = False
 
        'create the new comboboxcolumn
        Dim column = New GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn")
        'set the data source for the combobox column
        column.DataSource = New BuyersCollection(10, 10)
        column.ValueMember = "Id"
        column.FieldName = "BuyerId"
        column.DisplayMember = "Name"
 
        Me.RadGridView1.Columns.Add(column)
        Me.RadGridView1.BestFitColumns()
    End Sub
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return value
    End Function
 
 
End Class
 
#Region "Helpers"
 
Public Class Product
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set(ByVal value As Integer)
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer
 
    Public Property BuyerId() As Integer
        Get
            Return m_BuyerId
        End Get
        Set(ByVal value As Integer)
            m_BuyerId = Value
        End Set
    End Property
    Private m_BuyerId As Integer
 
    Public Sub New(ByVal id As Integer, ByVal buyerId As Integer)
        Me.Id = id
        Me.BuyerId = buyerId
    End Sub
End Class
 
Public Class Buyer
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set(ByVal value As Integer)
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer
 
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
 
    Public Sub New(ByVal id As Integer, ByVal name As String)
        Me.Id = id
        Me.Name = name
    End Sub
End Class
 
Public Class ProductsCollection
    Inherits BindingList(Of Product)
    Public Sub New(ByVal noItems As Integer)
        For i As Integer = 0 To noItems - 1
            Me.Add(New Product(i, i + 10))
        Next
    End Sub
End Class
 
Public Class BuyersCollection
    Inherits BindingList(Of Buyer)
    Public Sub New(ByVal startIndex As Integer, ByVal lenght As Integer)
        For i As Integer = 0 To 9
            Me.Add(New Buyer(i + 10, "Buyer" + (i + 1).ToString()))
        Next
    End Sub
End Class
 
#End Region

Please let me know if you need something different, of if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 18 Oct 2010, 02:38 AM
hello there..
umm...i don't understand what's the region helper's doing..???
0
Emanuel Varga
Top achievements
Rank 1
answered on 18 Oct 2010, 06:56 AM
Hello again mampus, just creating the data source for the grid.

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
mampus
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
mampus
Top achievements
Rank 1
Share this question
or