DATAGRIDVIEW CLICK

hello!

using datagridview, when i press/click one row, how will the selected item's data go to textbox for editing? then i'll return it to datagrid view manually thru SQL insert command or is there any other way like populating it using ADO in dataset?

I use the following code. but it always display the first record and not the selected record

Try
TxtEmpID.Text = EmployeeDataSet.Tables("Employee").Rows(intIndex).Item("EmpID").ToString.ToUpper
TxtEmpName.Text = EmployeeDataSet.Tables("Employee").Rows(intIndex).Item("EmpName")
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try

thanks

[681 byte] By [medel] at [2008-1-9]
# 1

Hi . Check your variable called " intIndex " . the problem may arises from it .

Pr.wa'el at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

You do not need to go back to the dataset to retrieve the row data. If a row being selected in the datagridview is what is triggering the textboxes being filled then you should make that connection as direct as possible. Here is some code that uses the RowEnter event of the datagridview to fill in the values of the textboxes.

Code Snippet

Private Sub DatagridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DatagridView1.RowEnter

'I chose the RowEnter event but there may be a more appropriate one.

If e.RowIndex <> -1 And e.ColumnIndex <> -1 Then

Try

TxtEmpID.Text = DatagridView1.Rows(e.RowIndex).Cells("EmpID").Value.ToString.ToUpper

TxtEmpName.Text = DatagridView1.Rows(e.RowIndex).Cells("EmpName").Value

Catch ex As Exception

MessageBox.Show(ex.Message.ToString)

End Try

End If

End Sub

Once you are done with the editing you should have some way of finding that row again to update the info (if it can be edited). I don;t know where you were getting the intIndex value from, but an index in a datagridview row may not match the underlying table's index if the datagridview has been sorted. Keep it in mind. Having a unique key on your table shoudl allow you to locate the row at any time.

DigBoy2000 at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Sir, thank you for your reply, i follow your procedure but i get an error

"Column named EmpName cannot be found.
Parameter Name: Column Name?

but i do have EmpName on my table and EmpID is indexed. i am populating my datagrid thru dataset any reason for this sir? please bear with me, this is my first time to use dataset, and also my first to use .NET i am using vb6 listview before.

medel at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

medel,

According to the error message, I would like to provide you the following suggestions:

1. Please make sure you use the DataTable with the DataSet correctly. The following code snippet provide you an example on the issue:

Code Snippet

Dim ds As New DataSet

Dim strConn As String

Dim da, daProducts As SqlDataAdapter

Dim conn As SqlConnection

strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select orderid, productid from [Order Details]", conn)

daProducts = New SqlDataAdapter("Select * from Products", conn)

da.Fill(ds, "Orders")

ds.Tables("Orders").Columns.Add(New DataColumn("SortOn"))

daProducts.Fill(ds, "Products")

ds.Tables("Products").PrimaryKey = New DataColumn() {ds.Tables("Products").Columns("ProductID")}

DataGridView1.DataSource = ds.Tables("Orders")

For Each dr As DataRow In ds.Tables("Orders").Rows

dr.BeginEdit()

Dim drFind As DataRow = ds.Tables("Products").Rows.Find(dr.Item("ProductID"))

dr.Item("SortOn") = drFind.Item("ProductName").ToString

dr.EndEdit()

Next

2. You can also take a look at the further information on DataSet and DataTable controls in ADO.NET.

Hope that can help you.

BrunoYu-MSFT at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
sir, thank you for your reply. i am able to display my data on the text box from dataset using this

TxtEmpID.Text =IIf(Expression:=IsDBNull(EmployeeDataSet.Tables("Employee").Rows(EmployeeDataGridView.CurrentCell.RowIndex.ToString)("EmpID")), TruePart:="", FalsePart:=EmployeeDataSet.Tables("Employee").Rows(EmployeeDataGridView.CurrentCell.RowIndex.ToString)("EmpID"))

TxtEmpName.Text =IIf(Expression:=IsDBNull(EmployeeDataSet.Tables("Employee").Rows(EmployeeDataGridView.CurrentCell.RowIndex.ToString)("EmpName")), TruePart:="", FalsePart:=EmployeeDataSet.Tables("Employee").Rows(EmployeeDataGridView.CurrentCell.RowIndex.ToString)("EmpName"))

now my problem is if i click the column header to sort columns and click again the data, textbox is not displaying the selected item, instead it displays the data which corresponds to data on my access table.

example:

ID Emp_Name
1 2nd Name
2 1st Name
3 3rd Name

if i click colmn header to sort Emp_name, it will place Emp_Name="1st Name" into the 1st row

ID Emp_Name
2 1st Name
1 2nd Name
3 3rd Name

and i will click/ select Emp_Name="1st Name". on the text box it will display "2nd Name"s data which on my access table it is on the 1st one, is there any way for this?

also, if i select row and it happens that one item is null at it will display on dtPicker, how can i make the dtPicker.value="null" because it always displays the current date.

medel at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

The reason you are getting the wrong record showing is that, when you sort the datagrid by clciking on the column header, you are changing the way the records are displayed on the grid but not changing how teh ercords are ordered in the data table that serves as the grid's datasource. In your example above you are using the index of the row for the grid's current cell to find a record in the datatable -- the two are not synched anymore once you sort. You need to reference the ID number of the grid's current cell's row then find the datarow in the underlying table.

The answer to your second question about the datetimepicker allowing nulls -- not that I've ever seen. For an earlier project I had to make a custom control to display an empty string in a textbox to handle null date values. although I'd be very interested to see if anyone has found a way to display nulls without a custom control.

DigBoy2000 at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
Sir, can you help me reference the ID number of my grid's value against the table?
medel at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

Based on the few details of your project I've seen in your posts here's what may work for you.

Code Snippet

If EmployeeDataGridView.Rows(EmployeeDataGridView.CurrentCell.RowIndex).Cells("EmpID").Value.GetType Is GetType(DBNull) Then

TxtEmpID.Text = ""

TxtEmpName.Text = ""

Else

Dim EmpId As Int32 = EmployeeDataGridView.Rows(EmployeeDataGridView.CurrentCell.RowIndex).Cells("EmpID").Value

For Each row As DataRow In EmployeeDataSet.Tables("Employee").Rows

If row("EmpId") = EmpId Then

TxtEmpID.Text = EmpId

TxtEmpName.Text = row("EmpName")

End If

Next

End If

DigBoy2000 at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
sir thank you on your immediate response. i followed your code, modifying

If row("EmpId") = EmpId Then

TxtEmpID.Text = EmpId

TxtEmpName.Text = row("EmpName")&""

End If

and it runs ok but certain employee in which EmpID have character on them. ex AC/1003. i got an error.

Conversion from string "AC/1003" to type 'Double' is not valid.

edit: additional

then also, i have checkbox and on access the property is set to Yes/No. how will the value of data display on check box.

medel at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

My apologies -- I made the assumption that your Id field was an autogenerated integer value.

You should always be aware of what the expected data type is of the incoming data and set your local variables accordingly. I'm not sure how you are trying to test a double variable -- did you set EmpId to a double type?

Regardless, If your EmpId field is a varchar or char type then you need to set the local EmpId field to a string type:

Dim EmpId As String = EmployeeDataGridView.Rows(EmployeeDataGridView.CurrentCell.RowIndex).Cells("EmpID").Value

As for checkboxes, you set the Checked property of the control to either True or False. So for example if you have a field in your table named "IsActive", then -- borrowing the code I wrote before -- you could do somethng like this:

If row("EmpId") = EmpId Then

TxtEmpID.Text = EmpId

TxtEmpName.Text = row("EmpName")

CheckBox1.Checked = Cbool(row("IsActive"))

End If

'NOTE: This will throw an exception if the datarow value is null, so always test for DbNull or add an IsNull() conversion to you original Sql query (or disallow nulls in your Db tables)

DigBoy2000 at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11
that really does the work sir, thank you very much, im trying your suggestion for the checked box code. thank you again sir

medel at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...