|
-
Nov 13th, 2002, 11:44 AM
#1
Thread Starter
Hyperactive Member
edit textbox fields in datagrid?
I've seen a page that had a datagrid that was displaying the database data in textboxes. I didn't see the code, but the guy that made it said that when somebody changed data in the textbox, that it automatically updated in the data base when the textbox field changed.
How can I do this? I want to update an access database without using edit/update buttons. So far, I have a datagrid that shows the data from my db, but it doesn't show it in textboxes, and it's not editable.
Code:
<asp:DataGrid
AllowSorting="true"
AlternatingItemStyle-BackColor="#E7EBFA"
AutoGenerateColumns="false"
Width="800"
CellPadding="5"
id="cptGrid"
runat="server"
ShowFooter="false"
ShowHeader="true">
<HeaderStyle BackColor="#E6E6FF" />
<Columns>
<asp:TemplateColumn
SortExpression="Operator"
HeaderText="Operator"
ItemStyle-Width="100px" >
<ItemTemplate>
<%# Container.DataItem ( "Operator" ) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
That's what my datagrid looks like (I have about 8 more columns though). Here's some of the code w/ it:
VB Code:
<script language="VB" runat="server">
Sub Page_Load(Sender as Object, E as EventArgs)
Dim objConn As OleDbConnection
Dim objComm As OleDbDataAdapter
Dim strConn As String
Dim strComm As String
Dim objDataSet As New DataSet
'Build the connection string
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
strConn += "Data Source=G:\database\cptInfo.mdb;"
strConn += "Persist Security Info=False"
'Build the SQL string
strComm = "SELECT cptFileInfo.Operator " 'Im grabbing 8 more cols in the real code
strComm += "FROM cptFileInfo"
'Create the connection and command objects
objConn = New OleDbConnection(strConn)
objComm = New OleDbDataAdapter(strComm, objConn)
'Fill the dataset with the results of the query
objComm.Fill(objDataSet, "cptFileInfo")
'Set the grid source to the dataset and bind the data
cptGrid.DataSource=objDataSet.Tables("cptFileInfo").DefaultView
cptGrid.DataBind()
End Sub
</script>
-
Nov 13th, 2002, 01:10 PM
#2
Thread Starter
Hyperactive Member
Ok, I found some stuff on microsoft's site. I think I need to use the DataGridTextBox class. I tried the sample code below and got this error "Compiler Error Message: BC30002: Type 'DataGridTextBoxColumn' is not defined."
VB Code:
private Sub CreateDataGridTextBoxColumn()
Dim myTextBoxColumn As DataGridTextBoxColumn = _
New DataGridTextBoxColumn()
' Set the MappingName and Format properties.
myTextBoxColumn.MappingName = "OrderDate"
' The character "D" means use the long date format.
myTextBoxColumn.Format = "D"
' Add the column to the DataGridTableStyle object.
dataGrid1.TableStyles("Orders").GridColumnStyles. _
Add(myTextBoxColumn)
End Sub
Any ideas why? I'm importing the correct namespace according to microsoft (<%@ import Namespace="System.Windows.Forms" %>)
Thanks.
Paul
-
Nov 15th, 2002, 10:55 AM
#3
Thread Starter
Hyperactive Member
-
Nov 15th, 2002, 10:03 PM
#4
Here is a grid that I have, that I have enabled editing on:
Code:
<asp:datagrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 82px" runat="server" Width="391px" Height="50px" BorderStyle="None" BackColor="White" BorderColor="#CCCCCC" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnEditCommand="datagrid1_editcommand">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#CC3333"></SelectedItemStyle>
<AlternatingItemStyle Font-Size="Smaller" Font-Names="tahoma" BackColor="PaleGoldenrod"></AlternatingItemStyle>
<ItemStyle Font-Size="Smaller" Font-Names="Tahoma" BackColor="PowderBlue"></ItemStyle>
<HeaderStyle Font-Size="Smaller" Font-Names="Tahoma" Font-Bold="True" ForeColor="White" BackColor="#000000"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn></asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="White"></PagerStyle>
</asp:datagrid>
Then in your code-behind you can do something like this:
Code:
Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
GridBind()
End Sub
Hope this helps,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|