Results 1 to 4 of 4

Thread: edit textbox fields in datagrid?

  1. #1

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342

    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:
    1. <script language="VB" runat="server">
    2. Sub Page_Load(Sender as Object, E as EventArgs)
    3.   Dim objConn     As OleDbConnection
    4.   Dim objComm     As OleDbDataAdapter
    5.   Dim strConn     As String
    6.   Dim strComm     As String
    7.   Dim objDataSet  As New DataSet
    8.  
    9.   'Build the connection string
    10.   strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
    11.   strConn += "Data Source=G:\database\cptInfo.mdb;"
    12.   strConn += "Persist Security Info=False"
    13.  
    14.   'Build the SQL string
    15.   strComm = "SELECT cptFileInfo.Operator " 'Im grabbing 8 more cols in the real code
    16.   strComm += "FROM cptFileInfo"
    17.  
    18.   'Create the connection and command objects
    19.   objConn = New OleDbConnection(strConn)
    20.   objComm = New OleDbDataAdapter(strComm, objConn)
    21.  
    22.   'Fill the dataset with the results of the query
    23.   objComm.Fill(objDataSet, "cptFileInfo")
    24.  
    25.   'Set the grid source to the dataset and bind the data
    26.   cptGrid.DataSource=objDataSet.Tables("cptFileInfo").DefaultView
    27.   cptGrid.DataBind()
    28. End Sub
    29.  
    30. </script>

  2. #2

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    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:
    1. private Sub CreateDataGridTextBoxColumn()
    2.    Dim myTextBoxColumn As DataGridTextBoxColumn  = _
    3.    New DataGridTextBoxColumn()
    4.    ' Set the MappingName and Format properties.
    5.    myTextBoxColumn.MappingName = "OrderDate"
    6.    ' The character "D" means use the long date format.
    7.    myTextBoxColumn.Format = "D"
    8.    ' Add the column to the DataGridTableStyle object.
    9.    dataGrid1.TableStyles("Orders").GridColumnStyles. _
    10.    Add(myTextBoxColumn)
    11.  
    12. End Sub

    Any ideas why? I'm importing the correct namespace according to microsoft (<%@ import Namespace="System.Windows.Forms" %>)

    Thanks.

    Paul

  3. #3

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    'bump'

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    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
  •  



Click Here to Expand Forum to Full Width