NOT ABLE TO SET THE DATASOURCE FOR THE COMBOBOX
-
respected Gurus i m making a windows application in vb.net. here i m trying to bind a combobox from dataset. but whenever i m trying to set the datasource property of my combobox,it jumps to the selectedindexchanged event for the combo box. my code is as follows:----------------- code to populate the combobox:--------------- Public Sub populateWebtype() Try cnSQL = New SqlConnection(ConnectionString) Dim strSQL As String = "select webcode,webdesc from webmast" cnSQL.Open() cmSQL = New SqlCommand(strSQL, cnSQL) cmSQL.CommandType = CommandType.Text adpSQL = New SqlDataAdapter(cmSQL) ds = New DataSet adpSQL.Fill(ds) Dim dr As DataRow dr = ds.Tables(0).NewRow 'dr(valuemember) = "" dr("webdesc") = "--Select--" ds.Tables(0).Rows.InsertAt(dr, 0) Me.cmbWebType.DataSource = ds.Tables(0).DefaultView 'AFTER THIS LINEJUMPS TO THE SELECTEDINDEXCHANGED Me.cmbWebType.DisplayMember = "webdesc" Me.cmbWebType.ValueMember = "webcode" Me.cmbWebType.SelectedIndex = 0 cnSQL.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub selectedindexchangedevent of combobox:----------- Private Sub cmbWebType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbWebType.SelectedIndexChanged Try Dim webcode As Integer webcode = cmbWebType.SelectedValue strSQL = "select rmdesc as RawMaterial,Micron,Density,Gsm,Quantity as Norm from webnorm,webmast,rmmast where delete_flag <> 'Y' and webnorm.webcode = webmast.webcode and webnorm.rmcode = rmmast.rmcode And webnorm.webcode = " & webcode cnSQL = New SqlConnection(ConnectionString) cnSQL.Open() cmSQL = New SqlCommand(strSQL, cnSQL) cmSQL.CommandType = CommandType.Text adpSQL = New SqlDataAdapter(cmSQL) ds = New DataSet adpSQL.Fill(ds) DataGrid1.DataSource = ds.Tables(0).DefaultView strSQL = "select distinct total_micron,total_gsm,total_quantity,sqmperkg from webnorm where delete_flag <> 'Y'And webnorm.webcode = " & webcode cmSQL = New SqlCommand(strSQL, cnSQL) Dim readSQL As SqlDataReader readSQL = cmSQL.ExecuteReader While readSQL.Read If No
-
respected Gurus i m making a windows application in vb.net. here i m trying to bind a combobox from dataset. but whenever i m trying to set the datasource property of my combobox,it jumps to the selectedindexchanged event for the combo box. my code is as follows:----------------- code to populate the combobox:--------------- Public Sub populateWebtype() Try cnSQL = New SqlConnection(ConnectionString) Dim strSQL As String = "select webcode,webdesc from webmast" cnSQL.Open() cmSQL = New SqlCommand(strSQL, cnSQL) cmSQL.CommandType = CommandType.Text adpSQL = New SqlDataAdapter(cmSQL) ds = New DataSet adpSQL.Fill(ds) Dim dr As DataRow dr = ds.Tables(0).NewRow 'dr(valuemember) = "" dr("webdesc") = "--Select--" ds.Tables(0).Rows.InsertAt(dr, 0) Me.cmbWebType.DataSource = ds.Tables(0).DefaultView 'AFTER THIS LINEJUMPS TO THE SELECTEDINDEXCHANGED Me.cmbWebType.DisplayMember = "webdesc" Me.cmbWebType.ValueMember = "webcode" Me.cmbWebType.SelectedIndex = 0 cnSQL.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub selectedindexchangedevent of combobox:----------- Private Sub cmbWebType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbWebType.SelectedIndexChanged Try Dim webcode As Integer webcode = cmbWebType.SelectedValue strSQL = "select rmdesc as RawMaterial,Micron,Density,Gsm,Quantity as Norm from webnorm,webmast,rmmast where delete_flag <> 'Y' and webnorm.webcode = webmast.webcode and webnorm.rmcode = rmmast.rmcode And webnorm.webcode = " & webcode cnSQL = New SqlConnection(ConnectionString) cnSQL.Open() cmSQL = New SqlCommand(strSQL, cnSQL) cmSQL.CommandType = CommandType.Text adpSQL = New SqlDataAdapter(cmSQL) ds = New DataSet adpSQL.Fill(ds) DataGrid1.DataSource = ds.Tables(0).DefaultView strSQL = "select distinct total_micron,total_gsm,total_quantity,sqmperkg from webnorm where delete_flag <> 'Y'And webnorm.webcode = " & webcode cmSQL = New SqlCommand(strSQL, cnSQL) Dim readSQL As SqlDataReader readSQL = cmSQL.ExecuteReader While readSQL.Read If No
Setting the datasource DOES cause an index to become selected. If you don't want that, set a flag and check it in the event.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Setting the datasource DOES cause an index to become selected. If you don't want that, set a flag and check it in the event.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
thanks sir for ur quick reply but sir how can i chk so that after setting the datasource it just go to the next statement in the event not to the selectedindexchanged
-
thanks sir for ur quick reply but sir how can i chk so that after setting the datasource it just go to the next statement in the event not to the selectedindexchanged
Set a class-scope flag (A boolean variable) to true just before you set the datasource of the ComboBox. In the SelectedIndexChanged event, check that flag. It it's set, reset the flag to false and don't do anything in the event handler. If it's false, then you do what you need to accomplish.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007