Select cells in datagridview{Urgent help}

Hihi.... I need help on the following problem.. Thanks in advance...ok my problem is.. i need to do a function that allow user to click on the cell of the datagrid and display the information of that row selected into both combo box and textbox.. I managed to do the part where user click on a cell details will be displayed... but how do i retrieve only the information of that row the cell belong to.... is there a way to do it in visual studio 2005 and sql server 2005.. really need help urgently.. thank you..~~



Answer this question

Select cells in datagridview{Urgent help}

  • palmirag

    This example shows one way of getting the contents out of a text box and into a textbox. It assumes you are allowing multiple cells to be selected - although this may not be the case - but the code would still work. As far as the combobox is concerned - if its a single items then you should be able to set the text property on the combobox with the value of the current selected cell and this should work.

    To retrieve other information on that row - maybe for items not selected you can obtain the rowindex property and then select the items collection item with that rowindex and examine the other items on this row.

    So check out
    Selectedcells Collection
    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells.aspx

    CurrentCellAddress
    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelladdress.aspx

    Multiselect Property
    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.multiselect.aspx

    SelectedCells
    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells.aspx

    DatagridviewCell Property
    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells.aspx

    Public Class Form1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '//This is just to set up the dataset for the datagridview
    DataSet1.Tables.Add()
    DataSet1.Tables(0).Columns.Add("column1")
    DataSet1.Tables(0).Columns.Add("column2")
    DataSet1.Tables(0).Columns.Add("column3")

    DataSet1.Tables(0).Rows.Add(1, 2, 3)
    DataSet1.Tables(0).Rows.Add(4, 5, 6)
    DataSet1.Tables(0).Rows.Add(7, 8, 9)

    '//Set the data source
    DataGridView1.DataSource = DataSet1.Tables(0)

    End Sub


    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
    '//Cant select multiple cells.
    If DataGridView1.SelectedCells.Count > 0 Then
    TextBox1.Text = ""
    For Each d As DataGridViewCell In DataGridView1.SelectedCells
    TextBox1.Text = TextBox1.Text & d.Value

    '//Column and Row Index of the currently selected cell
    Console.Write(d.RowIndex)
    Console.WriteLine(d.ColumnIndex)


    Next
    End If
    End Sub
    End Class



  • puromtec

    Each time you click on a row or cell (or on the grid, even) there is an event which ocurrs: select the most apporopriate event for what you want to detect.

    One of the parameters passed to the event is the row and column selected. Using these parameters you can retrieve the values from the cells.



  • FcoLomas

    Hi SJWhiteley, Thanks for the help.

    Yup, i have choosen to use CellMouseClick event...

    I managed to display the textbox with the item when select a cell.. By using tb_ttsProjNo.Text = dg_ttsDetails.CurrentRow.Cells(1).Value.

    But how can i display the details into combo boxes. And the combo box shld be load with the data from database in different tables. On selecting the cells the combo box will have to display the selected details and at the same time the boxes are loaded with the data from database. And the combo boxes will have to check some conditions. Is it possible in vs 2005


  • Select cells in datagridview{Urgent help}