Click here to Skip to main content
15,906,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m working on windows form.
1-----------------------------
i m using a grid view.
i want that when i select the grid view row.
the selected row data will be dispay in the text boxes.
and when i click the button of edit
it will save in data base.

2-----------------------
i want when i select the row
the slelected row should be deleted.


please help me out
to write this code

thanks and regard
subiya
Posted

Hi,

'I think this code will help u
'enter data to datagrid view
'in datagridview RowHeaderMouseDoubleClick event...

VB
Dim v_cellno As Integer
       Dim m_GrdRw() As String

       If DataGridView1.RowCount = 0 Then Exit Sub

       ReDim m_GrdRw(DataGridView1.ColumnCount)

       For v_cellno = 0 To DataGridView1.ColumnCount - 1
           m_GrdRw(v_cellno) = DataGridView1.Item(v_cellno, DataGridView1.SelectedCells(v_cellno).RowIndex).Value.ToString
       Next v_cellno
       TextBox1.Text = m_GrdRw(0)
       TextBox2.Text = m_GrdRw(1)
       TextBox3.Text = m_GrdRw(2)
       TextBox4.Text = m_GrdRw(3)
       TextBox5.Text = m_GrdRw(4)


       Dim rowToDelete As Int32 = Me.DataGridView1.Rows.GetFirstRow( _
       DataGridViewElementStates.Selected)
       If rowToDelete > -1 Then
           Me.DataGridView1.Rows.RemoveAt(rowToDelete)
       End If


' Here u can import the data of textboxes to your database
 
Share this answer
 
Comments
ahsan.subiya 6-Jun-11 6:46am    
tnx sir. it is working..
ahsan.subiya 6-Jun-11 7:03am    
bt now i want to save the data in the database
1. You need to use the datagrid's CurrentRow as shown in this sample

VB
Textbox1.text = datagridview1.currentrow.cells(i).value
'where i is the poistion in the row that you want get out


in regards to 2 you haven't given any details on how your handling your data so far? are you using a datalayer? tableadapters?
 
Share this answer
 
Add, Edit, and Delete in DataGridView with Paging[^]
Check this out, try something on your own, and then get back if you are stuck somewhere.
 
Share this answer
 
Hi Subiya,

Tell me pls. in witch database, for eg. MS Access, oracle, sql
Then i would try to solve it.

Thanks
 
Share this answer
 
Comments
ahsan.subiya 7-Jun-11 0:22am    
i m using ms sql
 
Share this answer
 
VB
Imports System.Data.SqlClient
Dim myConnection As SqlConnection = New SqlConnection()
Dim sql as string
myConnection.ConnectionString = 'your ConnectionString here
myConnection.Open()
sql = "insert into <tablename> (col1, col2,col3,col4)values('" & _
textbox1.text & "','" & textbox2.text & "','" & textbox3.text & "','" & _
textbox4.text & "')"
myConnection.execute sql
myConnection.Close()</tablename>
 
Share this answer
 
Comments
ahsan.subiya 7-Jun-11 6:25am    
sir i have alrdy done this in my project.
my problem is how to update the grid view row.
and how to delete the grid view row.
i have decided the senario to update the grid view row
what i want.
that when i select the grid view row the selected row data should be display in text boxes and when i cclick the edit button the updation made in the data base and grid view as well.. for this i write the code on edit button click
Try

'Dim conn As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cbse").ConnectionString)
Dim conn As New SqlClient.SqlConnection("Server=localhost;DataBase=cbse;Integrated Security=true")

conn.Open()
Dim cmd As New SqlCommand("spClassUpdate", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@strClassName", txtClass.Text)
cmd.Parameters.AddWithValue("@intSequence", txtSequence.Text)
Dim i As Integer
i = cmd.ExecuteNonQuery()
If (i > 0) Then
MessageBox.Show("Class updated Succsessfully")
BindGrid()
conn.Close()
End If
Catch ex As Exception

End Try

End Sub


and for deletion

when i click the row
and click on the delete button the should be deleted from grid view and database as well
please sir write the code for this .
i m new with windows form.
i have not so much idea abt it.
Sahu.Ashok 7-Jun-11 7:57am    
check in solutions
'on the button click event write the code to delete the grid row and database

VB
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
       Dim rowToDelete As Int32 = Me.DataGridView1.Rows.GetFirstRow( _
       DataGridViewElementStates.Selected)
       If rowToDelete > -1 Then
           Me.DataGridView1.Rows.RemoveAt(rowToDelete)
       End If
     '----------- This will delete ur grid row
     '-- I am not familiar with sql server and i think u know well to delete  
     'row from database
     ' U can use like this to update database

       Dim sql As String

       sql = "delete from table name where strClassName = '" &  txtClass.text & "'"

       connection.execute(sql)  ' connection is sqlconnection object



   End Sub
 
Share this answer
 
Hello.
I think You know who to fill a DatagridView??

You will get the selected rows with an Eventhandler:

VB
    Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick, DataGridView1.CellClick, DataGridView1.CellContentClick

...

'DS=Dataset=Datasource for the DatagridView
'In THins case DS contains only one table
'and the colum is known (In this example it is the colum with the ID 0)
'You can also use the COlum name instead 
'dim content as string = ds.Tables(0).Rows(Row).Item("ID")


Dim Row As Integer = e.RowIndex()
dim content as string = ds.Tables(0).Rows(Row).Item(0)



End Sub

So you can get every content form you DataGridView..


Problem Number 2:
Change Cells in Datagridview and write them back into DB:

MSIL
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection("YOUR SQL CONNECTION STRING")
        Dim sqlcmd As SqlCommand = New SqlCommand("YOUR SQL SEARCH = THE SAME AS FOR THE DATAGRID")

  sqlConnection.Open()

        Dim da As New SqlDataAdapter(sqlcmd)
        Dim dsDb As New DataSet
        da.Fill(dsDb)


        Dim cb As New SqlClient.SqlCommandBuilder(da)

        da.InsertCommand = cb.GetInsertCommand
        da.DeleteCommand = cb.GetDeleteCommand
        da.UpdateCommand = cb.GetUpdateCommand

'DA=Your New DataAdapter and dsDB = orginal Dataset form DB
'ds = your Old DataSet which is connected to the DatagridView

        Try

            da.Update(ds.Tables(0))
        Catch ex As Exception
            MessageBox.Show(Convert.ToString(ex))
        End Try

        sqlConnection.Close()

'REfresh Datagrid is an Method form my Programm.. I can recommend to refresh the datagrid now with the content form your database - so you can check if the command was succesfull
        refreshDatagrid()
    End Sub



Oh sry i Did't wrote your question. If you want to delete the selected row.. then the da.DeleteCommand = cb.GetDeleteCommand should be enough ;)


Best Regards,
Z
 
Share this answer
 
Hi,
You could use the events "RowEnter" or "RowHeaderMouseCIick using the event handler.(depends what exactly you need to do either add\modify data or delete row)
In the call back function this needs to be done.
You have to create a new form with test boxes as a child form and then get the text values(Depends on how many columns you have)to the text boxes(Which you should initialise during runtime.)
Or
You have to delete rows usings dataGridView1.Rows.Remove(DataGridViewRow rows)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900