<code>
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb.OleDbDataAdapter
Imports System.Data.OleDb.OleDbDataReader
Imports System.Data.OleDb.OleDbCommand
Public Class Form6
Inherits System.Windows.Forms.Form
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim sql As String
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Public WithEvents ColumnComboBox As NoKeyUpCombo 'special class
Private WithEvents cmSource As CurrencyManager
Private mRowNum As Integer
Private isEditing As Boolean
Shared Sub New()
End Sub
Public Sub New()
MyBase.New()
ColumnComboBox = New NoKeyUpCombo
AddHandler ColumnComboBox.SelectionchangeCommitted, _
New EventHandler(AddressOf ComboStartEditing)
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, _
ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, _
ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText, cellIsVisible)
mRowNum = rowNum
cmSource = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)
ColumnComboBox.Text = Me.TextBox.Text
TextBox.Visible = False
ColumnComboBox.Visible = True
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
End Sub
Protected Overloads Overrides Function Commit(ByVal dataSource As _
CurrencyManager, ByVal rowNum As Integer) As Boolean
If isEditing Then
isEditing = False
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
End If
Return True
End Function
Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs)
isEditing = True
MyBase.ColumnStartedEditing(DirectCast(sender, Control))
End Sub
Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs) _
Handles ColumnComboBox.Leave
If isEditing Then
SetColumnValueAtRow(cmSource, mRowNum, ColumnComboBox.Text)
isEditing = False
Invalidate()
End If
ColumnComboBox.Hide()
End Sub
End Class
Public Class NoKeyUpCombo
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg <> &H101 Then
MyBase.WndProc(m)
End If
End Sub
End Class
Public Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("Names")
Dim i As Integer
dt.Columns.Add("UniqueNo.")
dt.Columns.Add("Itemcode")
dt.Columns.Add("ProblemCode")
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=h:\vbproject\officeproject\officedatabase.mdb;")
cn.Open()
''cmd = New OleDbCommand("select * from login_table1 where Ename=" & " " & txt_name.Text & " " & " and Password =" & " " & txt_password.Text & " " & " ", cn)
''cmd = New OleDbCommand("select * from login_table1 where Ename= trim('" & TextBox1.Text & "') and Password =trim( '" & TextBox2.Text & "' ) ", cn)
''sql = "insert into party values ('" & code.Text & "','" & name1.Text & "','" & add1.Text & "', '" & add2.Text & "', '" & add3.Text & "','" & city.Text & "', '" & State.Text & "','" & pincode.Text & "', '" & country.Text & "','" & isd.Text & "','" & std.Text & "','" & tel1.Text & "','" & tel2.Text & "','" & fax.Text & "','" & contact1.Text & "','" & mobile1.Text & "','" & contact2.Text & "','" & mobile2.Text & "')"
cmd = New OleDbCommand("select rmnno from rmnmaster order by rmnno desc", cn)
''sql = "insert into rmnform values ('" & number_txt.Text & "','" & date_txt.Text & "','" & address_txt.Text & "', '" & item_txt.Text & "', '" & reference_txt.Text & "','" & serial_txt.Text & "', '" & complaint_txt.Text & "','" & remarks_txt.Text & "', '" & dcno_txt.Text & "')"
''cmd = New OleDbCommand("select * from login_table1", cn)
dr = cmd.ExecuteReader
dr.Read()
''For i = 0 To dr.RecordsAffected'
''dt.LoadDataRow(New Object() {"", dr.GetValue(0)}, True)
''Next i
''dt.LoadDataRow(New Object() {"Ken Tucker", ""}, True)
''dt.LoadDataRow(New Object() {"Cor Ligthert", ""}, True)
''dt.LoadDataRow(New Object() {"Terry Burns", ""}, True)
''dt.LoadDataRow(New Object() {"Armin Zignler", ""}, True)
''dt.LoadDataRow(New Object() {"Herfried K. Wagner", ""}, True)
''dt.LoadDataRow(New Object() {"Jay B Harlow", ""}, True)
'above only to build a sample datatable
dt.DefaultView.AllowNew = False
DataGrid1.DataSource = dt.DefaultView
Dim ts As New DataGridTableStyle
'datagrid1.BindingMode = TwoWay)
ts.MappingName = "Names"
Dim uniquenotextCol As New DataGridTextBoxColumn
uniquenotextCol.MappingName = "UniqueNo."
uniquenotextCol.HeaderText = "UniqueNo."
uniquenotextCol.Width = 120
' uniquenotextCol()
ts.GridColumnStyles.Add(uniquenotextCol, GetType(String))
'Dim j = 0
'Dim newitemno, subitemno, correctitemno As String
'Dim selecteditem As String
Dim uniqueno_txt() As String
''selecteditem = "120311"
For i = 0 To 2
uniqueno_txt(i) = String.Concat("aaa", "1")
Next
'uniqueno_txt(0) = "aaa"
'uniqueno_txt(0) = String.Concat("12345", "a")
'uniqueno_txt(1) = String.Concat("12345", "b")
'uniqueno_txt(2) = String.Concat("12345", "c")
'MsgBox(uniqueno_txt(2))
'cn.Close()
'----
Dim cmbTxtCol As New DataGridComboBoxColumn
cmbTxtCol.MappingName = "Itemcode"
cmbTxtCol.HeaderText = "Itemcode"
cmbTxtCol.Width = 100
ts.GridColumnStyles.Add(cmbTxtCol)
ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=h:\vbproject\officeprojectofficedatabase.mdb;")
cn.Open()
cmd = New OleDbCommand("select itemcode from itemmaster order by itemcode asc", cn)
dr = cmd.ExecuteReader
While dr.Read()
cmbTxtCol.ColumnComboBox.Items.Add(dr.GetValue(0))
End While
cn.Close()
Dim cmbTxtCol1 As New DataGridComboBoxColumn
cmbTxtCol1.MappingName = "ProblemCode"
cmbTxtCol1.HeaderText = "ProblemCode"
cmbTxtCol1.Width = 100
ts.GridColumnStyles.Add(cmbTxtCol1)
ts.PreferredRowHeight = (cmbTxtCol1.ColumnComboBox.Height + 3)
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=h:\vbproject\officeproject\officedatabase.mdb;")
cn.Open()
cmd = New OleDbCommand("select problemcode from problemmaster order by problemcode asc", cn)
dr = cmd.ExecuteReader
While dr.Read()
cmbTxtCol1.ColumnComboBox.Items.Add(dr.GetValue(0))
End While
cn.Close()
For i = 0 To 2
dt.LoadDataRow(New Object() {uniqueno_txt(i).ToString(), cmbTxtCol.ColumnComboBox, cmbTxtCol1.ColumnComboBox}, True)
Next i
DataGrid1.TableStyles.Add(ts)
End Sub
Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim i As Integer'
'cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=h:\vbproject\officeproject\officedatabase.mdb;")
'cn.Open()
'For i = 0 To 2
' cmd = new OleDbCommand("insert into rmnform values (" + 1.Cells[0].Text + ",'" + gvr.Cells[2].Text + "')", cn)
'cmd.ExecuteNonQuery()
'Next i
'cn.Close()
' }
End Sub
End Class
</code>
This is the coding i have used to a datagrid with textbox and combobox column. But for the first column i have to generate a unique name for each item and display in the first column of the data grid. I used array of string and tried to add that array value to the first column. But it is showing the error " Object reference not set to an instance of an object". Can anybody let me know how to add the unique name column to my datagrid.
Thanks in advance
Rekha
This post has been edited by modi123_1: 12 March 2012 - 10:48 AM
Reason for edit:: please use code tags

New Topic/Question
Reply



MultiQuote



|