Datagridview combobox column's value - vb.net

I have a datagridview combo box with a few values in there. I am trying to capture what user clicked. I tried CellValueChanged, CellContentClicked etc. but nothing works.
I want to store this value to a variable (important) and then shift the cursor to column after user has selected value.
Please help. Please also advise what event to fire.
Thanks
Private Sub dg_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellValueChanged
If flgLD = 1 Then
Dim retreivedValue As Object = dg.Rows(dg.CurrentRow.Index).Cells(0).Value
dg.Rows(dg.CurrentRow.Index).Cells(1).Value = retreivedValue
dg.CurrentCell = dg.Rows(dg.CurrentRow.Index).Cells(1)
dg.BeginEdit(True)
End If
End Sub

Use CellValueChanged Event, then check what Column is chosen by the user, then you can retreive the value. Here an example:
Private Sub yourGrid_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles yourGrid.CellValueChanged
If e.ColumnIndex = yourComboColumn.Index Then
Dim retreivedValue As Object = yourGrid(e.ColumnIndex, e.RowIndex).Value
End If
End Sub

Related

How to Transfer Datagrid Value of one form to TextBoxes of another form?

I cant somehow figure why it can't transfer the data with no errors
Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim pinfo As New Employee_Personal_Info()
pinfo.ShowDialog()
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
Employee_Personal_Info.TextBox1.Text = row.Cells("emp_firstname").Value.ToString
End If
End Sub
Apart from the fact that you're displaying the form before passing it the data, the issue is that you are setting the Text of a TextBox on the default instance of that form, not the instance you created. This:
Employee_Personal_Info.TextBox1.Text = row.Cells("emp_firstname").Value.ToString
should be this:
pinfo.TextBox1.Text = row.Cells("emp_firstname").Value.ToString
you can pass via globle variable like
public text as string
Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
text=row.Cells("emp_firstname").Value.ToString
End Sub

Passing DataGridView selected value to a text box

Private Sub DataGridViewMachine_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewMachine.CellContentClick
frmAddNewWrkReq.txtbxAssetName.Text = DataGridViewMachine.SelectedRows(DataGridViewMachine.CurrentRow.Index).Cells(1).Value.ToString
Me.Hide()
End Sub
How do I get the selected value from the datagridview on to the text box?
Try this code:
frmAddNewWrkReq.txtbxAssetName.Text = DataGridViewMachine.CurrentRow.Cells("name_of_the_cell").Value.ToString
May be necessary set SelectMode=FullRowSelect on DataGridViewMachine
you can try this code
frmAddNewWrkReq.txtbxAssetName.Text = DataGridViewMachine.Rows(DataGridViewMachine.CurrentRow.Index).Cells(1).Value.ToString

how to find footer's value of gridview on RowCommand event?

I want to find footer's value on the event of RowCommand in vb.net??
I'm assigning footer's value run-time on RowCreated.
e.Row.Cells(2).Text = FOOTERVALUE
e.Row.Cells(1).Text = "Total Subjects"
And this footer is for Parent Grid. I'm using nested gridview.
Protected Sub Gridview1RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview1.RowCommand
Dim Lblcolval = CType(dgrd_WWWH.FooterRow.FindControl("yourcolumn") ,Label)
End Sub
Protected Sub Gridview1_How_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
If e.Row.RowType And DataControlRowType.Footer Or (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlresp As DropDownList = CType(e.Row.FindControl("ddlResp"), DropDownList)
SrchWhoBind(ddlresp)
ddlresp.SelectedValue = Convert.ToInt32(Session("Uname"))
End If
End Sub
I got the solution, just needed to write the code in rowDataBound instead of rowCreated.

Getting the X/Y coordinates of the "current cell" in a DataGridView?

[using VB 2010 / Winforms]
I have a DataGridView with several columns. It's unbound, and not connected to any sort of database or anything -- I am simply populating it cell by cell based on user input.
So anyways, one of the columns in the DGV is of type "image" (DataGridViewImageColumn).
What I'm trying to do is whenever one of the image cells is clicked, a context menu strip is shown at the exact loaction where the image cell is clicked.
Here's what I've got so far...
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name
If columnName = "Image" Then
Me.Status_ContextMenuStrip1.Show(Me.DataGridView1.CurrentCell.ContentBounds.Location) ' <-- This isn't right, but I must be close!
End If
End Sub
When I run the above code and click on an image cell, the context menu appears, but it appears at the very top left corner of the screen. How can I get it to appear at the exact location that the clicked cell is at? I'd actually like it to appear just below the clicked cell, so that it has a similar visual effect to a combobox "drop down" (and I know how to offset the X and Y coordinates as soon as I can figure out how to get it in the general vicinity of where it needs to be).
Thanks!
Try the following code
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name
If columnName = "Image" Then
Dim RowHeight1 As Integer = DataGridView1.Rows(e.RowIndex).Height
Dim CellRectangle1 As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)
CellRectangle1.X += DataGridView1.Left
CellRectangle1.Y += DataGridView1.Top + RowHeight1
Dim DisplayPoint1 As Point = PointToScreen(New Point(CellRectangle1.X, CellRectangle1.Y))
ContextMenuStrip1.Show(DisplayPoint1)
End If
End Sub
For anyone struggling with this in future - this is what really works:
'For forms
Dim f as New Form2
f.Location = DGV.PointToScreen(DGV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Location)
For case of this post:
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As
Dim DisplayPoint1 As Point = DGV.PointToScreen(DGV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Location)
ContextMenuStrip1.Show(DisplayPoint1)
End Sub
Try to change your code like this ..
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name
If columnName = "Image" Then
DataGridView1.CurrentCell = dgvDataDaftar.Rows(e.RowIndex).Cells(e.ColumnIndex)
Me.Status_ContextMenuStrip1.Show(dgvDataDaftar, DataGridView1.PointToClient(Windows.Forms.Cursor.Position))
End If
End Sub

Declaring string and condition in vb

I have a String variable in this page named "a".
I wanted the scenario to be.
When the page is started "a" will be null.
But when the user selected an entry from the DetailView Control "a" will become "have".
The following is my code. But i keep getting "a" = null even though i have selected an entry from the detailView control.
Dim a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
a = "a"
End Sub
Why is this so? How should i go about doing it?
When ever you do any operation on you page with server side controls and your page postback, all the variables which are declared globally are again reset and go at their initial stage, so that's why you are getting a = null every time.
The code is ok, but you are changing the value when the selected index changes on a gridview, not a detailsview, also values are not stored through postbacks. If you assign the gridview selected value to a label for example, and viewstate is active, then it will be maintained on that control. But variables on the VB are reset on each postback.
Anyway for doing that you can update the string on the Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
a = "a" 'or whatever value you need. i.e. the gridview selected data key, etc...
End Sub
Or if its based on what the user does, add the page.ispostback
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
a = "a" 'or whatever value you need. i.e. the gridview selected data key, etc...
'or for example...
a = Me.aDropDown.SelectedItem.Text
Else
a = String.Empty
End If
End Sub
If not, "a" will allways be equal to string.empty each time the page loads.