msksurfer

hi,

i'm trying to get the individual data from the datagrid after an user has inserted data into a particular cell. can anyone advise me on how to extract it the example projects i saw online usually are about directly extracting and putting it into database. what i want it to do it step by step. first extract the data and use variables to store then transfer the variables to database to add or update. thanx



Re: Windows Forms General extracting data from datagrid

Gavin Jin - MSFT

I made a small sample before,drag a textox and a datagridview to your form,and paste follow code to your application,hope it helps

Imports System.Data

Imports System.Data.SqlClient

Public Class Form1

Inherits Form

Dim Conn As New SqlConnection("Server = .;Database = NorthWind; Integrated Security = SSPI;")

Dim pub As New SqlCommand("select * from Categories", Conn)

Dim da As New SqlDataAdapter()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Conn.Open()

Dim dt As New DataTable

da.SelectCommand = pub

da.Fill(dt)

Me.DataGridView1.DataSource = dt

Me.TextBox1.DataBindings.Add("Text", dt, "Description")

Conn.Close()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim id As String

id = Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value.ToString

Dim myCommandUpdate As SqlCommand = Conn.CreateCommand

myCommandUpdate.CommandText = "UPDATE Categories SET Description = '" + TextBox1.Text + "' WHERE CategoryID= '" + id + "'"

Conn.Open()

myCommandUpdate.ExecuteNonQuery()

Conn.Close()

End Sub

End Class






Re: Windows Forms General extracting data from datagrid

msksurfer

sorry, but i'm using C# with Visual Studio 2003, it doesn't comes with the datagridview control.



Re: Windows Forms General extracting data from datagrid

Zhi-Xin Ye - MSFT


Code Snippet

private void button3_Click(object sender, System.EventArgs e)

{

string Value = this.dataGrid1[1,0].ToString();

//The value in the cell[ row1, column0]

MessageBox.Show(Value);

}






Re: Windows Forms General extracting data from datagrid

msksurfer

thanx alot, this is what i wanted.