Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a DataGridView unbound DataGridViewComboBox at run time

Status
Not open for further replies.

JScannell

Programmer
Jan 9, 2001
306
US
I'm about at the end of my rope. I have a datagrid view with several bound columns and one unbound one. When the form is loaded, the data adapter is called to fill the datagrid. Then I need to go through each row, run a SQL Query based on 3 outside values and the data in column 0. With the data I get from that, I need to populate the next column which is a Combo Box. Everything I have seen on the internet so far, pertains to the creation of a column, not the manipulation of an existing one.

Here is code that works, but I don't know how to transfer the items to the current dropdown:

For RowIndex = 0 To Me.PSDShangerDataSet.JobRelBom.Count - 1
Row = JobRelBomDataGridView.Rows(RowIndex)
cell = Row.Cells(0)
numLineItem = CInt(cell.Value)

' Function that returns a string array...
strBomShopOps = GetBomShopOps(JobNumber, ReleaseNumber, MarkNo, numLineItem, numRows)

cell = Row.Cells(1)
Dim tempCol2 As DataGridViewComboBoxCell = CType(Row.Cells(1), DataGridViewComboBoxCell)

'Populate tempCol2 with new data

If strBomShopOps.Length > 0 Then
For y = 0 To strBomShopOps.Length - 1
If strBomShopOps(y) > "" Then
tempCol2.Items.Add(strBomShopOps(y))
End If
Next

Dim numItems As Integer = tempCol2.Items.Count
Dim strValue As String = tempCol2.Items(0).ToString()

' Set the displayed value.
' This value needs to be present
' in the items collection

' Row.Cells(1) = tempCol2 <---- DOES NOT WORK
End If

Next

I initially presume that sfter I fill up the tempCol2, that I somehow have to transfer it to the Row.Cells(1) somehow, but that's where I'm stuck.

Thanks in advance,

Jerry Scannell
 
Here is the C# code I have done. You will need to define 1) gridrow; 2) a combox row cell in design mode; 3) datatable as your datasource for the combox.


DataGridViewRow dgvRow = gridViewRateSheetList.Rows[0];
DataGridViewComboBoxCell rowCell = (DataGridViewComboBoxCell)dgvRow.Cells[columnHeaderText];

DataTable dtRSC = app.GetRateSheetCodeByUnionID(columnHeaderText);

for (int i = 0; i < dtRSC.Rows.Count; i++)
{
if (dtRSC.Rows["Rate_Sheet_Code"].ToString().Trim() != rowCell.Items[0].ToString().Trim())
{
rowCell.Items.Add(dtRSC.Rows["Rate_Sheet_Code"].ToString().Trim()); //<- this will add the value to the combox in the grid
}
}
 
vbdbcoder,

Thanks for the response.

How do I implement item 3)? What datatable can I add? The contents of the columkn are based on a SQL query.

Thanks in advance,

Jerry Scannell
 
DataTable dtRSC = app.GetRateSheetCodeByUnionID(columnHeaderText); //<- this line builds the Datatable from a function call. You might create your own data table here

I use N-tier structure to build to datatable and expose it to a function. You can do however you can to create the table.
 
I meant this text in your original post: 3) datatable as your datasource for the combox.


Is this something I need to do at design time? If so, what do I put in the data entry screen? Under DataSource are all the BindingSources I have in my project. No where to simple put in "datatable" And even if I could, what is the name of the table?


Jerry Scannell
 
My app. is to allow users key data directly to the gridview. Several columns of the grid are combo box type, which are populated with records from a table (with criteria in sql to build a datatable from a function). Then the users can select from the drop-down combo box in those columns. Business scenario is a Job has multiple Job sheets to select from. 1 record in the grid is for 1 Job and the combo box will need to display all the Job sheets available. The users can select 1 job sheet per job.

You will have to do it at design time to specify the column type as drop-down combo box in the gridview. Then you populate that combo box column with data (from a datatable).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top