Click here to Skip to main content
15,922,512 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionDoes any one know how to fix this adodc control problem? [modified] Pin
zhyuanshan23-Sep-07 21:56
zhyuanshan23-Sep-07 21:56 
QuestionWebbrowser Control Pin
shaikhmido23-Sep-07 17:29
shaikhmido23-Sep-07 17:29 
AnswerRe: Webbrowser Control Pin
Luc Pattyn24-Sep-07 0:26
sitebuilderLuc Pattyn24-Sep-07 0:26 
QuestionWarning 1: Unable to apply publish properties for item "axinterop.mscomctllib". ***HELP! Pin
vbDigger'z23-Sep-07 14:46
vbDigger'z23-Sep-07 14:46 
QuestionLost a .dll - VB 6.0 Pin
nnadams23-Sep-07 11:48
nnadams23-Sep-07 11:48 
AnswerRe: Lost a .dll - VB 6.0 Pin
prahlad198123-Sep-07 18:40
prahlad198123-Sep-07 18:40 
QuestionUpdate datagridview in vb.net Pin
Abbhie23-Sep-07 6:20
Abbhie23-Sep-07 6:20 
AnswerRe: Update datagridview in vb.net Pin
DigiOz Multimedia23-Sep-07 14:04
DigiOz Multimedia23-Sep-07 14:04 
Abbhie wrote:
can somebody help me and show me codes on how to save changes(in information) in datagridview connected to an SQL database. I was looking for codes ever since, but it doesn't work as i expected it to be.

I'm a beginner here, so I'm having a hard time creating the codes with datagridview.


Here is an example of how you can accomplish something like this:

Create two forms, call them Form1 and Form2. Put a DataGridView (DataGridView1) and a button (btnUpdateRow) on Form1. Create 3 textboxes on Form2 and call them txtId, txtFirstName and txtLastName.

Creating a sample dataset with 3 rows of data:

Public Table1 As DataTable<br />
<br />
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br />
        Table1 = New DataTable("People")<br />
        Dim Row1, Row2, Row3 As DataRow<br />
<br />
        Try<br />
            Dim Id As DataColumn = New DataColumn("Id")<br />
            Id.DataType = System.Type.GetType("System.Int32")<br />
            Table1.Columns.Add(Id)<br />
<br />
            Dim FirstName As DataColumn = New DataColumn("First Name")<br />
            FirstName.DataType = System.Type.GetType("System.String")<br />
            Table1.Columns.Add(FirstName)<br />
            Dim LastName As DataColumn = New DataColumn("Last Name")<br />
            LastName.DataType = System.Type.GetType("System.String")<br />
            Table1.Columns.Add(LastName)<br />
<br />
            Row1 = Table1.NewRow()<br />
            Row1.Item("Id") = 1<br />
            Row1.Item("First Name") = "Pete"<br />
            Row1.Item("Last Name") = "Soheil"<br />
            Table1.Rows.Add(Row1)<br />
<br />
            Row2 = Table1.NewRow()<br />
            Row2.Item("Id") = 2<br />
            Row2.Item("First Name") = "Bill"<br />
            Row2.Item("Last Name") = "Gates"<br />
            Table1.Rows.Add(Row2)<br />
<br />
            Row3 = Table1.NewRow()<br />
            Row3.Item("Id") = 3<br />
            Row3.Item("First Name") = "George"<br />
            Row3.Item("Last Name") = "Washington"<br />
            Table1.Rows.Add(Row3)<br />
<br />
            DataGridView1.DataSource = Table1<br />
        Catch ex As Exception<br />
            MessageBox.Show("Error: " & ex.Message)<br />
        End Try<br />
<br />
    End Sub<br />


Code to call the update Form2 and allow you to change row values:

Private Sub btnUpdateRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateRow.Click<br />
        Dim SelectedRowIndex As Integer<br />
        Dim SelectedRowCount As Integer = DataGridView1.SelectedRows.Count<br />
<br />
        If DataGridView1.SelectedRows Is Nothing Then<br />
            MessageBox.Show("Please Select a Row")<br />
            Exit Sub<br />
        End If<br />
<br />
        If SelectedRowCount < 1 OrElse SelectedRowCount > 1 Then<br />
            MessageBox.Show("Invalid Row Selection.")<br />
            Exit Sub<br />
        End If<br />
<br />
        Dim SelectedRow As DataGridViewRow = DataGridView1.SelectedRows.Item(0)<br />
        SelectedRowIndex = SelectedRow.Index<br />
<br />
        Dim f As New Form2<br />
<br />
        f.txtId.Text = SelectedRow.Cells(0).Value<br />
        f.txtFirstName.Text = SelectedRow.Cells(1).Value<br />
        f.txtLastName.Text = SelectedRow.Cells(2).Value<br />
        f.ShowDialog()<br />
<br />
        DataGridView1.Rows.Item(SelectedRowIndex).Cells(0).Value = f.txtId.Text<br />
        DataGridView1.Rows.Item(SelectedRowIndex).Cells(1).Value = f.txtFirstName.Text<br />
        DataGridView1.Rows.Item(SelectedRowIndex).Cells(2).Value = f.txtLastName.Text<br />
    End Sub


I used a DataTable for my example, but if you want to use a dataset instead all you have to do is to add the datatable to a dataset and you are good to go. Smile | :)

Pete Soheil
DigiOz Multimedia
http://www.digioz.com

GeneralRe: Update datagridview in vb.net Pin
Abbhie25-Sep-07 8:17
Abbhie25-Sep-07 8:17 
Questionupdating values at runtime... Pin
manni_n23-Sep-07 4:56
manni_n23-Sep-07 4:56 
QuestionReport gen.. Pin
SamRST23-Sep-07 4:40
SamRST23-Sep-07 4:40 
Questiondatagridview [modified] Pin
gt223-Sep-07 4:20
gt223-Sep-07 4:20 
AnswerRe: datagridview Pin
Paul Conrad23-Sep-07 4:33
professionalPaul Conrad23-Sep-07 4:33 
GeneralRe: datagridview Pin
SamRST23-Sep-07 4:41
SamRST23-Sep-07 4:41 
GeneralRe: datagridview Pin
Paul Conrad23-Sep-07 4:57
professionalPaul Conrad23-Sep-07 4:57 
GeneralRe: datagridview Pin
SamRST23-Sep-07 5:01
SamRST23-Sep-07 5:01 
Questionprintdialog control in vb.net2005 Pin
nilam1123-Sep-07 3:08
nilam1123-Sep-07 3:08 
QuestionHow to pass ftp path to VLC media player remotely Pin
juli Paris22-Sep-07 22:49
juli Paris22-Sep-07 22:49 
AnswerRe: How to pass ftp path to VLC media player remotely Pin
Dave Kreskowiak23-Sep-07 7:10
mveDave Kreskowiak23-Sep-07 7:10 
Questioncrystal report..need help Pin
SamRST22-Sep-07 20:23
SamRST22-Sep-07 20:23 
Answercrystal report..need help Pin
SamRST23-Sep-07 4:33
SamRST23-Sep-07 4:33 
QuestionAdd a text label to an image Pin
tomsteven22-Sep-07 14:54
tomsteven22-Sep-07 14:54 
AnswerRe: Add a text label to an image Pin
Guffa22-Sep-07 15:30
Guffa22-Sep-07 15:30 
GeneralRe: Add a text label to an image Pin
tomsteven22-Sep-07 15:44
tomsteven22-Sep-07 15:44 
AnswerRe: Add a text label to an image Pin
Christian Graus23-Sep-07 0:13
protectorChristian Graus23-Sep-07 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.