الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

الداتا قريد فيو مع الكومبوبوكس

بدأه serious في 1 سبتمبر 2009 · 1 رد · 689 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اخواني سبق وطرحت موضوع اتمنى من خلاله ان اجد مثال

فيه داتا قريد وبداخل الداتا قريد فيو كومبوبوكس

الكومبوبوكس مربوط بجدول

وعند اختيار عنصر من الكومبوبوكس

يتم عرض بياناته في الداتا قريد فيو

ووجدت هنا موضوع في منتدى اجنبي فيه المطلوب لكنني عجزت عن تطبيقه

لاني اعتقد انه وضع جزء من الكود فقط

وكان مضمون الموضوع

Hello.

I have a strange problem that i cannot solve:

On a form I have a DataGridView. Inside this DataGridView i have several columns of which one is combobox and the others are textboxes.

The combobox column is populated with values from a MySql database table using a query. So far so good. What i want next is to populate a certain textbox from that DataGridView, automatically after a value from the combobox is selected. When a value is selected from the dropdown list of the combobox, that value is used in a SQL query to find the value that will be displayed in the textbox. (All the textboxes and the combobox are inside the DataGridView!)

I did this using DataGridView EditingControlShowing event because a DataGridViewComboBoxCell does not have a SelectedIndexChanged event.

The value is displayed correctly in the textbox, but the problem is that the value selected from the combobox disappears after the selection, and the combobox remains empty. I need the value to remain in the combobox after it was selected and after the corresponding value was inserted in the textbox

وهذا الكود كامل

Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Populating the combobox

With Me.DataGridViewTextBoxColumn2

.DataPropertyName = ColumnName.process_step_name.ToString()

.HeaderText = ColumnName.process_step_name.ToString()

.DataSource = selectpsn()

.ValueMember = ColumnName.process_step_name.ToString()

.DisplayMember = .ValueMember

End With

End Sub

Enum ColumnName

process_step_name

End Enum

Private Function Populate(ByVal sqlCommand As String) As DataTable

Dim northwindConnection As New MySqlConnection("server=localhost; user id=root; password=panamera; database=abc2")

northwindConnection.Open()

Dim command As New MySqlCommand(sqlCommand, _

northwindConnection)

Dim adapter As New MySqlDataAdapter()

adapter.SelectCommand = command

Dim table As New DataTable()

table.Locale = System.Globalization.CultureInfo.InvariantCulture

adapter.Fill(table)

Return table

End Function

Private Function selectpsn() As DataTable

Return Populate( _

"select process_step_name from consultant")

End Function

'From here down -- identifying the value selected in the combobox

'and using it to find and insert the value in the textbox

Private cb As ComboBox

Private Sub Head_of_departmentDataGridView_EditingControlShowing(ByVal sender As Object, _

ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _

Handles Head_of_departmentDataGridView.EditingControlShowing

' Attempt to cast the EditingControl to a ComboBox.

cb = TryCast(e.Control, ComboBox)

If cb IsNot Nothing Then

RemoveHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged

AddHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged

End If

End Sub

Private Sub DGVComboIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

'using the value selected in the cbox to find and insert the right value in the textbox

Dim conn As New MySqlConnection

Dim myCommand As New MySqlCommand

Dim myAdapter As New MySqlDataAdapter

Dim myData As New DataTable

Dim ds As New DataSet

Dim SQL As String

'the query used to find the value for the textbox

SQL = "select process_Step_number from consultant where process_step_name = '" & cb.SelectedValue.ToString & "'"

Dim myConnString As String

myConnString = "server=localhost" & ";" _

& "user id=root" & ";" _

& "password=panamera; database = abc2"

conn.ConnectionString = myConnString

Try

conn.Open()

Try

myCommand.Connection = conn

myCommand.CommandText = SQL

myAdapter.SelectCommand = myCommand

myAdapter.Fill(myData)

DataGridViewTextBoxColumn1.DataGridView.DataSource = myData

Catch myerror As MySqlException

MsgBox("There was an error reading from the database: " & myerror.Message)

End Try

Catch myerror As MySqlException

MessageBox.Show("Error connecting to the database: " & myerror.Message)

Finally

If conn.State <> ConnectionState.Closed Then conn.Close()

End Try

conn.Close()

End Sub

وضعته كما هو ووضعت داتا قريد فيو

فضهرت لي بعض الاخطاء

فمن يتبرع ويطبقه في مثال ويضعه هنا

لتعم الفائده

والله تعبت من البحث

#2

اين الرد ياخوان

مواضيع مشابهة