Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridview and TextBox - Values

Status
Not open for further replies.

Alexandrumbm

Programmer
Jun 20, 2007
13
RO
Hello to all,

This is my fors post in this forum and i am glad to see alot of people right here.

1. I am a very very newb programmer.
2. I am working to a project that have a DatagridView with an Access DataBase.

This DataGridView has 3 columns that is populating thru a BindingSource. (This step is done).

On the same form i have 3 TextBoxes and 3 Buttons called "Add", "Edit" and "Delete".

I don't know how to make those Buttons to work for me on that DataGridView.

Can anyone help me to solve this problem ?!

Thanks in advice!
 
start with getting the values to populate the text boxes when you SELECT a row,

After that you will want to be able to alter the values then hit a SAVE/Update button and run an UPDATE statement for the info that you are working with.

let me know when you get that far, or have more questions.

-The answer to your problem may not be the answer to your question.
 
Hello,

See the following properties in DataRowView, AllowUserToAddRows and AllowUserToDeleteRows. If you are bound to a data source (eg a DataView) then check AllowNew, AllowEdit and AllowDelete properties there as well. Good Luck!
 
start with getting the values to populate the text boxes when you SELECT a row,"

This is done...

I don't know how to do this statement...

"After that you will want to be able to alter the values then hit a SAVE/Update button and run an UPDATE statement for the info that you are working with."

let me know when you get that far, or have more questions.
-The answer to your problem may not be the answer to your question.
 
Are you populating the datagrid with a "wizard" or do you have actually that is filling the datasource for the datagrid?


Sidebar-
you have the form with the datagrid, and the 3 textboxes. Hopefully you have some form of Key, so that if you change all 3 values you have some way of pointing to the correct value to update. Usually you will have a column that you do not show the user that is your primary key. So that when you do your updates you can "Tie" to the primary key and not accidentally update the wrong row.


-The answer to your problem may not be the answer to your question.
 
That's my actualy code...

-----------------------------


Public Class frm_plan_de_conturi

Private Sub Gecont_pcBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.Gecont_pcBindingSource.EndEdit()
Me.Gecont_pcTableAdapter.Update(Me.MbmgcntDataSet.gecont_pc)

End Sub

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

'TODO: This line of code loads data into the 'MbmgcntDataSet.gecont_pc' table. You can move, or remove it, as needed.
Me.Gecont_pcTableAdapter.Fill(Me.MbmgcntDataSet.gecont_pc)


'Activare doar citire pentru grid
DataGridView1.ReadOnly = True

'Ascunderea coloanelor de ID si Data Adaugare
DataGridView1.Columns(0).Visible = False
DataGridView1.Columns(3).Visible = False


'Redimensionarea coloanelor vizibile
DataGridView1.Columns(1).Width = 100
DataGridView1.Columns(2).Width = 820

'Dezactivarea redimensionarii pentru coloana de Simbol Conturi
DataGridView1.Columns(1).Resizable = DataGridViewTriState.False

'Afisarea numarului de inregistrari in baza de date
'pentru planul de conturi
lblContorPC.Text = "Total inregistrari in planul de conturi: " & DataGridView1.RowCount


End Sub

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

Private Sub cmdModificare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModificare.Click

If cmdModificare.Text = "Salvare" Then

cmdModificare.Text = "Modificare"
cmdIesire.Enabled = True
cmdAdaugare.Enabled = True
cmdStergere.Visible = True
Panel1.Width = 980
Panel1.Left = 12
Panel2.Visible = False
Panel3.Visible = True
cmdIesire.Focus()

Else

txtSimbolCont.Text = ""
txtDenumireCont.Text = ""
txtSimbolCont.Text = CStr(DataGridView1.CurrentRow.Cells(1).Value)
txtDenumireCont.Text = CStr(DataGridView1.CurrentRow.Cells(2).Value)
Panel2.Visible = True
Panel3.Visible = False
Panel1.Width = 312
Panel1.Left = 680
cmdIesire.Enabled = False
cmdAdaugare.Enabled = False
cmdStergere.Visible = False
txtSimbolCont.Focus()
cmdModificare.Text = "Salvare"

End If

End Sub

Private Sub cmdAdaugare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdaugare.Click

If cmdAdaugare.Text = "Salvare" Then

'DataGridView1.Rows.Add()

cmdAdaugare.Text = "Adaugare"

txtSimbolCont.Text = CStr(DataGridView1.CurrentRow.Cells(1).Value)
txtDenumireCont.Text = CStr(DataGridView1.CurrentRow.Cells(2).Value)

cmdIesire.Enabled = True
cmdModificare.Enabled = True
cmdStergere.Visible = True
Panel1.Width = 980
Panel1.Left = 12
Panel2.Visible = False
Panel3.Visible = True
cmdIesire.Focus()

Else

Panel2.Visible = True
Panel3.Visible = False
Panel1.Width = 312
Panel1.Left = 680
cmdIesire.Enabled = False
cmdModificare.Enabled = False
cmdStergere.Visible = False
txtSimbolCont.Text = ""
txtDenumireCont.Text = ""
txtSimbolCont.Focus()
cmdAdaugare.Text = "Salvare"

End If

End Sub

' Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

' txtSimbolCont.Text = DataGridView1.Item(1, e.RowIndex).Value
' txtDenumireCont.Text = DataGridView1.Item(2, e.RowIndex).Value

' End Sub

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

cmdModificare.PerformClick()
txtSimbolCont.Text = DataGridView1.Item(1, e.RowIndex).Value
txtDenumireCont.Text = DataGridView1.Item(2, e.RowIndex).Value

End Sub

Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
If MessageBox.Show("Sunteti sigur ca doriti stergerea datelor?", "Delete Row", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub

Private Sub CheckBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.Click

CheckBox2.Checked = False
CheckBox3.Checked = False
DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

End Sub

Private Sub CheckBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox2.Click

CheckBox1.Checked = False
CheckBox3.Checked = False
DataGridView1.Sort(DataGridView1.Columns(1), System.ComponentModel.ListSortDirection.Ascending)

End Sub

Private Sub CheckBox3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox3.Click

CheckBox2.Checked = False
CheckBox1.Checked = False
DataGridView1.Sort(DataGridView1.Columns(2), System.ComponentModel.ListSortDirection.Ascending)

End Sub

Private Sub cmdStergere_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStergere.Click

'If Not DataGridView1.CurrentRow.IsNewRow Then
'DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
'Gecont_pcTableAdapter.Update(MbmgcntDataSet)
'End If

frm_stergere_date_pc.show()

End Sub
End Class

-----------------------------
 
For the "Delete" action is working... i have comment that because i want to make a separate form for deleting with confirmation (not winth msgbox)....

Private Sub cmdStergere_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStergere.Click

'If Not DataGridView1.CurrentRow.IsNewRow Then
'DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
'Gecont_pcTableAdapter.Update(MbmgcntDataSet)
'End If

frm_stergere_date_pc.show()

End Sub
 
MbmgcntDataSet.gecont_pc

Do you have a dataset on your form that holds your connection to the access database? I am trying to figure out how you are filling the datagridview.

FYI: datagridview1.AutoResizeColumns()

You can set the datagridview selection so that it selects the entire row all at once.
Then set the datagridview so that you can only select 1 row at a time.
Then add a event to the Selection changed and poulate the text boxes from there.

Ex:
TextBox1.text = datagridview1.SelectedRows(0).Cells("Column1").Value.ToString



-The answer to your problem may not be the answer to your question.
 
Sorry, i am trying to make sense of your code but the french (??) makes it difficult.

-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top