OVH Cloud OVH Cloud

ComboBox dans un Datagrid

4 réponses
Avatar
salvo
Bonjour =E0 tous.

quelqu'un pourrai me dire comment gerer simplement une=20
combobox dans un datagrid.

merci d'avance

4 réponses

Avatar
faridBouja
bonjour salvo,
j'ai le meme pb, merci de me donner une piste si tu as réglé le pb.

Bouja


"salvo" a écrit :

Bonjour à tous.

quelqu'un pourrai me dire comment gerer simplement une
combobox dans un datagrid.

merci d'avance





Avatar
Axel Guerrier [MS]
Bonjour,

Il existait cette fiche à un moment:

323167 HOW TO: Add a ComboBox Control to a Windows Form DataGrid Control
http://support.microsoft.com/?id23167

mais je crois qu'elle est plus dispo...

Voici le code qu'elle contenait (non testé !)

Create the Sample

-----------------

1. Follow these steps to create a new Visual Basic Windows Application
project:

a. Start Microsoft Visual Studio .NET.

b. On the File menu, point to New, and then click Project.

c. In the New Project dialog box, click Visual Basic Project under Project
Types, and then click Windows Application under Templates. By default, Form1
is added.

2. Drag a DataGrid control from the toolbox to Form1.

3. Add the following code to the top of the code window in the Declarations
section of Form1.vb:


Imports System.Data.SqlClient

Imports System.Windows.Forms


4. Add the following code after the "Windows Form Designer generated code"
section of the code window:


Public MyCombo As New ComboBox()

Dim con As New
SqlConnection("server=myservername;uid=myid;pwd=mypassword;database=northwind")

Dim daEmp As New SqlDataAdapter("Select * From Employees", con)


Public ds As New DataSet()

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

AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged

'Fill ComboBox list.

MyCombo.Name = "MyCombo"

MyCombo.Visible = False

MyCombo.Items.Clear()

MyCombo.Items.Add("Sales Representative")

MyCombo.Items.Add("Inside Sales Coordinator")

MyCombo.Items.Add("Vice President, Sales")

MyCombo.Items.Add("Sales Manager")

MyCombo.Items.Add("Flunky")



daEmp.Fill(ds, "Employees")


'Set the RowHeight of the DataGrid to the height of the ComboBox.

DataGrid1.PreferredRowHeight = MyCombo.Height


DataGrid1.DataSource = ds


DataGrid1.DataMember = "Employees"

'Add ComboBox to the Control collection of the DataGrid.

DataGrid1.Controls.Add(MyCombo)

End Sub


Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width

End If

End Sub


Private Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs)

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Visible = False

If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then

SendKeys.Send("*")

End If

DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text

End If

End Sub


Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.CurrentCellChanged

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Visible = False

MyCombo.Width = 0

MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left

MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top

MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & ""

MyCombo.Visible = True

Else

MyCombo.Visible = False

MyCombo.Width = 0

End If

End Sub


Private Sub DataGrid1_Scroll(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.Scroll

MyCombo.Visible = False

MyCombo.Width = 0

End Sub


Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.Click

MyCombo.Visible = False

MyCombo.Width = 0

End Sub


5. Modify the connection string as necessary for your environment.

6. Press F5 to run the project. Click one of the fields in the Title

column in the DataGrid. Notice that the ComboBox control is located in

the DataGrid.

7. Expand the ComboBox. Notice that a list of titles is displayed.


--
Axel Guerrier
Microsoft France
--------------------
Merci de bien vouloir répondre à ce message dans le newsgroup où il a été
posté. Je le consulte régulièrement.

"faridBouja" wrote in message
news:
bonjour salvo,
j'ai le meme pb, merci de me donner une piste si tu as réglé le pb.

Bouja


"salvo" a écrit :

Bonjour à tous.

quelqu'un pourrai me dire comment gerer simplement une
combobox dans un datagrid.

merci d'avance







Avatar
faridBouja
Bjr Axel,
merci, c'est un bon exemple que je cherchais.

Bonjour


"Axel Guerrier [MS]" a écrit :

Bonjour,

Il existait cette fiche à un moment:

323167 HOW TO: Add a ComboBox Control to a Windows Form DataGrid Control
http://support.microsoft.com/?id23167

mais je crois qu'elle est plus dispo...

Voici le code qu'elle contenait (non testé !)

Create the Sample

-----------------

1. Follow these steps to create a new Visual Basic Windows Application
project:

a. Start Microsoft Visual Studio .NET.

b. On the File menu, point to New, and then click Project.

c. In the New Project dialog box, click Visual Basic Project under Project
Types, and then click Windows Application under Templates. By default, Form1
is added.

2. Drag a DataGrid control from the toolbox to Form1.

3. Add the following code to the top of the code window in the Declarations
section of Form1.vb:


Imports System.Data.SqlClient

Imports System.Windows.Forms


4. Add the following code after the "Windows Form Designer generated code"
section of the code window:


Public MyCombo As New ComboBox()

Dim con As New
SqlConnection("server=myservername;uid=myid;pwd=mypassword;database=northwind")

Dim daEmp As New SqlDataAdapter("Select * From Employees", con)


Public ds As New DataSet()

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

AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged

'Fill ComboBox list.

MyCombo.Name = "MyCombo"

MyCombo.Visible = False

MyCombo.Items.Clear()

MyCombo.Items.Add("Sales Representative")

MyCombo.Items.Add("Inside Sales Coordinator")

MyCombo.Items.Add("Vice President, Sales")

MyCombo.Items.Add("Sales Manager")

MyCombo.Items.Add("Flunky")



daEmp.Fill(ds, "Employees")


'Set the RowHeight of the DataGrid to the height of the ComboBox.

DataGrid1.PreferredRowHeight = MyCombo.Height


DataGrid1.DataSource = ds


DataGrid1.DataMember = "Employees"

'Add ComboBox to the Control collection of the DataGrid.

DataGrid1.Controls.Add(MyCombo)

End Sub


Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width

End If

End Sub


Private Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs)

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Visible = False

If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then

SendKeys.Send("*")

End If

DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text

End If

End Sub


Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.CurrentCellChanged

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Visible = False

MyCombo.Width = 0

MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left

MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top

MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & ""

MyCombo.Visible = True

Else

MyCombo.Visible = False

MyCombo.Width = 0

End If

End Sub


Private Sub DataGrid1_Scroll(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.Scroll

MyCombo.Visible = False

MyCombo.Width = 0

End Sub


Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.Click

MyCombo.Visible = False

MyCombo.Width = 0

End Sub


5. Modify the connection string as necessary for your environment.

6. Press F5 to run the project. Click one of the fields in the Title

column in the DataGrid. Notice that the ComboBox control is located in

the DataGrid.

7. Expand the ComboBox. Notice that a list of titles is displayed.


--
Axel Guerrier
Microsoft France
--------------------
Merci de bien vouloir répondre à ce message dans le newsgroup où il a été
posté. Je le consulte régulièrement.

"faridBouja" wrote in message
news:
> bonjour salvo,
> j'ai le meme pb, merci de me donner une piste si tu as réglé le pb.
>
> Bouja
>
>
> "salvo" a écrit :
>
>> Bonjour à tous.
>>
>> quelqu'un pourrai me dire comment gerer simplement une
>> combobox dans un datagrid.
>>
>> merci d'avance
>>
>>
>>





Avatar
Xavier
Bonjour,

Parfait petit exemple Axel : Merci !


"Axel Guerrier [MS]" a écrit :

Bonjour,

Il existait cette fiche à un moment:

323167 HOW TO: Add a ComboBox Control to a Windows Form DataGrid Control
http://support.microsoft.com/?id23167

mais je crois qu'elle est plus dispo...

Voici le code qu'elle contenait (non testé !)

Create the Sample

-----------------

1. Follow these steps to create a new Visual Basic Windows Application
project:

a. Start Microsoft Visual Studio .NET.

b. On the File menu, point to New, and then click Project.

c. In the New Project dialog box, click Visual Basic Project under Project
Types, and then click Windows Application under Templates. By default, Form1
is added.

2. Drag a DataGrid control from the toolbox to Form1.

3. Add the following code to the top of the code window in the Declarations
section of Form1.vb:


Imports System.Data.SqlClient

Imports System.Windows.Forms


4. Add the following code after the "Windows Form Designer generated code"
section of the code window:


Public MyCombo As New ComboBox()

Dim con As New
SqlConnection("server=myservername;uid=myid;pwd=mypassword;database=northwind")

Dim daEmp As New SqlDataAdapter("Select * From Employees", con)


Public ds As New DataSet()

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

AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged

'Fill ComboBox list.

MyCombo.Name = "MyCombo"

MyCombo.Visible = False

MyCombo.Items.Clear()

MyCombo.Items.Add("Sales Representative")

MyCombo.Items.Add("Inside Sales Coordinator")

MyCombo.Items.Add("Vice President, Sales")

MyCombo.Items.Add("Sales Manager")

MyCombo.Items.Add("Flunky")



daEmp.Fill(ds, "Employees")


'Set the RowHeight of the DataGrid to the height of the ComboBox.

DataGrid1.PreferredRowHeight = MyCombo.Height


DataGrid1.DataSource = ds


DataGrid1.DataMember = "Employees"

'Add ComboBox to the Control collection of the DataGrid.

DataGrid1.Controls.Add(MyCombo)

End Sub


Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width

End If

End Sub


Private Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs)

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Visible = False

If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then

SendKeys.Send("*")

End If

DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text

End If

End Sub


Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.CurrentCellChanged

If DataGrid1.CurrentCell.ColumnNumber = 3 Then

MyCombo.Visible = False

MyCombo.Width = 0

MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left

MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top

MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & ""

MyCombo.Visible = True

Else

MyCombo.Visible = False

MyCombo.Width = 0

End If

End Sub


Private Sub DataGrid1_Scroll(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.Scroll

MyCombo.Visible = False

MyCombo.Width = 0

End Sub


Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.Click

MyCombo.Visible = False

MyCombo.Width = 0

End Sub


5. Modify the connection string as necessary for your environment.

6. Press F5 to run the project. Click one of the fields in the Title

column in the DataGrid. Notice that the ComboBox control is located in

the DataGrid.

7. Expand the ComboBox. Notice that a list of titles is displayed.


--
Axel Guerrier
Microsoft France
--------------------
Merci de bien vouloir répondre à ce message dans le newsgroup où il a été
posté. Je le consulte régulièrement.

"faridBouja" wrote in message
news:
> bonjour salvo,
> j'ai le meme pb, merci de me donner une piste si tu as réglé le pb.
>
> Bouja
>
>
> "salvo" a écrit :
>
>> Bonjour à tous.
>>
>> quelqu'un pourrai me dire comment gerer simplement une
>> combobox dans un datagrid.
>>
>> merci d'avance
>>
>>
>>