Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of alicealiciasarawak
alicealiciasarawak

DataGridView editable with textbox and enter key to move to another column textbox
I want to create a datagridview as below:

Name     Age          Height         Bust       Waist
Alice      20             157               40            28
Ally        25              160              38            24
......

There are 25 rows for user to key in the age, height, bust and waist. I want it to be only 25 rows, how to set this, meaning to say that there are only 25 rows for users to key data. The fields Age, Height, Bust and Waist allow user to key in the data with textboxes.

I want it to be when user key in the age of Alice and press Enter, it will move to the Height of Alice, and when it reach the last Column Waist and user key in the data and press Enter, it will automatically move to the next Row, Ally's Age textbox.

Can anyone help to give an example for this? I really need help.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Deepak LakkadDeepak Lakkad🇮🇳

hi

check the attached files for your requirements


- Deepak Lakkad
MyDGV.zip

Avatar of ZhaolaiZhaolai🇺🇸

Add the following code to the end of your form's code. It's a class. If you want, your can make it a separate class.
Build your app.
Then you will see a myDataGirdView control in the toolbox.
Drag it to your form, just like a normal DataGridView control.
Now you can press Enter key to move to the next column.
Class myDataGridView
    Inherits DataGridView

    Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        If keyData = Keys.Enter Then
            Dim col As Integer = Me.CurrentCell.ColumnIndex
            Dim row As Integer = Me.CurrentCell.RowIndex

            If Not (row = Me.NewRowIndex) Then
                If col = (Me.Columns.Count - 1) Then
                    col = -1
                    row -= 1
                End If
                Me.CurrentCell = Me(col + 1, row)
            End If

            Return True
        End If
        If keyData = Keys.Back Then
            Dim canGoBack As Boolean = True
            If Not (Me.EditingControl Is Nothing) Then
                If TypeOf Me.EditingControl Is TextBox Then
                    Dim tx As TextBox = CType(Me.EditingControl, TextBox)
                    If Not (tx.SelectionStart = 0) Then
                        canGoBack = False
                    End If
                End If
            End If
            Dim col As Integer = Me.CurrentCell.ColumnIndex
            Dim row As Integer = Me.CurrentCell.RowIndex

            If row = 0 AndAlso col = 0 Then
                canGoBack = False
            End If
            If canGoBack Then
                If col = 0 Then
                    col = Me.Columns.Count - 1
                    row -= 1
                Else
                    col -= 1
                End If
                Me.CurrentCell = Me(col, row)
                Return True
            End If
        End If
        Return MyBase.ProcessDialogKey(keyData)
    End Function

    Protected Overloads Overrides Function ProcessDataGridViewKey(ByVal e As KeyEventArgs) As Boolean
        If e.Control AndAlso e.KeyCode = Keys.Left Then
            Return Me.ProcessLeftKey(e.KeyCode)
        End If
        If e.Control AndAlso e.KeyCode = Keys.Right Then
            Return Me.ProcessRightKey(e.KeyCode)
        End If
        If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Left OrElse e.KeyCode = Keys.Right Then
            Return False
        End If
        Return MyBase.ProcessDataGridViewKey(e)
    End Function

    Protected Overloads Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        If e.KeyData = Keys.Enter Then
            Dim col As Integer = Me.CurrentCell.ColumnIndex
            Dim row As Integer = Me.CurrentCell.RowIndex
            If Not (row = Me.NewRowIndex) Then
                If col = (Me.Columns.Count - 1) Then
                    col = -1
                    row += 1
                End If
                Me.CurrentCell = Me(col + 1, row)
            End If
            e.Handled = True
        End If
        MyBase.OnKeyDown(e)
    End Sub
End Class

Open in new window


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of alicealiciasarawakalicealiciasarawak

ASKER

Zhaolai,

I want to create the sample as I posted at the top in the SharpDevelop. Can you guide me how to create it? Can I have a complete code for this?

Your help is needed.

deepaklakkad,

I try your code in SharpDevelop, and I got this error:

Type 'DataTable' is not defined. (BC30002)
Type 'DataRow' is not defined. (BC30002)
Type 'DataRow' is not defined. (BC30002)
Type 'DataRow' is not defined. (BC30002)
Name 'myDGVData' is not declared. (BC30451)
Name 'myDGVData' is not declared. (BC30451)
function 'ProcessDialogKey' cannot be declared 'Overrides' because it does not override a function in a base class. (BC30284)
'ProcessDialogKey' is not a member of 'Object'. (BC30456)
function 'ProcessDataGridViewKey' cannot be declared 'Overrides' because it does not override a function in a base class. (BC30284)
'ProcessDataGridViewKey' is not a member of 'Object'. (BC30456)
'RowCount' is not a member of 'Object'. (BC30456)
'CurrentCell' is not a member of 'Object'. (BC30456)
'ColumnCount' is not a member of 'Object'. (BC30456)
'CurrentCell' is not a member of 'Object'. (BC30456)
'RowCount' is not a member of 'Object'. (BC30456)
'Rows' is not a member of 'Object'. (BC30456)
'CurrentCell' is not a member of 'Object'. (BC30456)
'NewRowIndex' is not a member of 'Object'. (BC30456)
'CurrentCell' is not a member of 'Object'. (BC30456)
'ColumnCount' is not a member of 'Object'. (BC30456)
'CurrentCell' is not a member of 'Object'. (BC30456)
'CurrentCell' is not a member of 'Object'. (BC30456)
'Rows' is not a member of 'Object'. (BC30456)
'CurrentCell' is not a member of 'Object'. (BC30456)
'ProcessRightKey' is not a member of 'Object'. (BC30456)
'ProcessRightKey' is not a member of 'Object'. (BC30456)

How to solve it?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


deepaklakkad,

I have used the sample code you provided, and thank you so much for the help. Now I got the problem.

When I am the Hazen at Location - 1 and I press Enter key, it will move the cursor to the Location-2. How to automatically move it to the Temperature, and not to the first column?

Please help me. I really need your assistance.

Hi,

MyDGV.vb is a user control derived from DataGridView.

It has two overriden methods (i)  ProcessDialogKey and (ii) ProcessDataGridViewKey and a Shadowed function (i) ProcessrightKey
In shadowed function, i have written the code to move to next column when enter is press.


Are you using that User Control or a regular DataGridView Control?


Hi

Ignore above answer.

I have made some changes in UserControl. Code is given below:
 Public Shadows Function ProcessrightKey(ByVal keyData As Keys) As Boolean
        Dim key As Keys = (keyData And Keys.KeyCode)
        If key = Keys.Enter Then
            If ((MyBase.CurrentCell.ColumnIndex = (MyBase.ColumnCount - 1)) And (MyBase.CurrentCell.RowIndex = (MyBase.RowCount - 1))) Then
                MyBase.CurrentCell = MyBase.Rows(MyBase.RowCount - 1).Cells(1)
                Return True
            End If

            If ((MyBase.CurrentCell.ColumnIndex = (MyBase.ColumnCount - 1)) And (MyBase.CurrentCell.RowIndex + 1 <> MyBase.NewRowIndex)) Then
                MyBase.CurrentCell = MyBase.Rows(MyBase.CurrentCell.RowIndex + 1).Cells(1)
                Return True
            End If
            Return MyBase.ProcessRightKey(keyData)
        End If
        Return MyBase.ProcessRightKey(keyData)
    End Function

Open in new window


In
         MyBase.CurrentCell = MyBase.Rows(MyBase.CurrentCell.RowIndex + 1).Cells(0)
and
         MyBase.CurrentCell = MyBase.Rows(MyBase.RowCount - 1).Cells(1)
statements, I have replaced Cells(0) with Cells(1).

- Deepak Lakkd

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Thanks deepaklakkad. I got another question regarding change the backcolor of the first column to Color.Aqua. I try to use dtData.Columns(0).DefaultCellStyle.BackColor = Color.Aqua but I got the following error:
'DefaultCellStyle' is not a member of 'System.Data.DataColumn'. (BC30456)

Is there any alternative way to change the first column backcolor so that the user will know the first column is not editable.

deepaklakkad,

How to resize the datagridview? The example of code you posted cannot resize. I have a combobox at the top, and the datagridview hide the combobox, because the datagridview take up the whole form.

How to change the datagridview size and to be centered and not take up the whole place?

Hi,

for resizeable DGV, change Dock property to "None".

Below is changed code for your background color's requirement

Public Class frmData

    Dim dtData As New DataTable

    ' New Sub is created
    Sub PrepareDataGridView()
        ' Create Columns in DataGridView
        Dim location As New DataGridViewTextBoxColumn
        location.DefaultCellStyle.BackColor = Color.Aqua        ' Set Background Color
        myDGVData.Columns.Add(location)

        Dim temperature As New DataGridViewTextBoxColumn
        myDGVData.Columns.Add(temperature)

        Dim ph As New DataGridViewTextBoxColumn
        myDGVData.Columns.Add(ph)

        Dim hazen As New DataGridViewTextBoxColumn
        myDGVData.Columns.Add(hazen)
    End Sub

    ' New Sub is created
    Sub BindDataToDGV()
        myDGVData.Columns(0).DataPropertyName = dtData.Columns("Location").ToString
        myDGVData.Columns(1).DataPropertyName = dtData.Columns("Temperature").ToString
        myDGVData.Columns(2).DataPropertyName = dtData.Columns("Ph").ToString
        myDGVData.Columns(3).DataPropertyName = dtData.Columns("Hazen").ToString
    End Sub

    Private Sub FillData()
        ' Create Columns in DataTable
        dtData.Columns.Add("Location")
        dtData.Columns.Add("Temperature")
        dtData.Columns.Add("Ph")
        dtData.Columns.Add("Hazen")

        ' Create Rows
        Dim dr1 As DataRow
        dr1 = dtData.NewRow

        dr1("Location") = "Location - 1"
        dr1("Temperature") = 22
        dr1("Ph") = 32
        dr1("Hazen") = 34

        ' Add Row to DataTable
        dtData.Rows.Add(dr1)

        Dim dr2 As DataRow
        dr2 = dtData.NewRow

        dr2("Location") = "Location - 2"
        dr2("Temperature") = 25
        dr2("Ph") = 37
        dr2("Hazen") = 38

        ' Add Row to DataTable
        dtData.Rows.Add(dr2)

        Dim dr3 As DataRow
        dr3 = dtData.NewRow

        dr3("Location") = "Location - 3"
        dr3("Temperature") = 28
        dr3("Ph") = 39
        dr3("Hazen") = 36

        ' Add Row to DataTable
        dtData.Rows.Add(dr3)

        'Set Data Source of the DataGridView
        myDGVData.DataSource = dtData
        myDGVData.AutoGenerateColumns = False       ' New Statement Added

        BindDataToDGV()                             ' New Statement Added

        myDGVData.Columns(0).ReadOnly = True
        myDGVData.Columns(0).Frozen = True
    End Sub

    Private Sub frmData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Prepare DataGridView
        PrepareDataGridView()               ' New Statement Added

        ' Fill Data to DataGridView
        FillData()
    End Sub

End Class

Open in new window



I have added comment where new code is place.

Hope this will solve your problem

- Deepak Lakkad

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


deepaklakkad,

I copied your code and I got the following:

 User generated image
It seems to have redundant columns. Which part should I delete?

And how to set the focus to second column?

ASKER CERTIFIED SOLUTION
Avatar of Deepak LakkadDeepak Lakkad🇮🇳

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

deepaklakkad,

Thanks a lot for the help. I got one more last question. How to set the the first row "Location - 1' not editable? Meaning to say user cannot key in the data for the first row, the first row will display the sample of data to be entered, and user will only to be able to key in record in the second row, and second column.

This is the last part of the problem, and thanks for helping me a lot, as I am the newbie to vb.net. I really appreciated your help a lot. Thank you so much.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Hi

put following code into BindDataToDGV()

myDGVData.Rows(0).ReadOnly = True

- Deepak Lakkad

deepaklakkad,

Thanks so much. Really appreciate your help.

I got another question. Let say for Location-1, we no need to key in the ph, how to automatically jump it to the hazen column after I key in the temperature and press enter?  And for Location-2, we no need to key in the temperature, ph and hazen, so the whole row can be skipped. How to automatically jump to the third row after I key in the hazen and press enter for Location-1?

Hope you can help me on this. This is the final part that I needed.

Hi

As I am looking to the question, that you are adding more and more points to your original question.

I think this is not right way for getting solution.

If you feel that your requirement is satisfied then close the question. For new query, you may ask other question.

BTW, for above question, consider following points:

1.  "Temperature" is second column where user has keyed in some data. Now user will decide that he need or not need to key in value for the "ph" (third column). So,application cannot take decision that now cursor should skip "ph" column and move to "hazen" (forth column) column.
2. Same way, application cannot decide that user will not enter data for second row so now skip the second row.
3. You can move to other columns by this by pressing Arrow Keys or Enter Key from key board. Same way to skip row you can use arrow keys.


I think, we should close this question here.

- Deepak Lakkad




- Deepak Lakkad

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.



I have given solution of main query raised by  alicealiciasarawak - then he had added more and more new points to the query - almost all query is solved by me for him.

Now how can he close the question without accepting it?

deepaklakkad,

Very sorry to the mistake that I have made. I am new registered member, and I don't know how the procedure or process goes. Really sorry and apologize for what I have made. I don't mean to do it. Hope you understand it.

_alias99,
Really sorry for what I have make. I don't know that I need to reward the points to the expert. Now I know about it. Really sorry and apologize for what I have make. I really don't mean it. Hope that deepaklakkad will understand and forgive the mistake that I have done.

deepaklakkad,

Please forgive me for the mistake that I have been made. I really appreciate your help and apologize that I didn't reward the points to you. Just now I get to know that I need to do so. Really sorry for that. Hope you will forgive me, and I have rewarded the 500 points to you. Hope you don't mind for the delay.

Your help is very useful to me, and really thank you for what you have posted. Hope that you will still help me on the other problem that I posted.


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Hi


alicealiciasarawak, I am happy to help you.

You may ask more questions related with this solution - using this thread - I will try to help you

Thanks

- Deepak Lakkad

deepaklakkad,

Thank you so much. You are really expert.

I got some more problem that needed your help. And how to add on the points to you?  I need validation for data entry to be numbers and decimal point only. It only can allow ".1234567890". How to do that?

And I also found out another problem. Hope you don't mind to advice and help me. I have posted the topic at https://www.experts-exchange.com/questions/26674797/DataGridView-in-MdiParent-The-rows-0-readonly-and-currentcell-not-working.html?anchorAnswerId=34339518#a34339518. Hope you can help me and get the points.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


deepaklakkad,

I also tried the code you posted here, and it works good. I also need your help. I want to have validate checking on the user input, less say for column 1 I want to validate user to enter value not more than 300, and for column 2 I want to validate user to enter value not more than 1000.

How to do this? Can you please help
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.