How to transfer selected value from listbox to datagridview

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 11457 Views - Last Post: 18 October 2012 - 01:06 AM Rate Topic: -----

#1 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

How to transfer selected value from listbox to datagridview

Posted 15 October 2012 - 06:30 AM

:helpsmilie:
I have a listbox which contains some items and a datagridview and a add button and now i want the user to select an item in the listbox and send it to the datagridview..my datagridview is bind to a data source
thanks in advance!!!!
Is This A Good Question/Topic? 0
  • +

Replies To: How to transfer selected value from listbox to datagridview

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to transfer selected value from listbox to datagridview

Posted 15 October 2012 - 07:26 AM

What I've done before (not sure if it's the best way to go) is to create a dataview from the datasource table and set the datasource of the dgv to the dataview. Then you can use something like:

dv.RowFilter = "FieldName = '" & Combobox1.Text & "'"

This should immediately switch the datagridview to rows matching the query results.
Was This Post Helpful? 0
  • +
  • -

#3 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: How to transfer selected value from listbox to datagridview

Posted 16 October 2012 - 12:39 AM

my basic problem is that i have a listbox which containts multiples items, a datagridview which has a datasource and a add button to select an item in the listbox and pass it to the datagridview (the user can select mutliple items and pass them to one datagridview column but row by row)
hw can i achieve that?
am tryng something like this
 Dim sqlcon As New SqlClient.SqlConnection(My.Settings.NewEvapcoDBConnectionString.ToString)
        sqlcon.Open()
        For x As Integer = 0 To ListBox1.Items.Count - 1
            Accessories_ItemsDataGridView.Columns.Add(ListBox1.SelectedItem(x).cells(0).value)
        Next
        sqlcon.Close()

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to transfer selected value from listbox to datagridview

Posted 16 October 2012 - 04:33 AM

You're going to have to add to the datasource then. When you bind a datasource to a datagridview, that is what you will have to populate.
Was This Post Helpful? 0
  • +
  • -

#5 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 01:19 AM

im very stuck right now i don't even know what code to write now do you think its possible to add item to a datagridview column when its data-bound?
Was This Post Helpful? 0
  • +
  • -

#6 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 02:10 AM

here is what i do with datagridview :

Posted Image

is it close enough for your purpose?
Was This Post Helpful? 0
  • +
  • -

#7 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 02:20 AM

Yes artemix that exactly what i wanna to but now instead of using datagridview to datagridview am using listbox to datagridview bcoz am gonna list all my product name in that listbox nad the user wil just have to select the item and add it to the datagridview
please help me....i really dont knw what code to do that!!!!!!
Was This Post Helpful? 0
  • +
  • -

#8 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 02:26 AM

why i use datagridview to datagridview and not listbox to datagridview? because it's more flexibel (i think), because with that you can pass the detail of product (like id or price or description of product) and if you don't want user see that you just can simply hide it (user just see the name of product and the id will hiding). just imagine if you use listbox, it can only pass one column at a time (maybe there is a way to concate it), but with datagridview, you can pass as much column as you like, and hide which not necessary for the user.

wait a minute i will post the code.

This post has been edited by artemix22: 17 October 2012 - 02:27 AM

Was This Post Helpful? 0
  • +
  • -

#9 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 02:37 AM

here is all the code :

Imports System.Data.SqlClient
Public Class Form1
    Public Sub addDGV()
        With DataGridView1
            .Columns(0).Width = 73
            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(1).Width = 127
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            .Columns(2).Width = 74
            .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Using conn As New MySqlConnection("your connection string goes here")
                conn.Open()
                Dim command As New SqlCommand("select product_id, product_name, product_price from product", conn)
                Dim adapter As New SqlDataAdapter
                Dim dt As New DataTable
                adapter.SelectCommand = command
                adapter.Fill(dt)
                DataGridView1.DataSource = dt
                addDGV()
                command.Dispose()
                adapter.Dispose()
                conn.Close()
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        If DataGridView1.Rows.Count > 0 Then
            DataGridView2.Rows.Add(DataGridView1.CurrentRow.Cells(0).Value, DataGridView1.CurrentRow.Cells(1).Value, DataGridView1.CurrentRow.Cells(2).Value)
        End If
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
        If DataGridView2.Rows.Count > 0 Then
            DataGridView2.Rows.Remove(DataGridView2.CurrentRow)
        End If
    End Sub
End Class



DataGridView1 properties :

AllowUserToAddRow = False
AllowUserToDelteRow = False
AllowUserToResizeColumn = False
AllowUserToResizeRow = False
ColumnHeaderVisible = False
RowHeaderVisible = False

Add Column manually to your DataGridView2, DataGridView2 is on the right side in my example (how many is depend how much you need, in my example i add 3 column : product_id, product_name, and price), and the properties for DataGridView2 :

AllowUserToAddRow = False
AllowUserToDelteRow = False
AllowUserToResizeColumn = False
AllowUserToResizeRow = False
ColumnHeaderVisible = False
RowHeaderVisible = False

tell me if there is an error.

PS : i create Column Header (Product Id, Product Name and Price) manually with label (autosize = false).

This post has been edited by artemix22: 17 October 2012 - 02:39 AM

Was This Post Helpful? 0
  • +
  • -

#10 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 02:42 AM

what you saying is right but the problem is i would like to have all the product name listed in the datagridview at design time inside one column but different rows so that the user can just select the product and add it to the other datagridview which i do not know if its can be done or not
PS: are both your datagridview data-bound or they not?

This post has been edited by christian Mukeba: 17 October 2012 - 02:51 AM

Was This Post Helpful? 0
  • +
  • -

#11 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 02:55 AM

if you add product detail in design time you can't update it (add more product or delete or change something), why don't you create a table in database to hold it?

DataGridView1 data came from database (table product in database) it's data-bound, and DataGridView2 not data-bound it just to hold user selected item from DataGridView1.

This post has been edited by artemix22: 17 October 2012 - 02:56 AM

Was This Post Helpful? 0
  • +
  • -

#12 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 03:14 AM

but if you want the way like that, here is how to put it in your DGV :

add a column to your DGV and then put this code on your form load event :
With DataGridView1.Rows
.add("Pillow")
.Add("Car")
.Add("Cloth")
End With



Posted Image

addButton Code :
If DataGridView1.Rows.Count > 0 Then
     DataGridView2.Rows.Add(DataGridView1.CurrentRow.Cells(0).Value)
End If



deleteButton Code :
If DataGridView2.Rows.Count > 0 Then
     DataGridView2.Rows.Remove(DataGridView2.CurrentRow)
End If


This post has been edited by artemix22: 17 October 2012 - 03:26 AM

Was This Post Helpful? 0
  • +
  • -

#13 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 04:26 AM

Thank you artemix its working you realy helped me thanks again!!!!!!!!!!!God bless you
Was This Post Helpful? 0
  • +
  • -

#14 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 04:32 AM

Aamiin. :D
Was This Post Helpful? 0
  • +
  • -

#15 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: How to transfer selected value from listbox to datagridview

Posted 17 October 2012 - 05:14 AM

Jst for more information if both datagridview are data-bound what will hapen how will you add and remove data from one to another??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2