ComboBox in DataGrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Tkatch

    #1

    ComboBox in DataGrid

    This is mostly for the fun of it. Just saw it in the FAQ and figured
    i'd quickly give it a shot.

    The syncfusion FAQ
    <URL:http://www.syncfusion. com/FAQ/WindowsForms/FAQ_c44c.aspx#q 480q>
    mentions:

    "5.9 How can I put a combobox in a column of a datagrid?

    There are several ways to go about this task. The simplest way involves
    adding a single combobox to the DataGrid.Contro ls, and then selectively
    displaying it as needed when a combobox cell becomes the currentcell.
    All the work is done in a few event handlers and no overrides or
    derived classes are necessary. This technique is discussed in Microsoft
    KB article Q323167."

    But the KB is missing. So, i decided to play with it.

    Open a form, put in a DataGrid, then use the following code:

    Dim CBox As New ComboBox

    Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim Table As New DataTable
    Dim Counter As Integer

    For Counter = 0 To 9
    Table.Columns.A dd()
    Table.Columns(C ounter).Default Value = ""
    Next

    For Counter = 0 To 9
    Table.Rows.Add( Table.NewRow)
    Next

    DataGrid1.DataS ource = Table

    DataGrid1.Contr ols.Add(CBox)

    For Counter = 0 To 9
    CBox.Items.Add( Counter)
    Next

    Move_CBox()

    End Sub

    Private Sub DataGrid1_Curre ntCellChanged(B yVal sender As Object,
    ByVal e As System.EventArg s) Handles DataGrid1.Curre ntCellChanged
    Move_CBox()
    End Sub

    Sub Move_CBox()

    Dim Rect As Rectangle = DataGrid1.GetCu rrentCellBounds

    With CBox
    .Hide()
    .Left = Rect.Left
    .Top = Rect.Top
    .Width = Rect.Width
    .Height = Rect.Height
    .Show()
    .BringToFront()
    End With

    End Sub

    Setting the comboboxe's height doesn't seem to actually do anything.
    Also, scrolling the Grid leaves the ComboBox where it is, look like the
    OnScroll event would need to figure out what's going on.

    Hide the ComboBox before moving it, otherwise it shows it in its new
    place, then hides the old one, making a moment where two ComboBoxes are
    displayed.

    BringToFront is required to show it on top of the textbox edit. Using a
    CheckBox ColumnStyle would not require this step.

    B.

  • Samuel Shulman

    #2
    Re: ComboBox in DataGrid

    Ther is a newer version called DataGridView that has this functionality
    built in (I use in VS.NET 2005)


    "Brian Tkatch" <Maxwell_Smart@ ThePentagon.com wrote in message
    news:1152727520 .264567.10530@h 48g2000cwc.goog legroups.com...
    This is mostly for the fun of it. Just saw it in the FAQ and figured
    i'd quickly give it a shot.
    >
    The syncfusion FAQ
    <URL:http://www.syncfusion. com/FAQ/WindowsForms/FAQ_c44c.aspx#q 480q>
    mentions:
    >
    "5.9 How can I put a combobox in a column of a datagrid?
    >
    There are several ways to go about this task. The simplest way involves
    adding a single combobox to the DataGrid.Contro ls, and then selectively
    displaying it as needed when a combobox cell becomes the currentcell.
    All the work is done in a few event handlers and no overrides or
    derived classes are necessary. This technique is discussed in Microsoft
    KB article Q323167."
    >
    But the KB is missing. So, i decided to play with it.
    >
    Open a form, put in a DataGrid, then use the following code:
    >
    Dim CBox As New ComboBox
    >
    Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    >
    Dim Table As New DataTable
    Dim Counter As Integer
    >
    For Counter = 0 To 9
    Table.Columns.A dd()
    Table.Columns(C ounter).Default Value = ""
    Next
    >
    For Counter = 0 To 9
    Table.Rows.Add( Table.NewRow)
    Next
    >
    DataGrid1.DataS ource = Table
    >
    DataGrid1.Contr ols.Add(CBox)
    >
    For Counter = 0 To 9
    CBox.Items.Add( Counter)
    Next
    >
    Move_CBox()
    >
    End Sub
    >
    Private Sub DataGrid1_Curre ntCellChanged(B yVal sender As Object,
    ByVal e As System.EventArg s) Handles DataGrid1.Curre ntCellChanged
    Move_CBox()
    End Sub
    >
    Sub Move_CBox()
    >
    Dim Rect As Rectangle = DataGrid1.GetCu rrentCellBounds
    >
    With CBox
    .Hide()
    .Left = Rect.Left
    .Top = Rect.Top
    .Width = Rect.Width
    .Height = Rect.Height
    .Show()
    .BringToFront()
    End With
    >
    End Sub
    >
    Setting the comboboxe's height doesn't seem to actually do anything.
    Also, scrolling the Grid leaves the ComboBox where it is, look like the
    OnScroll event would need to figure out what's going on.
    >
    Hide the ComboBox before moving it, otherwise it shows it in its new
    place, then hides the old one, making a moment where two ComboBoxes are
    displayed.
    >
    BringToFront is required to show it on top of the textbox edit. Using a
    CheckBox ColumnStyle would not require this step.
    >
    B.
    >

    Comment

    • Brian Tkatch

      #3
      Re: ComboBox in DataGrid


      Samuel Shulman wrote:
      Ther is a newer version called DataGridView that has this functionality
      built in (I use in VS.NET 2005)
      >
      >
      "Brian Tkatch" <Maxwell_Smart@ ThePentagon.com wrote in message
      news:1152727520 .264567.10530@h 48g2000cwc.goog legroups.com...
      This is mostly for the fun of it. Just saw it in the FAQ and figured
      i'd quickly give it a shot.

      The syncfusion FAQ
      <URL:http://www.syncfusion. com/FAQ/WindowsForms/FAQ_c44c.aspx#q 480q>
      mentions:

      "5.9 How can I put a combobox in a column of a datagrid?

      There are several ways to go about this task. The simplest way involves
      adding a single combobox to the DataGrid.Contro ls, and then selectively
      displaying it as needed when a combobox cell becomes the currentcell.
      All the work is done in a few event handlers and no overrides or
      derived classes are necessary. This technique is discussed in Microsoft
      KB article Q323167."

      But the KB is missing. So, i decided to play with it.

      Open a form, put in a DataGrid, then use the following code:

      Dim CBox As New ComboBox

      Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
      System.EventArg s) Handles MyBase.Load

      Dim Table As New DataTable
      Dim Counter As Integer

      For Counter = 0 To 9
      Table.Columns.A dd()
      Table.Columns(C ounter).Default Value = ""
      Next

      For Counter = 0 To 9
      Table.Rows.Add( Table.NewRow)
      Next

      DataGrid1.DataS ource = Table

      DataGrid1.Contr ols.Add(CBox)

      For Counter = 0 To 9
      CBox.Items.Add( Counter)
      Next

      Move_CBox()

      End Sub

      Private Sub DataGrid1_Curre ntCellChanged(B yVal sender As Object,
      ByVal e As System.EventArg s) Handles DataGrid1.Curre ntCellChanged
      Move_CBox()
      End Sub

      Sub Move_CBox()

      Dim Rect As Rectangle = DataGrid1.GetCu rrentCellBounds

      With CBox
      .Hide()
      .Left = Rect.Left
      .Top = Rect.Top
      .Width = Rect.Width
      .Height = Rect.Height
      .Show()
      .BringToFront()
      End With

      End Sub

      Setting the comboboxe's height doesn't seem to actually do anything.
      Also, scrolling the Grid leaves the ComboBox where it is, look like the
      OnScroll event would need to figure out what's going on.

      Hide the ComboBox before moving it, otherwise it shows it in its new
      place, then hides the old one, making a moment where two ComboBoxes are
      displayed.

      BringToFront is required to show it on top of the textbox edit. Using a
      CheckBox ColumnStyle would not require this step.

      B.
      The corporate standard here is still 2003. (2005 is being evaluated.)
      So, i may be stuck for now. But i'm going to have to check that out.

      Thanx.

      B.

      Comment

      Working...