B
Bill Blancett
Pardon me, I am new to the .NET environment but I am stuck with a problem
here. Hopefully there is a simple solution. I have a datagrid control with 9
fields that it pulls from the database. It pulls these fields correctly. But
I added a 10th field, a template column to the datagrid. Inside this
template column is a web form control that is a textbox. The idea is to pull
the records from the database and allow the user to update mutliple rows of
a field using a textbox. The problem is that I do not know how to retrieve
the value from the textbox in the datagrid. The plan was to loop through
each row in the datagrid and grab the value from the texbox field and update
the database using a SQL UPDATE statement. But I can't do that since I don't
know the command or syntax to grab the value from that last cell, textbox.
Does anyone know how to get this ?
Here is what the datagrid looks like in the HTML view:
<asp:datagrid id="dgEmp" style="Z-INDEX: 101; LEFT: 432px;POSITION:
absolute; TOP: 200px" runat="server"AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="EmployeeID"
HeaderText="EmployeeID"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName"
HeaderText="LastName"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName"
HeaderText="FirstName"></asp:BoundColumn>
<asp:BoundColumn DataField="Title"
HeaderText="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="Address"
HeaderText="Address"></asp:BoundColumn>
<asp:BoundColumn DataField="City"
HeaderText="City"></asp:BoundColumn>
<asp:BoundColumn DataField="Region"
HeaderText="Region"></asp:BoundColumn>
<asp:BoundColumn DataField="PostalCode"
HeaderText="PostalCode"></asp:BoundColumn>
<asp:BoundColumn DataField="HomePhone"
HeaderText="HomePhone"></asp:BoundColumn>
------------------------MY TEMPLATE COLUMN
BELOW---------------------------------------------------------------
<asp:TemplateColumn HeaderText="Notes">
<ItemTemplate>
<asp:textbox id="txtNotes" runat="server"></asp:textbox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
----------------------------------------------------------------------------
------------------------------------------------------------
Now when I go to code view underneath the cmdUpdate_Click action of my
update button I have this:
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdUpdate.Click
Dim rowCount As Integer = 0
Dim strSQL As String
Dim intID As Integer
'Loop through each row in the datagrid and update values in the database
with values from the textbox field.
Dim dgEmpItem As DataGridItem
Dim cmd As New SqlCommand
cmd.Connection = cn
For Each dgEmpItem In dgEmp.Items
-------------------BELOW IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE----------------
Dim myTextbox As TextBox =
CType(dgEmpItem.Cells(9).Controls(1),TextBox) ------------COULD YOU GIVE
ME THE CORRECT WAY TO PULL THE VALUE
-------------------ABOVE IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE-----------------
Dim myTextString As String = Str(myTextbox.Text)
rowCount += 1
intID = CInt(dgEmp.DataKeys(dgEmpItem.ItemIndex).ToString())
strSQL = "UPDATE Employees SET Notes = '" & myTextString & "' WHERE
EmployeeID =" & intID
cmd.CommandText = strSQL
cmd.ExecuteNonQuery()
Next
End Sub
here. Hopefully there is a simple solution. I have a datagrid control with 9
fields that it pulls from the database. It pulls these fields correctly. But
I added a 10th field, a template column to the datagrid. Inside this
template column is a web form control that is a textbox. The idea is to pull
the records from the database and allow the user to update mutliple rows of
a field using a textbox. The problem is that I do not know how to retrieve
the value from the textbox in the datagrid. The plan was to loop through
each row in the datagrid and grab the value from the texbox field and update
the database using a SQL UPDATE statement. But I can't do that since I don't
know the command or syntax to grab the value from that last cell, textbox.
Does anyone know how to get this ?
Here is what the datagrid looks like in the HTML view:
<asp:datagrid id="dgEmp" style="Z-INDEX: 101; LEFT: 432px;POSITION:
absolute; TOP: 200px" runat="server"AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="EmployeeID"
HeaderText="EmployeeID"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName"
HeaderText="LastName"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName"
HeaderText="FirstName"></asp:BoundColumn>
<asp:BoundColumn DataField="Title"
HeaderText="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="Address"
HeaderText="Address"></asp:BoundColumn>
<asp:BoundColumn DataField="City"
HeaderText="City"></asp:BoundColumn>
<asp:BoundColumn DataField="Region"
HeaderText="Region"></asp:BoundColumn>
<asp:BoundColumn DataField="PostalCode"
HeaderText="PostalCode"></asp:BoundColumn>
<asp:BoundColumn DataField="HomePhone"
HeaderText="HomePhone"></asp:BoundColumn>
------------------------MY TEMPLATE COLUMN
BELOW---------------------------------------------------------------
<asp:TemplateColumn HeaderText="Notes">
<ItemTemplate>
<asp:textbox id="txtNotes" runat="server"></asp:textbox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
----------------------------------------------------------------------------
------------------------------------------------------------
Now when I go to code view underneath the cmdUpdate_Click action of my
update button I have this:
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdUpdate.Click
Dim rowCount As Integer = 0
Dim strSQL As String
Dim intID As Integer
'Loop through each row in the datagrid and update values in the database
with values from the textbox field.
Dim dgEmpItem As DataGridItem
Dim cmd As New SqlCommand
cmd.Connection = cn
For Each dgEmpItem In dgEmp.Items
-------------------BELOW IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE----------------
Dim myTextbox As TextBox =
CType(dgEmpItem.Cells(9).Controls(1),TextBox) ------------COULD YOU GIVE
ME THE CORRECT WAY TO PULL THE VALUE
-------------------ABOVE IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE-----------------
Dim myTextString As String = Str(myTextbox.Text)
rowCount += 1
intID = CInt(dgEmp.DataKeys(dgEmpItem.ItemIndex).ToString())
strSQL = "UPDATE Employees SET Notes = '" & myTextString & "' WHERE
EmployeeID =" & intID
cmd.CommandText = strSQL
cmd.ExecuteNonQuery()
Next
End Sub