.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

DataGridView selected row

Posted By: srini     Posted Date: February 03, 2011    Points:2   Category :VB.Net
Hi,

I have a code for edit the DataGridView Selected Row. But its showing very first record instead of showing second,third record and so on.

I used the code like,

Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

Dim i As Integer
i = DataGridView1.SelectedCells(0).RowIndex
txtPer.Text = DataGridView1.Rows(i).Cells(0).Value.ToString()
txtLas.Text = DataGridView1.Rows(i).Cells(1).Value.ToString()
txtFir.Text = DataGridView1.Rows(i).Cells(2).Value.ToString()
txtAdd.Text = DataGridView1.Rows(i).Cells(3).Value.ToString()
txtCity.Text = DataGridView1.Rows(i).Cells(4).Value.ToString()
End Sub


I placed the code in the form_load event also. Its not showing other records except First record.

Please help me!


Thanks,
Srini.


Responses
Author: Syed Shakeer Hussain             
Posted Date: February 04, 2011     Points: 5   

Hi srini,
Use the below code to get the DataGrid selected rowIndex 

int i = dataGridView1.CurrentCell.RowIndex


Pass the RowIndex i value to get the DataGrid Row Values

txtPer.Text = DataGridView1.Rows(i).Cells(0).Value.ToString()
txtLas.Text = DataGridView1.Rows(i).Cells(1).Value.ToString()
txtFir.Text = DataGridView1.Rows(i).Cells(2).Value.ToString()
txtAdd.Text = DataGridView1.Rows(i).Cells(3).Value.ToString()


Author: srini             
Posted Date: February 04, 2011     Points: 5   

Hi Syed,

I tried this code. But it doesnt work. Its showing very first record in DataGridView. I want to select second, third record and so on...

Dim i As Integer
i = DataGridView1.CurrentCell.RowIndex
txtPer.Text = DataGridView1.Rows(i).Cells(0).Value.ToString()
txtLas.Text = DataGridView1.Rows(i).Cells(1).Value.ToString()
txtFir.Text = DataGridView1.Rows(i).Cells(2).Value.ToString()
txtAdd.Text = DataGridView1.Rows(i).Cells(3).Value.ToString()
txtCity.Text = DataGridView1.Rows(i).Cells(3).Value.ToString()

Please help me to solve this!!!!!!!!!

Thanks,
Srini

Srini
Author: Syed Shakeer Hussain             
Posted Date: February 04, 2011     Points: 5   

Hi,


Use the code as follows.You will get vales based on RowIndex.

txtPer.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString()
txtLas.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString()
txtFir.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString()
txtAdd.Text = DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString()


Note:To get Values,you have to click on the text.If you click outside the text containing in datagrid,you wont get RowIndex.

Author: srini             
Posted Date: February 04, 2011     Points: 5   

Hi syed,

Its showing error on e.RowIndex.


Help me!



Srini
Author: Syed Shakeer Hussain             
Posted Date: February 05, 2011     Points: 5   

Hi srini,
I tested the above code.its working.In which DataGrid event you had put the code.

Double click your DataGrid and write your code in it.


Author: srini             
Posted Date: February 05, 2011     Points: 5   

This is my code:

Imports System.Data.SqlClient
Public Class User_Edit
Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra As Integer

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=BillMasterDB;Integrated Security=True")
myCommand = New SqlCommand("update Tbl_UserDetails set Usr_Fname='" + txtFname.Text + "',Usr_Lname='" + txtLname.Text + "',Usr_Gender='" + txtGender.Text + "',Usr_DOB='" + txtDOB.Text + "',Usr_Address1='" + txtAddr1.Text + "',Usr_Address2='" + txtAddr2.Text + "',Usr_City='" + txtCity.Text + "',Usr_State='" + txtState.Text + "',Usr_Country='" + txtCountry.Text + "',Usr_Zip='" + txtZip.Text + "',Usr_Email='" + txtEmail.Text + "',Usr_Phone='" + txtPhone.Text + "',Usr_Mobile='" + txtMobile.Text + "' where Usr_Fname='" + txtFname.Text + "' ", myConnection)
Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=BillMasterDB;Integrated Security=True")
myCommand = New SqlCommand("update Tbl_User set Usr_Type='" + txtUsertype.Text + "',Usr_Pwd='" + txtPass.Text + "' where Usr_Name= '" + txtUsername.Text + "' ", myConnection)
Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
MessageBox.Show("Updated")
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

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

Dim conn As SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
conn = New SqlConnection("server=.\SQLExpress;Initial Catalog=BillMasterDB;Integrated Security=True")
conn.Open()
da = New SqlDataAdapter("select Tbl_User.Usr_Name,Tbl_User.Usr_Type,Tbl_User.Usr_Pwd,Tbl_UserDetails.Usr_Fname,Tbl_UserDetails.Usr_Lname,Tbl_UserDetails.Usr_Gender,Tbl_UserDetails.Usr_DOB,Tbl_UserDetails.Usr_Address1,Tbl_UserDetails.Usr_Address2,Tbl_UserDetails.Usr_City,Tbl_UserDetails.Usr_State,Tbl_UserDetails.Usr_Country,Tbl_UserDetails.Usr_Zip,Tbl_UserDetails.Usr_Email,Tbl_UserDetails.Usr_Phone,Tbl_UserDetails.Usr_Mobile from Tbl_User inner join Tbl_UserDetails on Tbl_User.Usr_Name=Tbl_UserDetails.Usr_Fname", conn)

ds = New DataSet("UserEdit")
da.Fill(ds, "UserEdit")
DataGridView1.DataSource = ds.Tables("UserEdit")
conn.Close()


txtUsername.Text = User_Management.DataGridView1.CurrentRow.Cells(0).Value.ToString()
txtUsertype.Text = User_Management.DataGridView1.CurrentRow.Cells(1).Value.ToString()
txtPass.Text = User_Management.DataGridView1.CurrentRow.Cells(2).Value.ToString()
txtFname.Text = User_Management.DataGridView1.CurrentRow.Cells(3).Value.ToString()
txtLname.Text = User_Management.DataGridView1.CurrentRow.Cells(4).Value.ToString()
txtGender.Text = User_Management.DataGridView1.CurrentRow.Cells(5).Value.ToString()
txtDOB.Text = User_Management.DataGridView1.CurrentRow.Cells(6).Value.ToString()
txtAddr1.Text = User_Management.DataGridView1.CurrentRow.Cells(7).Value.ToString()
txtAddr2.Text = User_Management.DataGridView1.CurrentRow.Cells(8).Value.ToString()
txtCity.Text = User_Management.DataGridView1.CurrentRow.Cells(9).Value.ToString()
txtState.Text = User_Management.DataGridView1.CurrentRow.Cells(10).Value.ToString()
txtCountry.Text = User_Management.DataGridView1.CurrentRow.Cells(11).Value.ToString()
txtZip.Text = User_Management.DataGridView1.CurrentRow.Cells(12).Value.ToString()
txtEmail.Text = User_Management.DataGridView1.CurrentRow.Cells(13).Value.ToString()
txtPhone.Text = User_Management.DataGridView1.CurrentRow.Cells(14).Value.ToString()
txtMobile.Text = User_Management.DataGridView1.CurrentRow.Cells(15).Value.ToString()


End Sub
End Class


if i run this code,i get a error message like"Object reference not set to an instance of an object" in the line txtUsername.Text = User_Management.DataGridView1.CurrentRow.Cells(0).Value.ToString().

Help me!

Srini
Author: Syed Shakeer Hussain             
Posted Date: February 05, 2011     Points: 5   

Hi,
To avoid your error,Please write the code as follows

if(User_Management !=Null)
{


txtUsername.Text = User_Management.DataGridView1.CurrentRow.Cells(0).Value.ToString()
txtUsertype.Text = User_Management.DataGridView1.CurrentRow.Cells(1).Value.ToString()
txtPass.Text = User_Management.DataGridView1.CurrentRow.Cells(2).Value.ToString()
txtFname.Text = User_Management.DataGridView1.CurrentRow.Cells(3).Value.ToString()
txtLname.Text = User_Management.DataGridView1.CurrentRow.Cells(4).Value.ToString()
txtGender.Text = User_Management.DataGridView1.CurrentRow.Cells(5).Value.ToString()
txtDOB.Text = User_Management.DataGridView1.CurrentRow.Cells(6).Value.ToString()
txtAddr1.Text = User_Management.DataGridView1.CurrentRow.Cells(7).Value.ToString()
txtAddr2.Text = User_Management.DataGridView1.CurrentRow.Cells(8).Value.ToString()
txtCity.Text = User_Management.DataGridView1.CurrentRow.Cells(9).Value.ToString()
txtState.Text = User_Management.DataGridView1.CurrentRow.Cells(10).Value.ToString()
txtCountry.Text = User_Management.DataGridView1.CurrentRow.Cells(11).Value.ToString()
txtZip.Text = User_Management.DataGridView1.CurrentRow.Cells(12).Value.ToString()
txtEmail.Text = User_Management.DataGridView1.CurrentRow.Cells(13).Value.ToString()
txtPhone.Text = User_Management.DataGridView1.CurrentRow.Cells(14).Value.ToString()
txtMobile.Text = User_Management.DataGridView1.CurrentRow.Cells(15).Value.ToString()

}


Author: srini             
Posted Date: February 05, 2011     Points: 5   

Hi,

Its showing error on "User_Management !=Null".

If (User_Management ! = Null) Then


txtUsername.Text = User_Management.DataGridView1.CurrentRow.Cells(0).Value.ToString()
txtUsertype.Text = User_Management.DataGridView1.CurrentRow.Cells(1).Value.ToString()
txtPass.Text = User_Management.DataGridView1.CurrentRow.Cells(2).Value.ToString()
txtFname.Text = User_Management.DataGridView1.CurrentRow.Cells(3).Value.ToString()
txtLname.Text = User_Management.DataGridView1.CurrentRow.Cells(4).Value.ToString()
txtGender.Text = User_Management.DataGridView1.CurrentRow.Cells(5).Value.ToString()
txtDOB.Text = User_Management.DataGridView1.CurrentRow.Cells(6).Value.ToString()
txtAddr1.Text = User_Management.DataGridView1.CurrentRow.Cells(7).Value.ToString()
txtAddr2.Text = User_Management.DataGridView1.CurrentRow.Cells(8).Value.ToString()
txtCity.Text = User_Management.DataGridView1.CurrentRow.Cells(9).Value.ToString()
txtState.Text = User_Management.DataGridView1.CurrentRow.Cells(10).Value.ToString()
txtCountry.Text = User_Management.DataGridView1.CurrentRow.Cells(11).Value.ToString()
txtZip.Text = User_Management.DataGridView1.CurrentRow.Cells(12).Value.ToString()
txtEmail.Text = User_Management.DataGridView1.CurrentRow.Cells(13).Value.ToString()
txtPhone.Text = User_Management.DataGridView1.CurrentRow.Cells(14).Value.ToString()
txtMobile.Text = User_Management.DataGridView1.CurrentRow.Cells(15).Value.ToString()

End If

Srini


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend