I have a gridview that is populated and a button outside the gridview that I want to enable editing on the selected row when clicked. I have this in the code behind. What goes in the button event to invoke the grid view editing?

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    FillGrid()
End Sub

Protected Sub btnEdit_Click(ByVal sender as Object, ByVal e As System.EventArgs) Handles btnEdit.Click

    What goes here??

End Sub

I'm not sure what arguments to pass to the GridView1_RowEditing from the btnClick. I don't want a column with an edit button on every row. I have a gridview with 5 columns. On the btnClick, I want 4 of the cells in the selected row to open as textbox for editing. I would like a seperate button outside the gridview that the user can click to enable editing of the selected row.

Please post your vb code and your question in the VB.NET section.

You're not using the sender parameter, so it can be anything (I'd just use Me). However, you are using the e parameter, so you need to fill out a new GridViewEventArgs object and pass it in:

Protected Sub btnEdit_Click(ByVal sender as Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    Dim editArgs As New GridViewEditEventArgs With {.NewEditIndex = row}

    GridView1_RowEditing(Me, editArgs)
End Sub

In this case, row would be whatever row you want to edit. You may want to use the first selected index, or loop through all selected indices and call the RowEditing handler for each, but that's entirely application specific.

Hi, this is what I figured out some time ago, for me it works ok.
To edit cells outside of the dgv event make use of textboxes.
Use the dgv cellclick event to read data from the selected row into them.
Edit the data in the textboxes and use the button to write back to the
selected row. ("Column1") is same as dgv column 0

Public Class Form1
Dim r As Integer

Private Sub DGView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGView1.CellClick
   r = DGView1.SelectedRows.Count
   TextBox1.Text = DGView1.Rows(r).Cells("Column1").Value
   TextBox2.Text = DGView1.Rows(r).Cells("Column2").Value
   ' and so on
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
   DGView1.Rows(r).Cells("Column1").Value = Nothing
   DGView1.Rows(r).Cells("Column1").Value = TextBox1.Text
   ' and so on
End Sub

thank you for ur help :).i already solved my problem. i have one more question is it possible if i save data from gridview and textboxes at the same time to sql database? and can u help me how to add a new row using button outside the gridview..

.my display items in gridview from sql stored procedure.it is joining two tables.
im using c# this time.

tnx :)

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.