How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

  1. #1
    Join Date
    Sep 2011
    Posts
    6

    How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    Hi,

    My application is in VS2008 and coded in VB.net.

    I have a datagridView(DGV) on one of my form.DGV contains a column of type DatagridViewComboboxColumn.I have another column of SrNo.

    Consider Table1 to be the table from where my DatagridView is filled which contains following fields SrNo,Value1,Value2.

    My requirement is i want to fill the values in my DatagridViewComboboxColumn from Table1.The DatagridViewComboboxColumn's combobox should contain values of columns Value1,Value2 of Table1.

    Sample data of Table1

    SrNo Value1 Value2

    1 2 3

    2 4 5

    As data in datagridView is filled from Table1 the data of SrNo column of Table1 is also populated in one of the column of DGV.

    Now my DatagridViewComboboxColumn combobox should contain the values of Value1 and Value2 of Respective SrNo.

    Like Row one of my DGV has SrNo 1 as first value,so the Value1 and Value2 of SrNo 1 should be added to the row's DatagridViewComboboxColumn.

    Select distinct Value1,Value2 From Table1 where SrNo=DGV.items(0,DGV.currentcell.rowIndex).value.Hope this Query willl make things more clear

    Please Suggest.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,000

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    That doesn't make a lot of sense. You want to WRITE fields from one table into another? That violates referential integrity rules (dup fields)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2011
    Posts
    6

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    where did i said that i want to WRITE fields from one table into another.I said i want to retrieve from table1 and populate in DatagridView.

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,000

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    You can get 123 in a button, but not 1,2,3 unless you split them either in the db then bind, or not bind and load later.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Sep 2011
    Posts
    6

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    Sir im aware of how to fill the datagridview...
    i want to fill the DatagridViewComboboxColumn.Please help how can i fill that combobox in DatagridView.DatagridViewComboboxColumn should have the values of columns Value1 and Value2 from Table1.

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,000

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    Load the data after you read (and format) it.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Sep 2011
    Posts
    6

    Question Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    Only you can understand what solution you are providing..

  8. #8
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,853

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    Are you getting anything in the combo box now? Are you wanting the two values to appear on the same line or different lines of the combo?
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Sep 2011
    Posts
    6

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    Just read this i think this will give clearer idea.
    Please check.
    I have a datagridview with three columns which is filled using Datareader. There is a DatagridViewComboboxcolumn in the datagridView.

    I want this DatagridViewComboboxcolumn should also be filled using datareader.

    Please suggest how can i add items to DatagridViewComboboxcolumn using Datareader. Below is the code that i have tried.

    Here dr is SqlDatareader
    I want to assign the value of Column3 to my DatagridViewComboboxcolumn

    Datagridview.Rows.Add(dr("Column1").ToString, dr("Column2"),dr("Column3 "))

    Please suggest me how can i add items to DatagridViewComboboxcolumn,that my only priority rest requirement i will do it myself.
    thanks.
    Last edited by Ehtesham Siddiqui; October 13th, 2011 at 09:59 AM. Reason: Correction

  10. #10
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,853

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    So I take it the answer to my questions are No you are not getting anything and that you want only one column in the combo box?

    C# sample
    http://www.rustemsoft.com/vbbegin.asp#0

    VB Sample
    http://skaterpro.net/net_datagrid.htm
    Last edited by DataMiser; October 13th, 2011 at 11:06 AM.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    I'm going to jump in (only to be ridiculed later). I believe you want to populate the ComboBox column's drop down items using Value1 & Value2. Essentially you have a two column DGV: first column has SrNo, second column has no value, yet, but the drop down items are Value1 & Value2 from that record (???). If I'm wrong stop reading now.

    You can set the DropDown Items programatically, however, they will be the same for each row in the DGV (which doesn't fit your requirements). The only way I believe would be to:
    1. catch the event each time the selection changes in the DGV
    2. get the SrNo for that row in the DGV
    3. get the Value1 & Value2 from your table which correspond to the SrNo
    4. clear then add these values to the ComboBoxColumn's Items
    5. may need some sort of DGV refresh/invalidate/etc. to redraw the dropdown items

    This will set the Items for the entire DGVColumn, however, they should change every time you change rows.

  12. #12
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,853

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    He did say he wanted the value from column 3 so I think that means he only wants the one, though it is hard to tell when they do not give direct answers to questions
    Always use [code][/code] tags when posting code.

  13. #13
    Join Date
    Sep 2011
    Posts
    6

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    so what is the solution to my query

  14. #14
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,853

    Re: How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

    Look at the links I provided your answer is there
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center