Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting desired column in datagridview to combobox,checkbox

Status
Not open for further replies.

MikeCt

Programmer
Nov 6, 2001
44
US
Hello

I have a Datagridview which is populated from a sql query. The order of the columns and binding of the grid are handled at runtime. Problem is I cannot find a way to assign the combobox and checkbox to the columns I desire. In the example below column (1) and the last column share the same header names. I'm trying to have the last Column ( filled with a combobox ) show in column (1). All examples I've come across, always show comboboxes and checkboxes as the end columns. As shown in the example I can achieve this. But I can not find any examples with code to show how to populate the desired columns with the comboboxes or checkboxes.
VB2013

Thank you for any help

Imports System.Data.OleDb
Public Class frm_Checkbook

Dim dbAdapter As OleDb.OleDbDataAdapter
Dim dbDataSet As New DataSet
Dim dbDataTable As New DataTable
Dim strSQL As String

Private Sub frm_Checkbook_Load(sender As Object, e As EventArgs) Handles MyBase.Load
gbl_DstConnect.Open()
Call datagridShow()
End Sub


Private Sub datagridShow()

strSQL = "Select Date,Transaction,Chkno,Payee,Category,Memo,Amount,Balance,Clear FROM Banking"
dbDataSet.Tables.Add(dbDataTable)

Using dbAdapter As New OleDb.OleDbDataAdapter(strSQL, gbl_DstConnect)
Using dbDataTable As New DataTable()
dbAdapter.Fill(dbDataTable)

DataGridView1.AutoGenerateColumns = False
DataGridView1.Size = New Size(960, 574) 'New Size(882, 574)
DataGridView1.RowTemplate.Height = 22
DataGridView1.ColumnCount = 8

DataGridView1.Columns(0).Width = 70
DataGridView1.Columns(0).Name = "Date"
DataGridView1.Columns(0).HeaderText = " Date"
DataGridView1.Columns(0).DataPropertyName = "Date"

DataGridView1.Columns(1).Width = 78
DataGridView1.Columns(1).Name = "Transaction"
DataGridView1.Columns(1).HeaderText = "Transaction"
DataGridView1.Columns(1).DataPropertyName = "Transaction"

DataGridView1.Columns(2).Width = 50
DataGridView1.Columns(2).Name = "Chkno"
DataGridView1.Columns(2).HeaderText = "Chkno"
DataGridView1.Columns(2).DataPropertyName = "Chkno"

DataGridView1.Columns(3).Width = 200
DataGridView1.Columns(3).Name = "Payee"
DataGridView1.Columns(3).HeaderText = " Payee"
DataGridView1.Columns(3).DataPropertyName = "Payee"

DataGridView1.Columns(4).Width = 100
DataGridView1.Columns(4).Name = "Category"
DataGridView1.Columns(4).HeaderText = " Category"
DataGridView1.Columns(4).DataPropertyName = "Category"

DataGridView1.Columns(5).Width = 120
DataGridView1.Columns(5).Name = "Memo"
DataGridView1.Columns(5).HeaderText = " Memo"
DataGridView1.Columns(5).DataPropertyName = "Memo"

DataGridView1.Columns(6).Width = 80
DataGridView1.Columns(6).DefaultCellStyle.Format = String.Format("c")
DataGridView1.Columns(6).Name = "Amount"
DataGridView1.Columns(6).HeaderText = " Amount"
DataGridView1.Columns(6).DataPropertyName = "Amount"

DataGridView1.Columns(7).Width = 90
DataGridView1.Columns(7).DefaultCellStyle.Format = String.Format("c")
DataGridView1.Columns(7).Name = "Balance"
DataGridView1.Columns(7).HeaderText = " Balance"
DataGridView1.Columns(7).DataPropertyName = "Balance"

Dim Cleared As New DataGridViewCheckBoxColumn()
DataGridView1.Columns.Add(Cleared)
Cleared.HeaderText = "Cleared"
Cleared.Name = "Cleared"
Cleared.Width = 50
Cleared.ReadOnly = True
DirectCast(DataGridView1.Columns("Cleared"), DataGridViewCheckBoxColumn).DataPropertyName = "Clear"


Dim Type As New DataGridViewComboBoxColumn()
Type.HeaderText = "Transactions"
Type.Name = "Type"
Type.Width = 78
Type.MaxDropDownItems = 4
Type.Items.Add("Check")
Type.Items.Add("ATM")
Type.Items.Add("Deposit")
Type.Items.Add("Misc Additions")
Type.Items.Add("Misc Charges")
Type.Items.Add("Interest")
DataGridView1.Columns.Add(Type)
Type.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
DirectCast(DataGridView1.Columns("Type"), DataGridViewComboBoxColumn).DataPropertyName = "Transaction"

DataGridView1.DataSource = dbDataTable
DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.Rows.Count - 1 'Scroll to end of records

'Disable column sort
For i = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next i

End Using
End Using

gbl_DstConnect.Close()

End Sub


End Class
 

Instead of using the Add method, like this:

DataGridView1.Columns.Add(Type)

use the Insert method, which allows you to specify the index of the column:

DataGridView1.Columns.Insert(1, Type)


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Excellent, Sorry for not getting back earlier
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top