How to automatically update my Datagrid from Textbox

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1562 Views - Last Post: 14 March 2015 - 03:40 AM Rate Topic: -----

#1 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 09:41 AM

Hi, i had reposted this topic from VB6 to VB.net.

Please help me guys, how to automatically update the values in that cell in the datagrid if im changing the value in the textbox...and vice versa...

Thanks in advance...


here is my code:




Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick
        Try
            If e.RowIndex >= 0 Then
                Dim row As DataGridViewRow
                row = Me.dataGridView1.Rows(e.RowIndex)
                TextBox1.Text = row.Cells("Product Title (EN)").Value.ToString
                TextBox2.Text = row.Cells("Brand").Value.ToString
                TextBox3.Text = row.Cells("Category Name").Value.ToString
                TextBox4.Text = row.Cells("Platform").Value.ToString
                RichTextBox1.Text = row.Cells("Bullet (EN)").Value.ToString


            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

      

    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: How to automatically update my Datagrid from Textbox

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 11:26 AM

So in the code above, you set your textbox's .Text properties to the values of the cells. The same holds true if you reverse it
Textbox1.Text = row.Cells("A Cell").Value.ToString

row.Cells("A Cell").Value = TextBox1.Text

Was This Post Helpful? 0
  • +
  • -

#3 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 12:19 PM

Hi Charlie May - sorry but still if i type on the textbox that correspond to the cell, it wont change, only if i type in the cell so as the textbox changed.. :-(
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 12:55 PM

If you're attempting this in that same event then it's probably not being triggered.

When you attempt to correct suggestions people are giving you, it is good practice to post the changes you made if you are still having issues. This ensures that we can see how you have applied the suggestion.

Outside that event, say like in a button click event you could try the following:

Datagridview1.CurrentRow.Cells("Brand").Value = TextBox2.Text


This takes the currently selected row (CurrentRow) and sets the cell you specified's value to whatever is in textbox2.text when you click the button.
Was This Post Helpful? 0
  • +
  • -

#5 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 01:35 PM

I tried that code - once i fire up the excel and opened in the datagrid, my "brand" cell becomes empty, and the textbox2 is also empty. And if i keep on clicking other cells in the datagrid, their brand gets empty too... :-( i mean im totally new to this... im sorry - i just want to change for example if the spelling is wrong so i can just change it in textbox or vice versa and updates
Was This Post Helpful? 0
  • +
  • -

#6 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 01:41 PM

this is what i did... and the textbox becomes empty and if i click on another row, their brand becomes empty too...(technically there should be a brand that comes from the excel).. i just want to make changes like spelling or so


Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick, dataGridView1.CellValueChanged, dataGridView1.CellContentDoubleClick
        Try
            If e.RowIndex >= 0 Then
                Dim row As DataGridViewRow
                row = Me.dataGridView1.Rows(e.RowIndex)

                TextBox1.Text = row.Cells("Product Title (EN)").Value.ToString
                row.Cells("Product Title (EN)").Value = TextBox1.Text
                dataGridView1.CurrentRow.Cells("Brand").Value = TextBox2.Text
                TextBox4.Text = row.Cells("Platform").Value.ToString
                RichTextBox1.Text = row.Cells("Bullet (EN)").Value.ToString


            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try



    End Sub

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6776
  • View blog
  • Posts: 27,942
  • Joined: 12-December 12

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 02:12 PM

I doubt very much that you want, or need, all this code to run on clicking, double-clicking or changing the value of a cell. It will only be problematic.
        TextBox1.Text = row.Cells("Product Title (EN)").Value.ToString
        row.Cells("Product Title (EN)").Value = TextBox1.Text

This code doesn't achieve anything, swapping the same value.

I think you should take a step back and decide exactly when, and how, you need code to run.

Please describe clearly what you need to happen and when.

If you use data binding, perhaps with the BindingSource Class, then both the DGV and TextBoxes can be bound to the same value. Then it would just require updating the DataSource (of the BindingSource) to commit changes back to the database.
Was This Post Helpful? 0
  • +
  • -

#8 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 02:41 PM

Hi - If only if you guys permit me to see my project then it will be nice...
Attached is my project. with the sample excel

I just want to change spellings in the text box like in the description then it will automatically update the datagrid...

Sorry if most of my button code is not yet finished.



Thanks in advance

Please view my project... :-(

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6776
  • View blog
  • Posts: 27,942
  • Joined: 12-December 12

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 03:19 PM

I personally have no desire to download your project but..

Quote

I just want to change spellings in the text box like in the description then it will automatically update the datagrid...

CharlieMay has already explained how to do this in his post #4:
Datagridview1.CurrentRow.Cells("Brand").Value = TextBox2.Text

This code will update the value of a DGV's cell with the Text in the TextBox. You could run this code on a button-click event, or perhaps the TextBox's Leave event.

This post has been edited by andrewsw: 08 March 2015 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: How to automatically update my Datagrid from Textbox

Posted 08 March 2015 - 03:22 PM

Or even the textchanged event.

As andrewsw pointed out, you need to decide when you want that change to take affect. Any of the 3 options given my both of us would be a good place to start.
Was This Post Helpful? 0
  • +
  • -

#11 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

Re: How to automatically update my Datagrid from Textbox

Posted 09 March 2015 - 07:46 AM

WOW!!!! thanks guys... it help me a lot...im not paying more attention to textbox leave event...but now it works... can i ask one more quick question?
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: How to automatically update my Datagrid from Textbox

Posted 09 March 2015 - 07:49 AM

Great!, glad you got it working. Ask away!
Was This Post Helpful? 0
  • +
  • -

#13 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

Re: How to automatically update my Datagrid from Textbox

Posted 09 March 2015 - 07:58 AM

in this same project - my richtextbox shows text with <ul></ul> tags and <p>...(basically fed from the excel sheet) now my question is, how can i make them html, like bullets instead of those tags..?? Please......


 Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick, dataGridView1.CellValueChanged, dataGridView1.CellContentDoubleClick
        Try
            If e.RowIndex >= 0 Then
                Dim row As DataGridViewRow
                row = Me.dataGridView1.Rows(e.RowIndex)
                TextBox1.Text = row.Cells("Product Title (EN)").Value.ToString
                row.Cells("Product Title (EN)").Value = TextBox1.Text
                TextBox2.Text = row.Cells("Brand").Value.ToString
                row.Cells("Brand").Value = TextBox2.Text
                TextBox4.Text = row.Cells("Platform").Value.ToString
                row.Cells("Platform").Value = TextBox4.Text
                RichTextBox1.Text = row.Cells("Bullet (EN)").Value.ToString


            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try



    End Sub
    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        dataGridView1.CurrentRow.Cells("Brand").Value = TextBox2.Text
    End Sub

Was This Post Helpful? 0
  • +
  • -

#14 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: How to automatically update my Datagrid from Textbox

Posted 09 March 2015 - 08:12 AM

Instead of using a RichTextBox. Have you thought about using a WebBrowser control and setting the DocumentText?
Was This Post Helpful? 0
  • +
  • -

#15 AK-479000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 08-March 15

Re: How to automatically update my Datagrid from Textbox

Posted 09 March 2015 - 08:29 AM

:bigsmile: :bigsmile: :bigsmile: wohooooooo!!! your genius!!! yessss!!! thats what i need! you fixed it....

can i ask please my last question so that i will pass my programming subject.. really, this is my second try and i hope i wont fail... minor subject for my Eng'g class... please CharlieMay.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2