I was wondering if I can fill up an edited row based on the value in my Label.
Here's the scenario: An employee will add and edit rows in the DataGridView1. All
rows involved in adding and editing will fill up the last row with the Employee ID
of the Employee that made the changes based in their EmpID stored in Label from
LoginForm. I don't have a clue where to start

Recommended Answers

All 7 Replies

Question: is the datagridview connected to a database?
if you have a database like access or mssql,
you would like to have another table that logs employees who made changes.

to know that the employee has made changes on some row call on datagridview.CellEndEdit

Private Sub datagridview_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datagridview_CellEndEdit.CellEndEdit
       datagridview.Rows(row_index).Cells(col_index).Value = employeeid
End Sub

index should be replaced with the row number and columnnumber you want

can I declare it this way

datagridview.Columns(col_index) = employeeid

instead of this

datagridview.Rows(row_index).Cells(col_index).Value = employeeid

oh yeah I forgot yes it is connected to the database and How can I make the DGV to put the employeeid on all dirty rows

Hi,

If you used a DataAdaptor to supply the datasource, you can specify the Insert Update and Delete Commands then apply the Update method to push the changes over to the database. DataAdaptor Updates To add a new row to your DataGridView you can either pass the "default" values in as an array:

dim DGVRow as New DataGridViewRow (Cell1value, Cell2value, .... LastCellValue)

MyDataGridView.Rows.Add(DGVRow)

OR Clone an existing row already in the grid.

Dim DGVRow as DataGridViewRow 

DGVRow = MyDataGridView.Rows(0).Clone

MyDataGridView.Rows.Add(DGVRow)

Problem solved!
I used this on CellEndEdit

DataGridView1.Rows(e.RowIndex).Cells(9).Value = EmpID

9 indicates the 10th column as the index will start on 0 instead of 1

Public Class GridSample

Private Sub GridSample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim dt As New DataTable

    dt.Columns.Add("Column1")
    dt.Columns.Add("Column2")
    dt.Columns.Add("Column3")

    Dim dr = dt.NewRow
    dr("Column1") = "1"
    dr("Column2") = "AA"
    dr("Column3") = "AAA"
    dt.Rows.Add(dr)

    dr = dt.NewRow
    dr("Column1") = "2"
    dr("Column2") = "BB"
    dr("Column3") = "EDIT"
    dt.Rows.Add(dr)

    dr = dt.NewRow
    dr("Column1") = "3"
    dr("Column2") = "CC"
    dr("Column3") = "CCC"
    dt.Rows.Add(dr)

    DataGridView1.DataSource = dt
    ' Make sure to set edit mode
    DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
End Sub

Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
    If DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString = "EDIT" Then
        DataGridView1.BeginEdit(True)
    End If
End Sub

End Class

The problem was already solved but thanks for dropping by and sharing your ideas. I'll use your codes for future reference

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.