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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right click on datagridview to edit cell 1

Status
Not open for further replies.

jrig

Technical User
Sep 5, 2006
36
US
I'm adding data into a dgv's cells via code. The data in the cell looks like "A/CV/D" (fields separated by forward slash. I would like for the user to be able to right-click on this cell and be presented with a small form that displays each individual field (i.e. A - CV - D) as well as a checkbox for each that the user can click to remove that value from the cell. Cell is read-only, can only be modified w/code

I haven't been able to work up any code for this that works, so any input would be appreciated. TIA-

jr
 
Have you tried using the CellMouseClick event and checking to see if the right mouse button was clicked, and then creating a new "form" that has the data cells that you need?

-The answer to your problem may not be the answer to your question.
 
Thanks for the quick response, yeah, I got that far. Guess I should have been more specific. I've gotten it down to where I have populated an array based on the cell value.

But I have no idea how to populate controls at run time based off this array other than it will involve some sort of 'for' loop. Here's what I have
Code:
If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.ColumnIndex = 0 Then
        If DataGridView1.Rows(i).Cells(j).Value <> "" Then
            Dim sCodes() As String = DataGridView1.Rows(i).Cells(j).Value.ToString.Split("/")
            For h = 0 To UBound(sCodes)
   
                    MsgBox(sCodes(h))
            Next
        End If
end if
 
try: (pseudo code:)

If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.ColumnIndex = 0 Then
If DataGridView1.Rows(i).Cells(j).Value <> "" Then
Dim sCodes() As String = DataGridView1.Rows(i).Cells(j).Value.ToString.Split("/")
Dim oForm as new Form1
For h = 0 To UBound(sCodes)
oForm = New Form1
oForm.TextBox1.Text = sCodes(0)
oForm.TextBox2.Text = sCodes(1)
oForm.Show()
'do processing here...

Next
End If
end if



-The answer to your problem may not be the answer to your question.
 
Qik-

Not sure what I'm doing wrong here, but the code you posted gives me the error-
Error 1 'TextBox2' is not a member of 'Repair.Form1'.

Seems like it's trying to modify an existing control. I think that if I can get the code to add controls and their event handlers at run time based off an ARRAY, I should be good to go on this one.

Any ideas? Thanks again-

jr
 
You should already have the form made, with the 3 textboxes that you need for the data in the datagrid.

I gave you "pseudo" code, which means, that the concept for what you need is there, not the actual code needed.

Every single form you have is an object, just like a string. you can have more than one if you need it.

What you are doing is saying,

Give me another "form1" and "pre-load" it's textboxes with the following values before i show it to the user.

You will need some form of close or commit button on your child form and will need to access the form after you are done with it, to get the data, that the user just entered, to update your datagrid.


-The answer to your problem may not be the answer to your question.
 
Thanks again. I understand what your code is doing, but I don't think it's quite what I need. I won't actually know how many entries there will be in the field. Could be one, or could be 10.

How can I create controls and their handlers at runtime?? I've seen dozens of examples of how to add one control but nothing on adding multiple controls at the same time.
 
OK wasn't so bad afterall:
Code:
    Private Sub frmDataGrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim iCount As Integer
        Dim x, y As Integer
        Dim c As Control

        Dim sCodes() As String = {"FIRST", "SECOND", "THIRD", "FOURTH", "LAST"}

        For iCount = 0 To UBound(sCodes)
            c = New Button()
            c.Name = sCodes(iCount)
            c.Text = sCodes(iCount)
            c.Location = New System.Drawing.Point(x, y)
            Me.Controls.Add(c)
            AddHandler c.Click, AddressOf c_click
            y += 25
        Next

        FillDataInGrids()
    End Sub
    Private Sub c_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(sender.ToString)
    End Sub
still working on a way to figure out which control was clicked, but after that I'm good-
 
As long as your handler will always deal with the same type of object then you can do something like:
Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim a As Button
        a = sender
        MsgBox(a.Name)

-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top