bang head here Posted January 27, 2003 Posted January 27, 2003 (edited) I have some code that makes a DataGrid editable. When you click on "Edit" the values are shown in a text box and then you have the ability to edit the text and update the record. Fantastic. Here's where things go horrible wrong. What I'm looking to do is to not have those text boxes, but rather dropdownlists populated from a couple queries. So that like the field "Name" is taken from a query. Well I can't seem to find where the code is placed to create the text boxes. In fact there isn't any. So my question is...is there some attribute that I'm just not seeing that creates these text boxes? And can I do my dropdownlist idea in a DataGrid? This is exactly what I've been looking for, and the idea is great. I'm afraid thatI'm trying to do too much in the DataGrid. Below is the file. Maybe someone can see something that I just can't. Many thanks! <%@ Page Language="VB" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server"> Public Sub Page_Load(Source As Object, E As EventArgs) If Not Page.IsPostBack Then BindData() End If End Sub Public Sub DataGrid_Edit(Source As Object, E As DataGridCommandEventArgs) DailyGrid.EditItemIndex = E.Item.ItemIndex BindData() End Sub Public Sub DataGrid_Cancel(Source As Object, E As DataGridCommandEventArgs) DailyGrid.EditItemIndex = -1 BindData() End Sub Public Sub DataGrid_Update(Source As Object, E As DataGridCommandEventArgs) Dim myConnection As SqlConnection Dim myCommand As SqlCommand 'record update stuff Dim txtName As TextBox = E.Item.Cells(2).Controls(0) Dim txtAddress As TextBox = E.Item.Cells(3).Controls(0) Dim strUpdateStmt As String strUpdateStmt = "UPDATE Person SET " & _ "Name = '" & txtName.Text & "', " & _ "Address = '" & txtAddress.Text & "' " & _ "WHERE ID = " & E.Item.Cells(1).Text myConnection = New SqlConnection( _ "server=eclipse;database=CitadelSQL;uid=sa;pwd=;") myCommand = New SqlCommand(strUpdateStmt, myConnection) myConnection.Open() myCommand.ExecuteNonQuery() DailyGrid.EditItemIndex = -1 BindData() End Sub Public Sub BindData() Dim myDataSet As New DataSet Dim mySqlDataAdapter As SqlDataAdapter mySqlDataAdapter = New SqlDataAdapter( _ "SELECT * FROM Person", _ "server=eclipse;database=CitadelSQL;uid=sa;pwd=;") mySqlDataAdapter.Fill(myDataSet, "Person") DailyGrid.DataSource = myDataSet.Tables("Person") DailyGrid.DataBind() End Sub </script> <html> <head> </head> <body> <form method="post" runat="server"> <p> <asp:DataGrid id="DailyGrid" runat="server" OnUpdateCommand="DataGrid_Update" OnCancelCommand="DataGrid_Cancel" OnEditCommand="DataGrid_Edit" AutoGenerateColumns="False" BorderStyle="None" GridLines="Vertical" BorderWidth="1px" BorderColor="#999999" BackColor="White" CellPadding="4" AllowSorting="True"> <FooterStyle forecolor="Black" backcolor="#CCCCCC"></FooterStyle> <HeaderStyle font-bold="True" forecolor="White" backcolor="#000084"></HeaderStyle> <PagerStyle horizontalalign="Center" forecolor="Black" backcolor="#999999" mode="NumericPages"></PagerStyle> <SelectedItemStyle font-bold="True" forecolor="White" backcolor="#008A8C"></SelectedItemStyle> <AlternatingItemStyle backcolor="Gainsboro"></AlternatingItemStyle> <ItemStyle forecolor="Black" backcolor="#EEEEEE"></ItemStyle> <Columns> <asp:EditCommandColumn ButtonType="LinkButton" HeaderText="Action" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn> <asp:BoundColumn DataField="ID" ReadOnly="True" HeaderText="ID"></asp:BoundColumn> <asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn> <asp:BoundColumn DataField="Address" HeaderText="Address"></asp:BoundColumn> </Columns> </asp:DataGrid> </p> </form> </body> </html> [edit]formated page width[/edit] Edited January 27, 2003 by Robby Quote
aikeith Posted January 28, 2003 Posted January 28, 2003 You will want to create a template column. You can then insert web controls (such as a dropdown) into the grid. <asp:DataGrid id=DataGrid1 runat="server"> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:DropDownList id=DropDownList1 runat="server"></asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> Quote
bang head here Posted January 28, 2003 Author Posted January 28, 2003 Ok, I get it. I need to have an ItemTemplate for regular viewing, and an EditItemTemplate for edit view. When I include the code below, I get nothing in the grid. It's just not showing up. <asp:TemplateColumn SortExpression="CompanyName" HeaderText="Name"> <ItemTemplate> <asp:BoundColumn DataField="CompanyName" HeaderText="Name"></asp:BoundColumn> </ItemTemplate> <EditItemTemplate> <asp:DropDownList runat="server" id="lstCompanyName" DataTextField="CompanyName" DataSource='<%# GetList() %>' /> </EditItemTemplate> </asp:TemplateColumn> here is the GetList() funct: Function GetList() as DataSet Dim myConnection As SqlConnection Dim ddlDataSet as DataSet = New DataSet() Const strSQLDDL as String = _ "SELECT CompanyName FROM Customers ORDER BY CompanyName" myConnection = New SqlConnection( _ "server=eclipse;database=Northwind;uid=sa;pwd=;") Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQLDDL, myConnection) myDataAdapter.Fill(ddlDataSet, "CompanyName") Return ddlDataSet End Function I mean it's just like I don't quite see the way to get the ItemTemplate to appear on a view, and the EditItemTemplate to appear when the DataGrid is being edited. Thanks for any help anyone can give me. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.