Wednesday, June 30, 2004

 

DataGrid Help -- Windows Form VB.NET








This was a KB article on microsoft site. now i don;t find the link to the same. so i am pasting the code here.

Create the Sample

  1. Follow these steps to create a new Visual Basic Windows Application project:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual Basic Project under Project Types, and then click Windows Application under Templates. By default, Form1 is added.
  2. Drag a DataGrid control from the toolbox to Form1.
  3. Add the following code to the top of the code window in the Declarations section of Form1.vb:
    Imports System.Data.SqlClient
    Imports System.Windows.Forms
  4. Add the following code after the "Windows Form Designer generated code" section of the code window:
    Public MyCombo As New ComboBox()
    Dim con As New SqlConnection("server=myservername;uid=myid;pwd=mypassword;database=northwind")
    Dim daEmp As New SqlDataAdapter("Select * From Employees", con)
    Public ds As New DataSet()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged
    'Fill ComboBox list.
    MyCombo.Name = "MyCombo"
    MyCombo.Visible = False
    MyCombo.Items.Clear()
    MyCombo.Items.Add("Sales Representative")
    MyCombo.Items.Add("Inside Sales Coordinator")
    MyCombo.Items.Add("Vice President, Sales")
    MyCombo.Items.Add("Sales Manager")
    MyCombo.Items.Add("Flunky")
    daEmp.Fill(ds, "Employees")
    'Set the RowHeight of the DataGrid to the height of the ComboBox.
    DataGrid1.PreferredRowHeight = MyCombo.Height
    DataGrid1.DataSource = ds
    DataGrid1.DataMember = "Employees"
    'Add ComboBox to the Control collection of the DataGrid.
    DataGrid1.Controls.Add(MyCombo)
    End Sub


    Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
    If DataGrid1.CurrentCell.ColumnNumber = 3 Then
    MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width
    End If
    End Sub

    Private Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If DataGrid1.CurrentCell.ColumnNumber = 3 Then
    MyCombo.Visible = False
    If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then
    SendKeys.Send("*")
    End If
    DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text
    End If
    End Sub


    Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
    If DataGrid1.CurrentCell.ColumnNumber = 3 Then
    MyCombo.Visible = False
    MyCombo.Width = 0
    MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left
    My Combo.Top = DataGrid1.GetCurrentCellBounds.Top
    MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & ""
    MyCombo.Visible = True
    Else
    MyCombo.Visible = False
    MyCombo.Width = 0
    End If
    End Sub

    Private Sub DataGrid1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Scroll
    MyCombo.Visible = False
    MyCombo.Width = 0
    End Sub


    Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
    MyCombo.Visible = False
    MyCombo.Width = 0
    End Sub
  5. Modify the connection string as necessary for your environment.
  6. Press F5 to run the project.
    Click one of the fields in the Title column in the DataGrid.
    Notice that the ComboBox control is located in the DataGrid.
  7. Expand the ComboBox.
    Notice that a list of titles is displayed.



Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?