1 Replies - 1209 Views - Last Post: 30 June 2012 - 04:43 AM Rate Topic: -----

#1 purvabhilare1587  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 27-May 12

change order of the columns read from database into a datagridview

Posted 30 June 2012 - 03:42 AM

I am reading a record from a database into a datagridview.I have changed the column "ComputerAwareness" from a textbox column to a combobox column.This column is at the 21st position in the database but after changing it to a combobox in datagridview it is diplayed as the first column in the datagridview.How can I keep the position of this column same as in the database?I have googled a lot for the solution and tried all the possible ways but none seem to work.One of the ways I tried is in the code below.

 Private Sub DisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayButton.Click
        Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;Data Source=C:\Users\technowin\Documents\Candidates.accdb;Jet OLEDB:Database Password=techno;"
        Dim sql As String

        If LastNameTextBox.Text = Nothing Then
            sql = "SELECT * FROM Infotable WHERE FirstName = '" & FirstNameTextBox.Text & "' AND MiddleName = '" & MiddleNameTextBox.Text & "'"
        Else
            sql = "SELECT * FROM Infotable WHERE LastName = '" & LastNameTextBox.Text & "' AND FirstName = '" & FirstNameTextBox.Text & "' AND Lastname = '" & LastNameTextBox.Text & "' "
        End If
        ' DataGridView1.Visible = True
        Dim connection As New OleDb.OleDbConnection(cs)
        dataadapter = New OleDb.OleDbDataAdapter(sql, connection)
        ds = New DataSet()
        connection.Open()

        'Removing the existing column from datagrid and adding a combobox column
        ' EditDataGridView.Columns.Remove("ComputerAwareness")
        Dim dgvCombo As New DataGridViewComboBoxColumn

        With dgvCombo
            .Width = 150
            .Items.Add("Excellent")
            .Items.Add("Very Good")
            .Items.Add("Good")
            .Items.Add("Okay")
            .Items.Add("Bad")
            .DataPropertyName = "ComputerAwareness"
            .HeaderText = "ComputerAwareness"
        End With

        EditDataGridView.Columns.Add(dgvCombo)


        'EditDataGridView.Columns(0).DisplayIndex = 2
        AdjustColumnOrder()
        'make the datagrid visible
        EditDataGridView.Visible = True
        ' DataGridView1CloseButton.Visible = True
        dataadapter.Fill(ds, "Infotable")
        connection.Close()
        EditDataGridView.DataSource = ds
        EditDataGridView.DataMember = "Infotable"
        ' DataGridView1.ReadOnly = True

    End Sub
    Sub AdjustColumnOrder()
        EditDataGridView.Columns("ID").Visible = False
        EditDataGridView.Columns("ComputerAwareness").DisplayIndex = 21
    End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: change order of the columns read from database into a datagridview

#2 purvabhilare1587  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 27-May 12

Re: change order of the columns read from database into a datagridview

Posted 30 June 2012 - 04:43 AM

I have not added columns to the datagridview.I have read all the columns from database.So I am not able to change the order using the edit column property of datagridview.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1